如何将 NSAttributedString 保存到 CoreData
How to save NSAttributedString to CoreData
我想保存我的 UItextview 文本的 attributedString。
我试图转换为字符串并保存。但是当我返回设置 text'attributedString 时,它不起作用。
这是我转换为字符串的方式:
var a = String(stringInterpolationSegment: text.attributedText)
Data.setValue(a, forKey: "font")
Data.managedObjectContext?.save(nil)
我是这样设置的:
text.attributedText = NSAttributedString(string: size)
但我的 TextView 只显示 AttributedSting
{
NSFont = "<UICTFont: 0x7fa9fa82aec0> font-family: \"Helvetica Neue\"; font-weight: normal; font-style: normal; font-size: 18.00pt";
NSParagraphStyle = "Alignment 0, LineSpacing 0, ParagraphSpacing 0, ParagraphSpacingBefore 0, HeadIndent 0, TailIndent 0, FirstLineHeadIndent 0, LineHeight 0/0, LineHeightMultiple 0, LineBreakMode 0, Tabs (\n 28L,\n 56L,\n 84L,\n 112L,\n 140L,\n 168L,\n 196L,\n 224L,\n 252L,\n 280L,\n 308L,\n 336L\n), DefaultTabInterval 0, Blocks (null), Lists (null), BaseWritingDirection -1, HyphenationFactor 0, TighteningFactor 0, HeaderLevel 0";
}
很抱歉我无法 post 图片。我使用 swift 来执行此操作。
我的问题:我该怎么做?或者有另一种方法来保存属性文本?
是的,您可以通过简单的解决方法添加 attributedText。
添加 Transformable
类型的属性。创建 .h 文件后,将该属性的类型更改为 NSAttributedString
。在该属性中,您可以轻松保存属性字符串。
希望这对您有所帮助..:)
该属性在数据模型中应如下所示。
应修改头文件以匹配此文件:
@property (nonatomic, retain) NSAttributedString * attributedText;
就是这样。您应该能够像任何其他属性一样保留您的属性字符串。
假设您的实体是事件,并且您有一个事件类型的对象事件,您可以通过 event.attributedText 访问它。这里有一些示例 Swift 代码:
event.attributedText = NSAttributedString(string: "Hello World")
let attributedString = event.attributedText
如果您更喜欢用您的母语回答,请告诉我们。
在 Xcode 10 中使用自动代码生成,这比其他建议简单得多。
- 打开数据模型和select属性的名称
- 打开数据模型检查器(Command+Option+3)
- 将
Attribute Type
设置为Transformable
- 将
Custom Class
设置为NSAttributedString
就是这样,现在您可以按照预期将数据保存在 Swift 代码中,无需修改生成的 类 或强制转换,例如:
detailItem.content = textView.attributedText
我想保存我的 UItextview 文本的 attributedString。 我试图转换为字符串并保存。但是当我返回设置 text'attributedString 时,它不起作用。
这是我转换为字符串的方式:
var a = String(stringInterpolationSegment: text.attributedText)
Data.setValue(a, forKey: "font")
Data.managedObjectContext?.save(nil)
我是这样设置的:
text.attributedText = NSAttributedString(string: size)
但我的 TextView 只显示 AttributedSting
{
NSFont = "<UICTFont: 0x7fa9fa82aec0> font-family: \"Helvetica Neue\"; font-weight: normal; font-style: normal; font-size: 18.00pt";
NSParagraphStyle = "Alignment 0, LineSpacing 0, ParagraphSpacing 0, ParagraphSpacingBefore 0, HeadIndent 0, TailIndent 0, FirstLineHeadIndent 0, LineHeight 0/0, LineHeightMultiple 0, LineBreakMode 0, Tabs (\n 28L,\n 56L,\n 84L,\n 112L,\n 140L,\n 168L,\n 196L,\n 224L,\n 252L,\n 280L,\n 308L,\n 336L\n), DefaultTabInterval 0, Blocks (null), Lists (null), BaseWritingDirection -1, HyphenationFactor 0, TighteningFactor 0, HeaderLevel 0";
}
很抱歉我无法 post 图片。我使用 swift 来执行此操作。
我的问题:我该怎么做?或者有另一种方法来保存属性文本?
是的,您可以通过简单的解决方法添加 attributedText。
添加 Transformable
类型的属性。创建 .h 文件后,将该属性的类型更改为 NSAttributedString
。在该属性中,您可以轻松保存属性字符串。
希望这对您有所帮助..:)
该属性在数据模型中应如下所示。
应修改头文件以匹配此文件:
@property (nonatomic, retain) NSAttributedString * attributedText;
就是这样。您应该能够像任何其他属性一样保留您的属性字符串。
假设您的实体是事件,并且您有一个事件类型的对象事件,您可以通过 event.attributedText 访问它。这里有一些示例 Swift 代码:
event.attributedText = NSAttributedString(string: "Hello World")
let attributedString = event.attributedText
如果您更喜欢用您的母语回答,请告诉我们。
在 Xcode 10 中使用自动代码生成,这比其他建议简单得多。
- 打开数据模型和select属性的名称
- 打开数据模型检查器(Command+Option+3)
- 将
Attribute Type
设置为Transformable
- 将
Custom Class
设置为NSAttributedString
就是这样,现在您可以按照预期将数据保存在 Swift 代码中,无需修改生成的 类 或强制转换,例如:
detailItem.content = textView.attributedText