如何为UITextView Swift添加Underline / Bold / Italicize功能 [英] How to add Underline/Bold/Italicize Abilities for UITextView Swift
问题描述
我一直在寻找一个解决方案来解决如何授予用户在 UITextView
中加下划线,斜体和粗体文字的能力,但是还没有能力找到一个坚实的解决方案。我已经意识到我将需要使用 NSAttributedString
,但我不确定如何创建 UI
并格式化字符串,所以我能够将其保存到数据库。如果你能指出我正确的方向,将非常感激。谢谢!
I have been searching for quite a while for a solution as to how to grant users the ability to underline, italicize and bold text in a UITextView
but have not been able to find a solid solution. I have realized that I will need to use an NSAttributedString
but am not sure as to how to create the UI
and format the string so I am able to save it to a database. If you could point me in a proper direction that would be greatly appreciated. Thanks!
推荐答案
UI只是一个普通的可编辑UITextView,其中 allowsEditingTextAttributes
设置为 true
。
The UI is simply an ordinary editable UITextView with allowsEditingTextAttributes
set to true
.
您如何保存取决于您,但最通用的解决方案可能是取出UITextView的 attributedText
,将其存档到NSData(现在称为Swift中的数据),并将其保存。
How you save is up to you, but the most universal solution is probably to pull out the UITextView's attributedText
, archive it into an NSData (now known as Data in Swift), and save it off.
这篇关于如何为UITextView Swift添加Underline / Bold / Italicize功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!