【问题标题】:Save Text Attributed to Parse (Swift) [duplicate]保存归因于解析的文本(Swift)[重复]
【发布时间】:2015-11-10 01:59:02
【问题描述】:

我正在创建一个基本的笔记,虽然使用 Swift 进行解析。我想保存和加载文本颜色、大小和字体以及常规文本。我已经保存了“非归属”文本,但没有别的。这是我的代码:

NotesTableViewController.swift:

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = self.tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) as! NotesTableViewCell

let object: PFObject = self.notObjects.objectAtIndex(indexPath.row) as! PFObject

cell.masterCell?.text = object["title"] as? String
cell.masterTextLabel?.text = object["text"] as? String

return cell

addNoteTableController.swift:

override func viewDidLoad() {
super.viewDidLoad()

self.colorPicker.alpha = 0
self.fontPicker.alpha = 0

if (self.object != nil) {
    self.titleField?.text = self.object["title"] as? String
    self.textView?.text = self.object["text"] as? String
}else {
    self.object = PFObject(className: "Note")
    }
}

@IBAction func saveAction(sender: UIBarButtonItem) {
self.object["username"] = PFUser.currentUser()?.username
self.object["title"] = self.titleField?.text
self.object["text"] = self.textView?.text

self.object.saveEventually { (sucess, error) -> Void in
    if (error == nil) {
    }else {
        print(error?.userInfo)
    }
}

self.navigationController?.popViewControllerAnimated(true)
}

我的目标是让用户能够将效果应用于文本,保存它,当他们重新加载它时,效果仍然存在。 (请看屏幕截图。)

Screen Shot

【问题讨论】:

    标签: ios xcode swift parse-platform


    【解决方案1】:

    尝试将属性文本转换为 NSData 并将其保存为 Parse 文件。然后,您可以再次从 Parse 下载它并将其转换为属性文本:

    // Convert to NSData
    let nsdata = NSKeyedArchiver.archivedDataWithRootObject(attributedString)
    
    // Back to NSAttributedString
    let attributedString = NSKeyedUnarchiver.unarchiveObjectWithData(nsdata)
    

    【讨论】:

    • 好的...你能告诉我我将如何把它放在我的代码中
    • 你有什么想法吗?
    • 嗯,这很简单。您是否阅读过有关如何保存 PFFile 的 Parse 文档??
    • 我不知道怎么做,即使我阅读了文档。可以给我代码吗?
    猜你喜欢
    • 2018-07-12
    • 1970-01-01
    • 2020-06-19
    • 2017-05-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多