我正在创建一个使用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)
}
我的目标是让用户能够将效果应用于文本,保存,并在重新加载时,效果仍然存在。 (请看屏幕截图。)
答案 0 :(得分:1)
尝试将您的属性文本转换为NSData并将其另存为Parse上的文件。然后,您可以再次从Parse下载并将其转换为属性文本:
// Convert to NSData
let nsdata = NSKeyedArchiver.archivedDataWithRootObject(attributedString)
// Back to NSAttributedString
let attributedString = NSKeyedUnarchiver.unarchiveObjectWithData(nsdata)