views:

335

answers:

1

I have a UILabel and if i adjust the size of the text i can make it look loke a UITextView however the left margin is different, on the UIlabel the text is right up against the left border where the UITextView has a slight margin. How do i adjust the UILabel so that when these controls are placed above one another, they look consistent?

A: 

Simply change the label's frame:

CGRect frame = label.frame;
CGRect newFrame = CGRectMake(frame.origin.x + MARGIN, frame.origin.y, frame.size.width - MARGIN, frame.size.height);
label.frame = newFrame;

Of course replace MARGIN with whatever you want your margin to be.

Or you could subclass UILabel and override textRectForBounds:limitedToNumberOfLines: like so:

- (CGRect)textRectForBounds:(CGRect)bounds limitedToNumberOfLines:(NSInteger)numberOfLines {
    CGRect newBounds = CGRectMake(bounds.origin.x + MARGIN, bounds.origin.y, bounds.size.width - MARGIN, bounds.size.height);
    return [super textRectForBounds:newBounds limitedToNumberOfLines:numberOfLines];
}

Hope this helps!

robinjam
changing the labels frame doesn't work, subclassing does however. thanks!
Aran Mulholland
Happy to help! If this answer worked for you, please consider awarding the bounty :)
robinjam
done so, it wouldn't let me award it straight away. thanks.
Aran Mulholland