gpt4 book ai didi

ios - 计算不同字体类型的 UILabel 的大小

转载 作者:行者123 更新时间:2023-11-29 02:26:40 25 4
gpt4 key购买 nike

我正在使用最低版本的 iOS 7 构建一个应用程序。在我绘制单元格的方法中,我有这段代码来绘制具有不同 UIFontsUILabel类型。

//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文本大小,以便我可以在cell.subtext下方绘制一个UILabel 20px。我怎样才能做到这一点?

最佳答案

所以基本上,当您知道文本宽度时,您需要计算文本的高度。

CGRect rect = [str boundingRectWithSize:CGSizeMake(cellWidth, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin context:nil];

按照您想要的方式使用生成的 rect.size。

关于ios - 计算不同字体类型的 UILabel 的大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27443580/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com