Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make a UILabel which adjusts it's text to the upper left?

Tags:

uilabel

iphone

For some strange reason, in iPhone OS 3.0 this doesn't work: I made a big fullscreen UILabel with numberOfLines = 0 and baselineAdjustment = UIBaselineAdjustmentNone.

It refuses to show the text in the upper left. It's always in the center of the bounding box, aligned to the left.

The documentation says:

UIBaselineAdjustmentNone Adjust text relative to the top-left corner of the bounding box. This is the default adjustment. Available in iPhone OS 2.0 and later.

Probably a framework bug? I started with shiny new labels to test it. Text is centered.

like image 222
dontWatchMyProfile Avatar asked Dec 29 '22 16:12

dontWatchMyProfile


1 Answers

The default implementation vertically centers text and does not honor the contentMode property. Implement drawTextInRect: in a subclass.

@implementation TopLeftLabel

-(void) drawTextInRect:(CGRect)inFrame {
    CGRect      draw = [self textRectForBounds:inFrame limitedToNumberOfLines:[self numberOfLines]];

    draw.origin = CGPointZero;

    [super drawTextInRect:draw];
}

@end

The baselineAdjustment property is strictly for when the font size is being adjusted to fit the width.

like image 159
drawnonward Avatar answered Feb 22 '23 22:02

drawnonward