简体   繁体   English

UILabel.attributedText在iPhone Simulator上不显示(但在iPad Simulator上可用)

[英]UILabel.attributedText doesn't show on iPhone Simulator (but works on iPad Simulator)

In my app, I have a UILabel and I set an attributedText on it like this: 在我的应用中,我有一个UILabel,并在其上设置了attributedText,如下所示:

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

When I launch my app on a real iPhone or iPad Device or in an iPad Simulator, that works great, but when I launch it on an iPhone Simulator (SE, 7 or 7 Plus), my label doesn't show my text. 当我在真实的iPhone或iPad设备上或iPad Simulator中启动我的应用程序时,效果很好,但是当我在iPhone Simulator(SE,7或7 Plus)上启动应用程序时,我的标签不显示文本。

What is really funny, when I enter into the view debugger, my text is there! 真正有趣的是,当我进入视图调试器时,我的文本在那里!

So, is that a problem in my app or is it an iPhone Simulator problem? 那么,这是我的应用程序中的问题还是iPhone模拟器问题?

You can use below method to set attributed text to UILabel. 您可以使用以下方法将属性文本设置为UILabel。 Attributed text is supported in iOS 6 and greater version of iOS only. 仅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;
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 UILabel.attributedText无法在iPhone 4 + iOS 7.0.3上显示 - UILabel.attributedText not show up on iPhone 4 + iOS 7.0.3 代码在iPhone上不起作用,但在模拟器上起作用 - Code doesn't work on iPhone but works on simulator 该应用程序未安装在iPhone上,但可在模拟器上使用 - App doesn't install on iPhone, but works on simulator iOS启动图像不会出现在iPhone或模拟器上,适用于iPad - iOS launch image won't show up on iPhone or simulator, works on iPad 具有自定义字体的UILabel在iOs 5 Simulator中不显示自定义字体 - UILabel with custom font doesn't show custom font in iOs 5 Simulator 为什么当模拟器处于横向模式时 UILabel 不显示? - Why doesn't the UILabel show when the simulator is in Landscape Mode? 在UILabel.attributedText中建立链接*不是*蓝色,*不是*带下划线 - Make link in UILabel.attributedText *not* blue and *not* underlined UILabel甚至在iPhone 5c上具有内容压缩所需的优先级时也会截断字符串。 但可以在模拟器和iPad(设备)上正常工作 - UILabel truncating string even with required priority for content compression on iPhone 5c. But works fine in the simulator and iPad(device) 随时随地在UILabel.attributedText上应用不同的属性 - Apply different attributes on UILabel.attributedText on the go UIPopovercontroller在iPad Simulator(xCode6.1)上不显示任何内容 - UIPopovercontroller doesn't show anything on iPad Simulator(xCode6.1)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM