UILabel.attributedText 不显示在 iPhone 模拟器上(但适用于 iPad 模拟器)
Posted
技术标签:
【中文标题】UILabel.attributedText 不显示在 iPhone 模拟器上(但适用于 iPad 模拟器)【英文标题】:UILabel.attributedText doesn't show on iPhone Simulator (but works on iPad Simulator) 【发布时间】:2017-07-21 08:22:49 【问题描述】:在我的应用程序中,我有一个 UILabel,我在它上面设置了一个属性文本,如下所示:
let htmlString = try? NSMutableAttributedString(data: data, options: [NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType, NSCharacterEncodingDocumentAttribute: String.Encoding.utf8.rawValue], documentAttributes: nil)
let characterCount = htmlString?.string.characters.count ?? 0
htmlString?.addAttributes([NSParagraphStyleAttributeName:paragraphStyle,NSBaselineOffsetAttributeName:0], range: NSMakeRange(0, characterCount))
self.chapterTextLabel.attributedText = htmlString
当我在真正的 iPhone 或 iPad 设备或 iPad 模拟器上启动我的应用程序时,效果很好,但是当我在 iPhone 模拟器(SE、7 或 7 Plus)上启动它时,我的标签不显示我的文本。
真正有趣的是,当我进入视图调试器时,我的文本就在那里!
那么,这是我的应用程序的问题还是 iPhone 模拟器的问题?
【问题讨论】:
ios 7 Simulator Bug - NSAttributedString does not appear的可能重复 我在 iOS 10 模拟器上。而且这个问题只在 iPhone 模拟器(iOS 10)上,在 iPad 模拟器(iOS 10 也是)上效果很好 究竟是什么版本? 10.3 ?检查这个:***.com/questions/43074652/… iOS 10.3.1 我已经尝试了您帖子中的所有解决方案,但不适用于我的情况。我不明白为什么它只在 iPhone 模拟器上这样做 【参考方案1】:您可以使用以下方法将属性文本设置为 UILabel。 属性文本仅在 iOS 6 及更高版本的 iOS 中受支持。
+(void)attributedString:(NSString*)AllStr FirstStr:(NSString*)Fstr FirstStrFont:(UIFont*)FFont FirstStrColor:(UIColor*)FColor SecondStr:(NSString*)Sstr SecondStrFont:(UIFont*)SFont SecondStrColor:(UIColor*)SColor ThirdStr:(NSString*)Tstr ThirdStrFont:(UIFont*)TFont ThirdStrColor:(UIColor*)TColor FourthStr:(NSString*)Fostr FourthStrFont:(UIFont*)FoFont FourthStrColor:(UIColor*)FoColor ForLable:(UILabel*)Lable
NSString *redText = Fstr;
NSString *greenText = Sstr;
NSString *purpleBoldText = Tstr;
NSString *GrayText = Fostr;
NSString *text = AllStr;
// If attributed text is supported (iOS6+)
if ([Lable respondsToSelector:@selector(setAttributedText:)])
// Define general attributes for the entire text
NSDictionary *attribs = @
NSForegroundColorAttributeName: Lable.textColor,
NSFontAttributeName: Lable.font
;
NSMutableAttributedString *attributedText =
[[NSMutableAttributedString alloc] initWithString:text
attributes:attribs];
// First text attributes
NSRange redTextRange = [text rangeOfString:redText];// * Notice that usage of rangeOfString in this case may cause some bugs - I use it here only for demonstration
[attributedText setAttributes:@NSForegroundColorAttributeName:FColor,NSFontAttributeName:FFont
range:redTextRange];
// Second text attributes
NSRange greenTextRange = [text rangeOfString:greenText];// * Notice that usage of rangeOfString in this case may cause some bugs - I use it here only for demonstration
[attributedText setAttributes:@NSForegroundColorAttributeName:SColor,NSFontAttributeName:SFont
range:greenTextRange];
//Third and bold text attributes
NSRange purpleBoldTextRange = [text rangeOfString:purpleBoldText];// * Notice that usage of rangeOfString in this case may cause some bugs - I use it here only for demonstration
[attributedText setAttributes:@NSForegroundColorAttributeName:TColor,
NSFontAttributeName:TFont
range:purpleBoldTextRange];
//Third and bold text attributes
NSRange GrayTextRange = [text rangeOfString:GrayText];// * Notice that usage of rangeOfString in this case may cause some bugs - I use it here only for demonstration
[attributedText setAttributes:@NSForegroundColorAttributeName:FoColor,
NSFontAttributeName:FoFont
range:GrayTextRange];
Lable.attributedText = attributedText;
// If attributed text is NOT supported (iOS5-)
else
Lable.text = text;
【讨论】:
对我不起作用。我认为这是一个模拟器错误。因为它只在模拟器上。以上是关于UILabel.attributedText 不显示在 iPhone 模拟器上(但适用于 iPad 模拟器)的主要内容,如果未能解决你的问题,请参考以下文章