I have a label added to a cell which has a dynamic height based on the text to be added to it. I have set my font size to be 12 as seen below:
CGFloat height = [CustomCell getIndividualLabelHeight:text];
NSLog(@"height of commet:%@ is %f",commentText, height);
CustomOHAttributLabel *label = [[CustomOHAttributLabel alloc]initWithFrame:CGRectMake(CELL_TEXT_LEFT_MARGIN, 2*CELL_SPACING+totalCommentLabelHeight, CELL_CONTENT_WIDTH - (CELL_TEXT_LEFT_MARGIN*2), height)];
[label setLabelwithText:text fontSize:12 andSubString:userName withURL:url];
However, in my getIndividualLabelHeight method, if I set the font to 12.0 as well (in setting of CGSize size), the text in the label might be truncated. It is only when I set it to 14 will the text not be truncated.
+ (CGFloat)getIndividualLabelHeight:(NSString *)text
{
CGSize constraint = CGSizeMake(CELL_CONTENT_WIDTH - (CELL_CONTENT_MARGIN * 2), 20000.0f);
//The full text will only show when I set fontsize to 14 (instead of 12)
CGSize size = [text sizeWithFont:[UIFont systemFontOfSize:14] constrainedToSize:constraint lineBreakMode:UILineBreakModeWordWrap];
return size.height;
}
Anyone has any idea why I cannot set the same font size in my get height method as the actual font size I am using for my text?
I have added my implementation code for the CustomOHAttributLabel for further reference
@implementation CustomOHAttributLabel
- (CustomOHAttributLabel*) initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self)
{
}
return self;
}
- (void) setLabelwithText:(NSString *)text fontSize:(CGFloat)fontSize andSubString:(NSString *)subString withURL:(NSString *)url
{
NSMutableAttributedString* attrStr = [NSMutableAttributedString attributedStringWithString:text];
[attrStr setFont:[UIFont systemFontOfSize:fontSize]];
[attrStr setTextColor:[UIColor grayColor]];
[attrStr setFont:[UIFont boldSystemFontOfSize:fontSize] range:[text rangeOfString:subString]];
[attrStr setTextColor:[UIColor darkGrayColor] range:[text rangeOfString:subString]];
self.attributedText = attrStr;
[self addCustomLink:[NSURL URLWithString:url] inRange:[text rangeOfString:subString]];
}
@end