I have face problem when set different type of font and color in one UILable using Attribute.My problem is how to get exact height of lable so i have provided that height to cell height and maintain my UI and text not cutting.
I have try out following things: 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;
When i set frame to UILabel at that time i have used below code
================================================================
int heightlbl = [AppSingletonObj get_LblHeight:strJoinTime withWidth:lblnotifyDesWidthAnswerQues withFont:_SETREGULARFONT(14.0)];
=======================================================
if i will calculated different height and set that height to label then height is greater and text is not fit to vertically center.
========================================================
- (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]);
}
}
=====================================================
给您的标签文本,然后计算框架并将该框架分配给标签。
CGRect labelSize = [label.text boundingRectWithSize:CGSizeMake(label.frame.size.width, self.view.frame.size.height) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:[UIFont fontWithName:@"font name" size:size]} context:nil];
Try this solution :
It's on swift but i think you can convert it into objective-c
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.