2017-03-26 3 views
1

Je reçois un texte html à partir du serveur et d'essayer de le mettre à une étiquette avec ce codeensemble NSAttributedString texte d'étiquette de HTML avec la police

let about = try! NSAttributedString(
        data: (myHTMLText as! String).data(using: String.Encoding.unicode, allowLossyConversion: true)!, 
        options: [NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType], 
        documentAttributes: nil) 
self.aboutLabel.attributedText = about 

mais après avoir fait cela ma police d'étiquettes est en train de changer de police par défaut . Comment puis-je définir ma police sur l'étiquette sans perdre les parties du texte attribuées?

+0

Est-ce que la chaîne HTML définissent un style de police? – ozgur

+0

non ce n'est pas @ozgur –

+0

Do: '" \ n \ n ".appendingString (myHTMLText)' puis la convertir en 'NSAttributedString' Tout ce qui est dans le Paragraphe-Tag utilisera la police spécifiée. – Brandon

Répondre

0

J'utilise ce qui suit et cela fonctionne très bien pour styliser un paragraphe. Si vous souhaitez styliser le texte entier, utilisez plutôt la balise body. Vous pouvez voir que le résultat NSAttributedString est stylé avec la police correcte.

//: Playground - noun: a place where people can play 

import UIKit 

extension UIFont { 
    class func printFontNames() { 
     for family in UIFont.familyNames { 
      let fonts = UIFont.fontNames(forFamilyName: family) 

      print("Family: ", family, "Font Names: ", fonts) 
     } 
    } 
} 

UIFont.printFontNames() 


let paragraphFont = "AvenirNextCondensed-Medium" 
let paragraphSize = 12.0 
let defaultFont = "Helvetica-Bold" 
let defaultSize = 15.0 

let htmlStyle = "<style>p {font-family:\(paragraphFont); font-size:\(paragraphSize)px;} body {font-family:\(defaultFont); font-size:\(defaultSize)px;}}</style>" 

let htmlText = "<p>Some Paragraph..</p> Some other text" 
let htmlString = "\(htmlStyle)\n\n\(htmlText)" 

let html = try! NSAttributedString(data: htmlString.data(using: .utf8)!, options: [NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType], documentAttributes: nil) 

print(html) 

Et le résultat est:

Family: Avenir Next Condensed 
Font Names: ["AvenirNextCondensed-BoldItalic", "AvenirNextCondensed-Heavy", "AvenirNextCondensed-Medium", "AvenirNextCondensed-Regular", "AvenirNextCondensed-HeavyItalic", "AvenirNextCondensed-MediumItalic", "AvenirNextCondensed-Italic", "AvenirNextCondensed-UltraLightItalic", "AvenirNextCondensed-UltraLight", "AvenirNextCondensed-DemiBold", "AvenirNextCondensed-Bold", "AvenirNextCondensed-DemiBoldItalic"] 

Some Paragraph.. 
{ 
    NSColor = "kCGColorSpaceModelRGB 0 0 0 1 "; 
    NSFont = "<UICTFont: 0x7fbd5c706c20> font-family: \"AvenirNextCondensed-Medium\"; font-weight: normal; font-style: normal; font-size: 12.00pt"; 
    NSKern = 0; 
    NSParagraphStyle = "Alignment 4, LineSpacing 0, ParagraphSpacing 12, ParagraphSpacingBefore 0, HeadIndent 0, TailIndent 0, FirstLineHeadIndent 0, LineHeight 17/0, LineHeightMultiple 0, LineBreakMode 0, Tabs (\n), DefaultTabInterval 36, Blocks (\n), Lists (\n), BaseWritingDirection 0, HyphenationFactor 0, TighteningForTruncation NO, HeaderLevel 0"; 
    NSStrokeColor = "kCGColorSpaceModelRGB 0 0 0 1 "; 
    NSStrokeWidth = 0; 
}Some other text{ 
    NSColor = "kCGColorSpaceModelRGB 0 0 0 1 "; 
    NSFont = "<UICTFont: 0x7fbd5f203bf0> font-family: \"Helvetica\"; font-weight: bold; font-style: normal; font-size: 15.00pt"; 
    NSKern = 0; 
    NSParagraphStyle = "Alignment 4, LineSpacing 0, ParagraphSpacing 0, ParagraphSpacingBefore 0, HeadIndent 0, TailIndent 0, FirstLineHeadIndent 0, LineHeight 19/0, LineHeightMultiple 0, LineBreakMode 0, Tabs (\n), DefaultTabInterval 36, Blocks (\n), Lists (\n), BaseWritingDirection 0, HyphenationFactor 0, TighteningForTruncation NO, HeaderLevel 0"; 
    NSStrokeColor = "kCGColorSpaceModelRGB 0 0 0 1 "; 
    NSStrokeWidth = 0; 
} 
+0

merci qui a changé de police. mais maintenant je n'ai pas de texte en gras et tout est régulier –