I have a subclass of UILabel, which is supposed to update its text when the user types something. Naturally, as the length of text increases, the size of the label must adju
You should override the UILabel method (CGSize)sizeThatFits:(CGSize)size in your subclass like example below. I just add 10pt to the height calculated by UILabel to accommodate the subscript.
@implementation ESKLabel
- (CGSize)sizeThatFits:(CGSize)size
{
CGSize theSize = [super sizeThatFits:size];
return CGSizeMake(theSize.width, theSize.height + 10);
}
@end
Sample output:
self.eskLabel.text = @"Hello Long² Long\u2082 World";
NSLog(@"CGSize: %@", NSStringFromCGSize(self.eskLabel.frame.size));
[self.eskLabel sizeToFit];
NSLog(@"CGSize: %@", NSStringFromCGSize(self.eskLabel.frame.size));
From the NSLog:
This GDB was configured as "x86_64-apple-darwin".sharedlibrary apply-load-rules all Attaching to process 864.
2012-01-06 23:34:21.949 Stackoverflow4[864:f803] CGSize: {85, 61}
2012-01-06 23:34:21.951 Stackoverflow4[864:f803] CGSize: {302, 44}
kill
quit
This should to the trick:
self.eskLabel.adjustsFontSizeToFitWidth = YES;