How do you use NSAttributedString?

后端 未结 15 1109
佛祖请我去吃肉
佛祖请我去吃肉 2020-11-22 04:19

Multiple colours in an NSString or NSMutableStrings are not possible. So I\'ve heard a little about the NSAttributedString which was introduced wit

15条回答
  •  孤城傲影
    2020-11-22 04:52

    You can load an HTML attributed string in Swift as follow

       var Str = NSAttributedString(
       data: htmlstring.dataUsingEncoding(NSUnicodeStringEncoding, allowLossyConversion: true),
       options: [ NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType],
       documentAttributes: nil,
       error: nil)
    
       label.attributedText = Str  
    

    To load a html from file

       if let rtf = NSBundle.mainBundle().URLForResource("rtfdoc", withExtension: "rtf", subdirectory: nil, localization: nil) {
    
       let attributedString = NSAttributedString(fileURL: rtf, options: [NSDocumentTypeDocumentAttribute:NSRTFTextDocumentType], documentAttributes: nil, error: nil)
            textView.attributedText = attributedString
            textView.editable = false
        }
    

    http://sketchytech.blogspot.in/2013/11/creating-nsattributedstring-from-html.html

    And setup string as per your required attribute....follow this..
    http://makeapppie.com/2014/10/20/swift-swift-using-attributed-strings-in-swift/

提交回复
热议问题