How to Vertically Align Text Using UILabel and NSAttributedString

Raon

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

Raon

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];

}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Is this possible to align the text vertically on UILabel?

From Java

Vertically align text within a UILabel (Note : Using AutoLayout)

From Dev

How to align a string by a single character UILabel/NSAttributedString

From Java

Vertically align text to top within a UILabel

From Dev

Vertically align text within a UILabel (xcode)

From Dev

How to align text vertically?

From Dev

How to align text in middle (vertically)

From Dev

How to align text vertically in JavaFX?

From Dev

centering text in a UILabel with an NSAttributedString

From Dev

How to align a text vertically using SVG with cross-browser compatibility?

From Dev

How to align UILabel text in paragraph

From Dev

How does UILabel vertically center its text?

From Dev

vertically align text based on image using bootstrap

From Dev

How to vertically align text in a definition list?

From Java

How do I vertically align text in a div?

From Java

How to vertically align text with icon font?

From Java

How to vertically align all text in CSS?

From Java

How to align View vertically to the text inside TextView?

From Dev

How to align vertically span text (floated) to bottom

From Dev

How to vertically align bootstrap buttons with text in a row?

From Dev

How to vertically align text in the middle of two divs

From Dev

How to align text horizontally & vertically in UITextView?

From Dev

How to Vertically "center" align the multi line text

From Dev

How to vertically align text in a TextView with Compound Drawable

From Dev

How to align text vertically equal top CSS

From Dev

How to vertically align text beside an image in the footer?

From Dev

How to vertically align text over an image

From Dev

How to vertically align bootstrap buttons with text in a row?

From Dev

How to align text horizontally & vertically in UITextView?

Related Related

  1. 1

    Is this possible to align the text vertically on UILabel?

  2. 2

    Vertically align text within a UILabel (Note : Using AutoLayout)

  3. 3

    How to align a string by a single character UILabel/NSAttributedString

  4. 4

    Vertically align text to top within a UILabel

  5. 5

    Vertically align text within a UILabel (xcode)

  6. 6

    How to align text vertically?

  7. 7

    How to align text in middle (vertically)

  8. 8

    How to align text vertically in JavaFX?

  9. 9

    centering text in a UILabel with an NSAttributedString

  10. 10

    How to align a text vertically using SVG with cross-browser compatibility?

  11. 11

    How to align UILabel text in paragraph

  12. 12

    How does UILabel vertically center its text?

  13. 13

    vertically align text based on image using bootstrap

  14. 14

    How to vertically align text in a definition list?

  15. 15

    How do I vertically align text in a div?

  16. 16

    How to vertically align text with icon font?

  17. 17

    How to vertically align all text in CSS?

  18. 18

    How to align View vertically to the text inside TextView?

  19. 19

    How to align vertically span text (floated) to bottom

  20. 20

    How to vertically align bootstrap buttons with text in a row?

  21. 21

    How to vertically align text in the middle of two divs

  22. 22

    How to align text horizontally & vertically in UITextView?

  23. 23

    How to Vertically "center" align the multi line text

  24. 24

    How to vertically align text in a TextView with Compound Drawable

  25. 25

    How to align text vertically equal top CSS

  26. 26

    How to vertically align text beside an image in the footer?

  27. 27

    How to vertically align text over an image

  28. 28

    How to vertically align bootstrap buttons with text in a row?

  29. 29

    How to align text horizontally & vertically in UITextView?

HotTag

Archive