0

In my app, I have a UILabel and I set an attributedText on it like this:

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.

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?

Tamás Sengel
  • 55,884
  • 29
  • 169
  • 223
Jordan Favray
  • 179
  • 1
  • 12
  • Possible duplicate of [iOS 7 Simulator Bug - NSAttributedString does not appear](https://stackoverflow.com/questions/18994479/ios-7-simulator-bug-nsattributedstring-does-not-appear) – Himanth Jul 21 '17 at 08:26
  • I'm on iOS 10 Simulator. And this problem is only on iPhone Simulator (iOS 10), works great on iPad Simulator (iOS 10 too) – Jordan Favray Jul 21 '17 at 08:30
  • What version exactly? 10.3 ? Check this: https://stackoverflow.com/questions/43074652/ios-10-3-nsstrikethroughstyleattributename-is-not-rendered-if-applied-to-a-sub – Larme Jul 21 '17 at 08:46
  • iOS 10.3.1 I have trying all solution in your post, not working in my case. I don't understand why it's only do that on iPhone Simulator – Jordan Favray Jul 21 '17 at 09:56

1 Answers1

0

You can use below method to set attributed text to UILabel. Attributed text is supported in iOS 6 and greater version of iOS only.

+(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;
    }
}