objective-c - UILabel attributedText 和 minimumScaleFactor

标签 objective-c cocoa-touch cocoa uilabel

是否可以同时使用 minimumScaleFactor 和属性文本字符串?

        [myLabel setFrame:CGRectMake(6, 0, 200, 25)];
    NSMutableAttributedString *attStr = [[NSMutableAttributedString alloc] initWithString:@"A very long string and its first 20 characters should be bold"];
    [attStr addAttribute:NSFontAttributeName value:[UIFont boldSystemFontOfSize:17] range:NSMakeRange(0, 20)];
    myLabel.attributedText = attStr;
    [myLabel setAdjustsFontSizeToFitWidth:YES];
    [myLabel setAdjustsLetterSpacingToFitWidth:YES];
    [myLabel setMinimumScaleFactor:0.3];

这似乎行不通。如果我设置 myLabel.text 它会按预期缩放。如何让缩放正常工作?

最佳答案

你可以取前 20 个字符的部分并获取它的宽度,然后你可以使用相同的方法获取其余部分的宽度:

CGSize maximumLabelSize = CGSizeMake(500.0,20.0);//write a really long width

//this will turn the expected length of the labels.. 
 CGSize expectedFirstLabelSize = [[[[NSMutableAttributedString alloc] initWithString:@"A very long string and its first 20 characters should be bold"] substringToIndex:21]
 sizeWithFont:[UIFont  boldSystemFontOfSize:17] constrainedToSize:maximumLabelSize lineBreakMode:nil];


 CGSize expectedLastLabelSize = [[[[NSMutableAttributedString alloc] initWithString:@"A very long string and its first 20 characters should be bold"] substringFromIndex:20] 
sizeWithFont:[UIFont  normalSystemFontOfSize:15] constrainedToSize:maximumLabelSize lineBreakMode:nil];

然后你就会知道他们的长度率.. 例如。第一部分是 140 像素,其余部分是 260 像素,标签区域是 100 像素, 然后你可以创建 2 个不同的标签,分别为 35 像素和 65 像素。 然后您可以对这两个标签使用 setAdjustsFontSizeToFitWidth。

关于objective-c - UILabel attributedText 和 minimumScaleFactor,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13433481/

相关文章:

ios - 如何将十六进制值的 NSString 转换为 Char - Objective C

objective-c - 用 MPMoviePlayer 播放电影——控件不会显示

iphone - 我真的需要在核心数据中始终存在双向关系吗?

iphone - 在 iOS 上创建自定义循环 UIScrollView

Cocoa:在应用程序启动时隐藏 TextView 的自定义滚动条

iphone - 调用函数的问题

iphone - 在同一 View Controller 中进行一次、两次和三次点击操作

objective-c - 为什么使用setLevel : won't make the window higher than a full screen game?

cocoa - 如何判断哪个 NSTextField 被编辑了?

iphone - 这是内存泄漏吗?