【问题标题】:How to use store and use an NSMutableAttributedString in NSUserDefaults如何在 NSUserDefaults 中使用存储和使用 NSMutableAttributedString
【发布时间】:2016-04-29 13:56:49
【问题描述】:

我想创建一个属性字符串,然后将它存储在NSUserDefaults,然后再次访问它并将属性字符串分配给textView.attributedText。我该怎么做?提前致谢。

我对objective c了解不多,所以无法参考this answer

【问题讨论】:

  • 您只需要保存字符串并重新添加效果,您可以从 attString.string 获取字符串
  • 我想你可以使用 NSKeyedArchiver 来存储属性字符串。

标签: ios string swift nsuserdefaults nsattributedstring


【解决方案1】:

您必须将NSMutableAttributedString 转换为NSData,然后将其存储在NSUserDefaults 中。

    // Convert into NSData
    let data = NSKeyedArchiver.archivedDataWithRootObject(distanceMutableAttributedString)
    NSUserDefaults.standardUserDefaults().setObject(data, forKey: "yourStringIntoData")

    // Convert your NSData to NSMutableAttributedString
    let yourStringInData = NSUserDefaults.standardUserDefaults().objectForKey("yourStringIntoData") as? NSData
    let newStr = NSKeyedUnarchiver.unarchiveObjectWithData(yourStringInData!) as? NSMutableAttributedString

   // Assign it to your textView
    textView.attributedText = newStr

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-05-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多