Search code examples
iosobjective-cuilabelnsattributedstring

How to Vertically Align Text Using UILabel and NSAttributedString


I am using UILabel and NSAttributedString to set linespacing for label texts in IOS7. But when i use this the text doesnt seems aligned centrally on the Label. Here is my code to set text (attributed) to the label.

-(void)setText:(NSString *)text
{
    [super setText:text];

    if(text)
        [self setLineSpace];
}
-(void)setLineSpace
{
    if([[[UIDevice currentDevice] systemVersion] floatValue] >= 7)
    {
        NSMutableAttributedString *string=[[NSMutableAttributedString alloc]initWithString:self.text];

        NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle defaultParagraphStyle] mutableCopy];
        paragraphStyle.alignment=NSTextAlignmentJustified;
        [paragraphStyle setLineSpacing:4] ;
//        paragraphStyle.minimumLineHeight =0;
//        paragraphStyle.maximumLineHeight=7;
     //  CTTextAlignment alignment = kCTCenterTextAlignment;
        [string addAttribute:NSParagraphStyleAttributeName value:paragraphStyle range:NSMakeRange(0, string.length)];
        self.text=nil;
        self.attributedText=string;

        [self setBackgroundColor:[UIColor redColor]];

    }

}

Here are some Screenshots ,BTW am subclassing UILabel to and overriding the setter to implement linespacing.

enter image description here


Solution

  • Actually I solved it ....:) :) .The problem was with the custom font i was using.

    Took some R&D but now it seems working

    Just created a subclass for label and override some default methods.

    - (CGRect)textRectForBounds:(CGRect)bounds limitedToNumberOfLines:(NSInteger)numberOfLines
    {
    
        CGRect textRect = [super textRectForBounds:bounds limitedToNumberOfLines:numberOfLines];
    
        if(([self.font.fontName isEqualToString:@"Gotham"]||[self.font.fontName isEqualToString:@"Gotham-Bold"]||[self.font.fontName isEqualToString:@"Gotham-Medium"])&&((int)[[[UIDevice currentDevice] systemVersion] floatValue])==6&&self.verticalAlignment==AlignMiddle)
        {
            textRect.origin.y = bounds.origin.y + (bounds.size.height - textRect.size.height) / 2.0;
            return [self addToRect:textRect x:0 y:2];
        }
        return [self addToRect:textRect x:0 y:0.5];
    }
    
    -(void)drawTextInRect:(CGRect)requestedRect
    {
        if(((int)[[[UIDevice currentDevice] systemVersion] floatValue])>=7)
           [super drawTextInRect:[self addToRect:requestedRect x:0 y:0.5]];
        else
        {
            CGRect actualRect = [self textRectForBounds:requestedRect limitedToNumberOfLines:self.numberOfLines];
            [super drawTextInRect:actualRect];
        }
    }
    -(CGRect)addToRect:(CGRect)rect x:(CGFloat)dx y:(CGFloat) dy
    {
        rect=CGRectMake(rect.origin.x+dx, rect.origin.y+dy, rect.size.width, rect.size.height);
        return rect;
    }
    -(CGRect)makeRect:(CGRect)rect x:(CGFloat)dx y:(CGFloat) dy
    {
        rect=CGRectMake(dx,dy, rect.size.width+dx, rect.size.height+dy);
        return rect;
    }
    -(CGRect)makeRects:(CGRect)rect x:(CGFloat)dx y:(CGFloat) dy
    {
        rect=CGRectMake(rect.origin.x,rect.origin.y, rect.size.width+dx, rect.size.height+dy);
        return rect;
    }
    -(void)truncateToFit
    {
        if([[[UIDevice currentDevice] systemVersion] floatValue] < 7)
        {
            [self sizeToFit];
            CGRect frame=self.frame;
        if(frame.size.height<self.frame.size.height)
            self.frame=[self makeRects:frame x:0 y:0.5];
        }
        else
        {
            self.frame=[self makeRects:self.frame x:0 y:0.5];
            [self sizeToFit];
        }
    }
    
    -(void)truncatesToFit
    {
        self.lineBreakMode=NSLineBreakByTruncatingTail;
    
            self.numberOfLines=2;
            [self sizeToFit];
            self.frame=[self makeRects:self.frame x:0 y:0.5];
    }
    
    
    
    
    -(void)truncatesToFit:(NSInteger )linesCount
    
    {
    
        self.numberOfLines=linesCount;
    
        [self sizeToFit];
    
        self.frame=[self makeRects:self.frame x:0 y:0.5];
    
    }