SwiftノートlabelはリッチテキストまたはHTMLを表示します

2216 ワード

Labelにカスタムカラーまたはサイズを表示する方法は次のとおりです.

ツールバーの

        // 
        var attributeString = NSMutableAttributedString(string:"welcome to hangge.com")
        // 0 6 HelveticaNeue-Bold,16 
        attributeString.addAttribute(NSFontAttributeName, value: UIFont(name: "HelveticaNeue-Bold", size: 16)!,
            range: NSMakeRange(0,6))
        // 
        attributeString.addAttribute(NSForegroundColorAttributeName, value: UIColor.blueColor(),
            range: NSMakeRange(0, 3))
        // 
        attributeString.addAttribute(NSBackgroundColorAttributeName, value: UIColor.greenColor(),
            range: NSMakeRange(3,3))

msgLabel.attributedText = attributeString

htmlを表示

       var htmlText = " は
An apple is red."
do{ let attrStr = try NSAttributedString(data: htmlText.dataUsingEncoding(NSUnicodeStringEncoding, allowLossyConversion: true)!, options: [NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType], documentAttributes: nil) msgLabel.attributedText = attrStr }catch let error as NSError { print(error.localizedDescription) }