我有UILabel
,它包含两个由新行分隔的属性字符串。 第一个字符串的字体大小设置为17,第二个字符的大小设置为14. 我希望我的第一个NSMutableAttributedString
的大小调整为最小字体大小,如果其内容不能放在一行中。iOS - 使用Attributed文本自动缩小UILabel
这可能吗?
通过在IB中为纯文本设置“自动缩小为最小字体大小”来配置此类UILabel
行为非常简单,但不知道如何处理属性文本。
这里是我的代码:
NSString *eventName = @"Looong Event Name";
NSString *placeString = @"My place";
eventName = [eventName stringByAppendingString:@"\n"];
NSMutableAttributedString *attrName = [[NSMutableAttributedString alloc] initWithString:eventName];
[attrName addAttribute:NSFontAttributeName value:[UIFont boldSystemFontOfSize:17] range:NSMakeRange(0, [eventName length])];
NSMutableAttributedString *attrPlace = [[NSMutableAttributedString alloc] initWithString:placeString];
[attrPlace addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:14] range:NSMakeRange(0, placeString.length)];
[attrPlace addAttribute:NSForegroundColorAttributeName value:[UIColor grayColor] range:NSMakeRange(0, placeString.length)];
NSMutableAttributedString *finalString = [[NSMutableAttributedString alloc] initWithAttributedString:attrName];
[finalString appendAttributedString:attrPlace];
UILabel *nameLabel = (UILabel *)[cell viewWithTag:100];
nameLabel.attributedText = finalString;
当我尝试这样做时,这变成了相当头疼的事情。我不会将此作为答案发布,因为可能有更好的解决方案,我无法找到。但是,我最终以编程方式配置了我的字符串的所有属性(除了那些与大小有关的属性),并将标签设置为IBOutlet,并在属性检查器的“Autoshrink”下拉列表中配置“Minimum Font Size”。 – 2013-02-10 14:22:32