简体   繁体   English

将NSMutableAttributedString的所有属性放入字典

[英]Get all attributes of a NSMutableAttributedString into a dictionary

I have a NSMutableAttributedString which contains different attributes for different ranges. 我有一个NSMutableAttributedString,其中包含不同范围的不同属性。 I want to read all Ranges with attributes in a dictionary so that I can save all attributes into Core Data and MySql via Api and then again reconstruct the attributed string. 我想在字典中读取具有属性的所有范围,以便可以通过Api将所有属性保存到Core Data和MySql中,然后再次重建属性字符串。

Here is my attributed string 这是我的属性字符串

    let myString = "P is for Pizza and Pizza is for me"

    //: Initialize the mutable string
    let myMutableString = NSMutableAttributedString(string: myString,attributes: [NSFontAttributeName:UIFont(name: "Georgia", size: 18.0)!])

    //: Make the first Pizza in chalkduster 24 point
    myMutableString.addAttribute(NSFontAttributeName,value: UIFont(name: "Chalkduster",size: 24.0)!,range: NSRange(location: 9,length: 5))


    //: Make the second pizza red and outlined in Helvetica Neue
    myMutableString.addAttribute(NSFontAttributeName,value: UIFont(name: "Helvetica Neue",size: 36.0)!,range: NSRange(location: 19,length: 5))

    myMutableString.addAttribute(NSStrokeColorAttributeName,value: UIColor.red,range:  NSRange(location: 19,length: 5))

    myMutableString.addAttribute(NSStrokeWidthAttributeName,value: 4,range: NSRange(location: 19,length: 5))

    //: Set the background color is attributes text.
    //: which is not the color of the background text.
    let  stringLength = myString.characters.count
    myMutableString.addAttribute(NSBackgroundColorAttributeName,value: UIColor.magenta,range: NSRange(location: 0,length: stringLength))


    //:Change to 48 point Menlo
    myMutableString.addAttribute(NSFontAttributeName,value: UIFont(name: "Menlo",size: 48.0)!,range: NSRange(location: 27,length: 7))

myMutableString输出

on executing the following code 在执行以下代码

 let attributes = attributedText.attributes(at: 0, longestEffectiveRange: nil, in: NSRange(location: 0, length: attributedText.length))

 print(attributes)

getting the following output 得到以下输出

 ["NSColor": UIExtendedSRGBColorSpace 0 0 1 1, "NSBackgroundColor": UIExtendedSRGBColorSpace 1 0 1 1, "NSFont": <UICTFont: 0x7fff12000110> font-family: "American Typewriter"; font-weight: bold; font-style: normal; font-size: 36.00pt]

I am getting the following string when I am printing myMutableString.description 打印myMutableString.description时出现以下字符串

 print(myMutableString.description)

 P is for {
     NSBackgroundColor = "UIExtendedSRGBColorSpace 1 0 1 1";
     NSFont = "<UICTFont: 0x7fd90cc36e20> font-family: \"Georgia\"; font-weight: normal; font-style: normal; font-size: 18.00pt";
 }Pizza{
     NSBackgroundColor = "UIExtendedSRGBColorSpace 1 0 1 1";
     NSFont = "<UICTFont: 0x7fd90cd32a80> font-family: \"Chalkduster\"; font-weight: normal; font-style: normal; font-size: 24.00pt";
 } and {
     NSBackgroundColor = "UIExtendedSRGBColorSpace 1 0 1 1";
     NSFont = "<UICTFont: 0x7fd90cc36e20> font-family: \"Georgia\"; font-weight: normal; font-style: normal; font-size: 18.00pt";
 }Pizza{
     NSBackgroundColor = "UIExtendedSRGBColorSpace 1 0 1 1";
     NSFont = "<UICTFont: 0x7fd90cd32d90> font-family: \"Helvetica Neue\"; font-weight: normal; font-style: normal; font-size: 36.00pt";
     NSStrokeColor = "UIExtendedSRGBColorSpace 1 0 0 1";
     NSStrokeWidth = 4;
 } is{
     NSBackgroundColor = "UIExtendedSRGBColorSpace 1 0 1 1";
     NSFont = "<UICTFont: 0x7fd90cc36e20> font-family: \"Georgia\"; font-weight: normal; font-style: normal; font-size: 18.00pt";
 } for me{
     NSBackgroundColor = "UIExtendedSRGBColorSpace 1 0 1 1";
     NSFont = "<UICTFont: 0x7fd90f8406d0> font-family: \"Menlo-Regular\"; font-weight: normal; font-style: normal; font-size: 48.00pt";
 }

Convert to html 转换成html

var s: NSAttributedString? = "Pizza stirng"
var documentAttributes: [AnyHashable: Any] = [NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType]
var htmlData: Data? = try? s?.data(from: NSRange(location: 0, length: s?.length), documentAttributes: documentAttributes as? [String : Any] ?? [String : Any]())
var htmlString = String(data: htmlData, encoding: String.Encoding.utf8)

and try this to get back NSAttributedString 并尝试以此取回NSAttributedString

 var atributedStr = try? NSAttributedString(data: htmlString.data(using: String.Encoding.utf8), options: [NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType, NSCharacterEncodingDocumentAttribute: (String.Encoding.utf8)], documentAttributes: nil)

Swift 3.0 Extension Swift 3.0扩展

 extension NSAttributedString {

     convenience init(htmlString:String){   
         do {       
             try self.init(data: htmlString.data(using: .utf8)!, options: [NSDocumentTypeDocumentAttribute:NSHTMLTextDocumentType], documentAttributes: nil)       
         } catch {       
             print(error.localizedDescription)       
         }   
     }

     func toHtml(location:Int,length:Int) -> String {   
         let documentAttributes = [NSDocumentTypeDocumentAttribute:NSHTMLTextDocumentType]   
         let range = NSRange.init(location: location, length: length)   
         do {       
             let htmlData = try self.data(from: range, documentAttributes: documentAttributes)       
             let htmlString = String(data: htmlData, encoding: .utf8)       
             return htmlString!       
         } catch {       
             return error.localizedDescription       
         }   
     }
 }

Usage: 用法:

 let myString = "P is for Pizza and Pizza is for me"

 //: Initialize the mutable string
 let myMutableString = NSMutableAttributedString(string: myString,attributes: [NSFontAttributeName:UIFont(name: "Georgia", size: 18.0)!])

 //: Make the first Pizza in chalkduster 24 point
 myMutableString.addAttribute(NSFontAttributeName,value: UIFont(name: "Chalkduster",size: 24.0)!,range: NSRange(location: 9,length: 5))


 //: Make the second pizza red and outlined in Helvetica Neue
 myMutableString.addAttribute(NSFontAttributeName,value: UIFont(name: "Helvetica Neue",size: 36.0)!,range: NSRange(location: 19,length: 5))

 //: Create Html String from NSMutableAttributedString
 let htmlString = myMutableString.toHtml(location: 0, length: myMutableString.string.length);

 print(htmlString)

 //: Create NSMutableAttributedString from HTML String
 let newAttributedString = NSMutableAttributedString(htmlString: htmlString)

 print(newAttributedString)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 iOS - 将所有NSMutableAttributedString属性复制到另一个NSMutableAttributedString - iOS - copy all NSMutableAttributedString attributes to another NSMutableAttributedString 是否可以获得 NSMutableAttributedString 的属性和范围列表? - Is it possible to get a listing of attributes and ranges for an NSMutableAttributedString? 逐个字符地向 NSMutableAttributedString 添加属性 - Add attributes to NSMutableAttributedString character by character 更改NSMutableAttributedString的多个部分的属性 - Change attributes of multiple parts of a NSMutableAttributedString NSMutableAttributedString上的临时属性在编辑字符串时被删除 - Temporary attributes on NSMutableAttributedString that are removed when string is edited 如何清除 NSMutableAttributedString 中的属性? - How do you clear attributes from NSMutableAttributedString? NSMutableAttributedString 添加多个隐藏文本的属性 - NSMutableAttributedString adding multiple attributes hiding the text 重置NSMutableAttributedString字符串是不是应用了它的属性? - Resetting NSMutableAttributedString string is not applying its attributes? Swift-NSMutableAttributedString更改UITextView中的所有文本 - Swift - NSMutableAttributedString changes all text in UITextView 范围的NSMutableAttributedString addAttribute适用于所有字符串 - NSMutableAttributedString addAttribute for range apply for all string
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM