【发布时间】:2017-06-16 07:33:32
【问题描述】:
我有一个包含 UITextView 的视图控制器。这个 UITextView 根据控制它的数据模型(大约 30 个不同的文本选项)以编程方式更新文本。 UITextView 中的内容总是很长,每个大约 450 个字。段落范围为 2 - 5。
一切都很完美。我只需要一种视觉方式来分解段落。现在,我正在使用 \n\n 在段落之间创建几个换行符。虽然这可行,但我最终还是需要标题。
在 IB 中,我将 text 属性从 Plain 更改为 Attributed。这允许我在 Xcode 中以类似 WYSIWYG 的方式修改文本。效果很好,但我没有使用 IB 来更改文本。
我确实尝试过使用 WebView(下面的代码)。我用 H1 和 P 标签创建了一个非常基本的 HTML 文件。不幸的是,这将在我的项目中创建 30 多个 HTML 文件。从可管理性的角度来看,我不喜欢这样。文本也不能选择,它与 webview 一起使用(我不想诉诸 CSS [-webkit-user-select] 来缓解这个问题。)
let path: String? = Bundle.main.path(forResource: "myHTML", ofType: "html")
let file = try? String(contentsOfFile: path!, encoding: String.Encoding.utf8)
let baseURL = URL(fileURLWithPath: Bundle.main.bundlePath)
self.myWebView.loadHTMLString(file!, baseURL: baseURL)
我的理想解决方案
我可以坚持我目前的设置吗?使用我的数据模型以编程方式更改 UITextView 文本,同时能够更改代码中的文本属性以创建我的段落标题?
编辑 - 试图让它与 MVC 一起使用
allFormattedDescriptions: [
Formatted(heading: "heading 1", descriptionText: "Lorem Ipsum Paragraph 1"),
Formatted(heading: "heading 2", descriptionText: "Lorem Ipsum Paragraph 2"),
Formatted(heading: "heading 3", descriptionText: "Lorem Ipsum Paragraph 3")
]
// Ideal formatting; every paragraph will have a heading. Can handle that with one object that requires both a heading and description text (paragraph).
struct Formatted {
var heading: String!
var descriptionText: String!
var bodyParagraphStyle: NSMutableParagraphStyle = {
let style = NSMutableParagraphStyle()
style.lineSpacing = 10
style.paragraphSpacingBefore = 6
style.paragraphSpacing = 6
return style
}()
var headerParagraphStyle: NSMutableParagraphStyle = {
let style = NSMutableParagraphStyle()
style.paragraphSpacingBefore = 24
return style
}()
var bodyAttributes: [String: AnyObject]!
var headerAttributes: [String: AnyObject]!
}
【问题讨论】:
-
你绝对可以在代码中做到这一点。
NSMutableAttributedString允许您随意添加和删除属性。您还可以使用它来显示 HTML。如果您不需要 HTML 显示,那么您可以调用enumerateAttributesInRange来获取所有属性并修改它们,或者您可以调用addAttributes或setAttributes来设置各种样式。这包括段落样式和缩进、行距、颜色、下划线、删除线、字体等。
标签: ios swift xcode uitextview heading