If textLabel uses a single font, you can supply it in the attributes (Using .UsesFontLeading or NSStringDrawingUsesFontLeading option).
Swift
let size:CGSize = self.textLabel.attributedText.string.boundingRectWithSize(
CGSizeMake(self.textLabel.bounds.width, CGFloat.max),
options: (.UsesLineFragmentOrigin | .UsesFontLeading),
attributes: [NSFontAttributeName : font],
context: nil).size
Putting it all together (get the font from the attributed text) in Swift:
self.textLabel.attributedText.enumerateAttribute(
NSFontAttributeName,
inRange: NSMakeRange(0, self.textLabel.attributedText.length),
options: NSAttributedStringEnumerationOptions(0)) {
(value:AnyObject!, range:NSRange, stop:UnsafeMutablePointer<ObjCBool>) -> Void in
stop.initialize(true)
if let font:UIFont = value as? UIFont {
let size:CGSize = self.textLabel.attributedText.string.boundingRectWithSize(
CGSizeMake(self.textLabel.bounds.width, CGFloat.max),
options: (.UsesLineFragmentOrigin | .UsesFontLeading),
attributes: [NSFontAttributeName : font],
context: nil).size
self.textHeight.constant = size.height // Use height
}
}
Obj-C
CGSize size = [self.textLabel.attributedText.string
boundingRectWithSize:CGSizeMake(self.textLabel.bounds.size.width,
CGFLOAT_MAX)
options:(NSStringDrawingUsesLineFragmentOrigin |
NSStringDrawingUsesFontLeading)
attributes:@{NSFontAttributeName: font}
context:nil].size;
Putting it all together (get the font from the attributed text) in Obj-C:
[self.textLabel.attributedText
enumerateAttribute:NSFontAttributeName
inRange:(NSRange){0, self.textLabel.attributedText.length}
options:(NSAttributedStringEnumerationOptions)0
usingBlock:^(id value, NSRange range, BOOL *stop) {
*stop = YES;
if( [value isKindOfClass:[UIFont class]]) {
UIFont * font = (UIFont*)value;
CGSize size = [self.textLabel.attributedText.string
boundingRectWithSize:CGSizeMake(self.textLabel.bounds.size.width,
CGFLOAT_MAX)
options:(NSStringDrawingUsesLineFragmentOrigin |
NSStringDrawingUsesFontLeading)
attributes:@{NSFontAttributeName: font}
context:nil].size;
self.textHeight.constant = size.height; // USe height
}
}];
NSStringDrawingUsesLineFragmentOrigin? Why have you chosen to useNSStringDrawingUsesLineFragmentOrigin? What have you tried already?