2017-03-26 40 views
1

我越來越從服務器HTML文本,並試圖將其與該代碼的標籤文本NSAttributedString與字體

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

設置標籤,但這樣做我的標籤字體後更改爲默認字體。我怎樣才能設置我的字體標籤,而不會丟失文本的屬性部分?

+0

是否HTML字符串定義任何字體樣式? – ozgur

+0

不,它不@ozgur –

+0

做:'「 \ n \ n」.appendingString(myHTMLText)'然後轉換成'NSAttributedString'段落標籤內的任何內容都將使用指定的字體。 – Brandon

回答

0

我使用以下,它適用於段落樣式。如果您想要設置整個文字的樣式,請改爲使用body標記。您可以看到生成的NSAttributedString使用正確的字體樣式。

//: 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) 

,其結果是:

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

感謝更改字體。但現在我沒有任何粗體文本,一切都是正常的 –