iphone - iOS - 自动缩小带有属性文本的 UILabel

标签 iphone ios xcode cocoa-touch nsattributedstring

我有 UILabel,它包含两个由新行分隔的属性字符串。 第一个字符串的字体大小设置为 17,第二个字符串设置为 14。 如果我的第一个 NSMutableAttributedString 的内容无法放在一行中,我希望将其调整为最小字体大小。

这可能吗?

通过在 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;

最佳答案

我想这是您的 earlier question 的后续内容.

我不认为你可以自动执行此操作,但有一个 size NSAttributedString 的方法,您可以使用它来检查您的字符串是否太大,并在需要时自行调整。

关于iphone - iOS - 自动缩小带有属性文本的 UILabel,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14798500/

相关文章:

iphone - 无法向苹果提交应用内购买产品

iphone - UISearchBar 代表不响应取消按钮

ios - UIActivityViewController 在 Testflight 中崩溃,但在从 Xcode 构建时不会崩溃

php - JSON 请求发送空数据(swift)

ios - 当我在 SwiftUI 中按下导航后退按钮时,可以添加一些操作吗?

iphone - NSDictionary 分成两个数组(对象和键),然后按对象数组(或类似的解决方案)排序

ios - 删除和添加相同插件后的 Cordova 构建错误

iOS7 : background task ("myapp" has active assertions beyond permitted time)

ios - SVProgressHUD 在显示另一条消息后不显示

iphone - 在 Objective-C 中生成具有随机数长度的随机数