在我的应用程序中,我有一个UILabel,我在上面设置了一个attributesText:
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模拟器问题?
答案 0 :(得分:0)
您可以使用以下方法将属性文本设置为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;
}
}