我有一个面对的问题,当设置不同类型的字体和颜色在一个UILable使用属性。我的问题是如何获得标签的确切高度,所以我已经提供了高度单元格高度和维持我的用户界面和文本不切割。如何在UILabel中设置不同类型的NSFontAttributeName来计算UILabel的高度?
我有尝试以下操作: CODE:
int italicHeight = [AppSingletonObj get_LblHeight:strJoin withWidth:270 withFont:_SETITALICFONT(14.0)];
int normalTextHeight = [AppSingletonObj get_LblHeight:strCmt withWidth:270 withFont:_SETREGULARFONT(14.0)];
int cellheight = italicHeight+normalTextHeight;
当我在那个时候设置帧的UILabel我用下面的代码
=========== ================================================== ===
int heightlbl = [AppSingletonObj get_LblHeight:strJoinTime withWidth:lblnotifyDesWidthAnswerQues withFont:_SETREGULARFONT(14.0)];
======================================== ===============
如果我将计算不同的高度并将该高度设置为标签,那么高度更大,文本不适合垂直居中。
============================================== ==========
- (CGFloat)get_LblHeight:(NSString*)str withWidth:(CGFloat)width withFont:(UIFont *)uiFont {
@try
{
// Get text
CFMutableAttributedStringRef attrString = CFAttributedStringCreateMutable(kCFAllocatorDefault, 0);
CFAttributedStringReplaceString (attrString, CFRangeMake(0, 0), (CFStringRef) str);
CFIndex stringLength = CFStringGetLength((CFStringRef) attrString);
// Change font
CTFontRef ctFont = CTFontCreateWithName((__bridge CFStringRef) uiFont.fontName, uiFont.pointSize, NULL);
CFAttributedStringSetAttribute(attrString, CFRangeMake(0, stringLength), kCTFontAttributeName, ctFont);
// Calc the size
CTFramesetterRef framesetter = CTFramesetterCreateWithAttributedString(attrString);
CFRange fitRange;
CGSize frameSize = CTFramesetterSuggestFrameSizeWithConstraints(framesetter, CFRangeMake(0, 0), NULL, CGSizeMake(width, CGFLOAT_MAX), &fitRange);
CFRelease(ctFont);
CFRelease(framesetter);
CFRelease(attrString);
return frameSize.height +05;
}
@catch (NSException *exception)
{
NSLog(@"Exception heightEmoji = %@",[exception description]);
}
}
================================= ====================