繁体   English   中英

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

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

在我的应用中,我有一个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

当我在真实的iPhone或iPad设备上或iPad Simulator中启动我的应用程序时,效果很好,但是当我在iPhone Simulator(SE,7或7 Plus)上启动应用程序时,我的标签不显示文本。

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

那么,这是我的应用程序中的问题还是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;
    }
}

暂无
暂无

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM