我正在使用最低版本的iOS 7构建应用。在我绘制单元格的方法中,我使用此代码绘制具有不同UILabel
类型的UIFonts
。
//Get Strings
NSString* author = [self getUserCreatedVideo:notf];
NSString* caption = [NSString stringWithFormat:@"\"%@\"", [notf objectForKey:@"title"]];
NSString* challenge = @"challenged you to reply to";
NSString* timeToReply = @"24h to reply";
NSString* myString = [NSString stringWithFormat:@"%@ %@ %@ %@", author, challenge, caption, timeToReply];
NSMutableAttributedString *str = [[NSMutableAttributedString alloc] initWithString:myString];
UIColor* greenColor = [UIColor colorWithRed:102.0/255.0 green:204.0/255.0 blue:153.0/255.0 alpha:1];
//Author
[str addAttribute:NSForegroundColorAttributeName value:[UIColor darkGrayColor] range:NSMakeRange(0,author.length)];
[str addAttribute:NSFontAttributeName value: [UIFont fontWithName:@"Helvetica" size:17.0] range:NSMakeRange(0,author.length)];
//Challenge
[str addAttribute:NSForegroundColorAttributeName value:[UIColor lightGrayColor] range:NSMakeRange(author.length+1,challenge.length)];
[str addAttribute:NSFontAttributeName value: [UIFont fontWithName:@"Helvetica" size:13.0] range:NSMakeRange(author.length+1,challenge.length)];
//Caption
[str addAttribute:NSForegroundColorAttributeName value:[UIColor darkGrayColor] range:NSMakeRange(author.length+1+challenge.length+1,caption.length)];
[str addAttribute:NSFontAttributeName value: [UIFont fontWithName:@"Helvetica" size:14.0] range:NSMakeRange(author.length+1+challenge.length+1,caption.length)];
//Time To Reply
[str addAttribute:NSForegroundColorAttributeName value:greenColor range:NSMakeRange(author.length+1+challenge.length+1+caption.length+1,timeToReply.length)];
[str addAttribute:NSFontAttributeName value: [UIFont fontWithName:@"Helvetica" size:14.0] range:NSMakeRange(author.length+1+challenge.length+1+caption.length+1,4)];
cell.subtext.attributedText = str;
现在我想计算UILabel
文字大小,以便我可以在UILabel
以下cell.subtext
20px。我怎样才能做到这一点?
答案 0 :(得分:1)
所以基本上你想知道文本宽度时计算文本的高度。
CGRect rect = [str boundingRectWithSize:CGSizeMake(cellWidth, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin context:nil];
以您希望的方式使用生成的rect.size。