gpt4 book ai didi

ios - UILabel.attributedText 不显示在 iPhone 模拟器上(但适用于 iPad 模拟器)

转载 作者:行者123 更新时间:2023-11-30 12:17:21 25 4
gpt4 key购买 nike

在我的应用程序中,我有一个 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)上启动它时,我的标签不显示我的文本。

真正有趣的是,当我进入 View 调试器时,我的文本就在那里!

那么,这是我的应用程序的问题还是 iPhone 模拟器的问题?

最佳答案

您可以使用以下方法将属性文本设置为 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;
}
}

关于ios - UILabel.attributedText 不显示在 iPhone 模拟器上(但适用于 iPad 模拟器),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45232657/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com