2017-07-21 6 views
0

In meiner Anwendung habe ich eine UILabel und ich setzte einen attributedText auf es wie folgt aus:UILabel.attributedText zeigt nicht auf dem iPhone Simulator (wohl aber auf dem iPad Simulator)

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 

Wenn ich meine app starten Auf einem echten iPhone oder iPad Gerät oder in einem iPad Simulator funktioniert das großartig, aber wenn ich es auf einem iPhone Simulator (SE, 7 oder 7 Plus) starte, zeigt mein Etikett meinen Text nicht an.

Was ist wirklich lustig, wenn ich in den View-Debugger gehe, ist mein Text da!

Also, ist das ein Problem in meiner App oder ist es ein iPhone Simulator Problem?

+0

Mögliche Duplikat [iOS 7 Simulator Bug - NSAttributedString nicht erscheint] (https://stackoverflow.com/questions/18994479/ios-7-simulator-bug-nsattributedstring-does-not-appear) – Himanth

+0

Ich bin auf iOS 10 Simulator. Und dieses Problem ist nur auf iPhone Simulator (iOS 10), funktioniert gut auf iPad Simulator (iOS 10 zu) –

+0

Welche Version genau? 10.3? Überprüfen Sie dies: https://stackoverflow.com/questions/43074652/ios-10-3-nsstrikethroughstyleattributename-is-not-rended-if-applied-to-a-sub – Larme

Antwort

0

Sie können die folgende Methode verwenden, um attributierten Text auf UILabel zu setzen. Zugeschriebener Text wird nur in iOS 6 und einer höheren Version von iOS unterstützt.

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

nicht für mich arbeiten. Ich denke, es ist ein Simulator-Bug. Weil es nur am Simulator ist. –

Verwandte Themen