如何在 UITextView 中像块引号一样格式化文本
How to format text like a blockquote within a UITextView
我正在尝试让自己熟悉 CoreText
,我开始制作自己的笔记应用程序。
我将正则表达式与 AttributedStrings
结合使用,并试图从本质上模仿我现在输入的 Whosebug
文本框的功能。
我拥有所有常见的东西,例如:
加粗
斜体
headers
emphasis blocks
但我正在努力制作块引用/代码块。
Something like this where it breaks to its own line and creates a box that goes to the edge no matter how long the text is.
And it changes the background color
这甚至可以严格使用 AttributedStrings 吗?我见过一些注入 HTML / CSS 的旧示例,但我希望我可以使用一些特殊的 NSAttributedStringKey
组合来完成它。
是的,这是可能的。这是 Swift 3 中的示例游乐场:
import UIKit
import PlaygroundSupport
let richText = NSMutableAttributedString()
let chunk0 = NSAttributedString(string: "But I am struggling to make a block quote / code block.\n\n")
richText.append(chunk0)
let paragraphStyle = NSMutableParagraphStyle()
paragraphStyle.headIndent = 20
// Note that setting paragraphStyle.firstLineHeadIndent
// doesn't use the background color for the first line's
// indentation. Use a tab stop on the first line instead.
paragraphStyle.tabStops = [NSTextTab(textAlignment: .left, location: paragraphStyle.headIndent, options: [:])]
let chunk1 = NSAttributedString(string: "\tSomething like this where it breaks to its own line and creates a box that goes to the edge no matter how long the text is.\n", attributes: [
NSParagraphStyleAttributeName: paragraphStyle,
NSBackgroundColorAttributeName: UIColor.yellow
])
richText.append(chunk1)
let chunk2 = NSAttributedString(string: "\nIs this even possible to do strictly using AttributedStrings?")
richText.append(chunk2)
let textView = UITextView(frame: CGRect(x: 0, y: 0, width: 120, height: 400))
textView.backgroundColor = .white
textView.attributedText = richText
PlaygroundPage.current.liveView = textView
这是输出:
但请务必记住,UITextView
的功能(在布局和样式方面)远不如 Web 视图。如果你想变得花哨(就像 Whosebug 对 blockquote 框左边缘的深色线所做的那样),你应该只生成 HTML 和 CSS 并使用网络视图。
我正在尝试让自己熟悉 CoreText
,我开始制作自己的笔记应用程序。
我将正则表达式与 AttributedStrings
结合使用,并试图从本质上模仿我现在输入的 Whosebug
文本框的功能。
我拥有所有常见的东西,例如:
加粗
斜体
headers
emphasis blocks
但我正在努力制作块引用/代码块。
Something like this where it breaks to its own line and creates a box that goes to the edge no matter how long the text is.
And it changes the background color
这甚至可以严格使用 AttributedStrings 吗?我见过一些注入 HTML / CSS 的旧示例,但我希望我可以使用一些特殊的 NSAttributedStringKey
组合来完成它。
是的,这是可能的。这是 Swift 3 中的示例游乐场:
import UIKit
import PlaygroundSupport
let richText = NSMutableAttributedString()
let chunk0 = NSAttributedString(string: "But I am struggling to make a block quote / code block.\n\n")
richText.append(chunk0)
let paragraphStyle = NSMutableParagraphStyle()
paragraphStyle.headIndent = 20
// Note that setting paragraphStyle.firstLineHeadIndent
// doesn't use the background color for the first line's
// indentation. Use a tab stop on the first line instead.
paragraphStyle.tabStops = [NSTextTab(textAlignment: .left, location: paragraphStyle.headIndent, options: [:])]
let chunk1 = NSAttributedString(string: "\tSomething like this where it breaks to its own line and creates a box that goes to the edge no matter how long the text is.\n", attributes: [
NSParagraphStyleAttributeName: paragraphStyle,
NSBackgroundColorAttributeName: UIColor.yellow
])
richText.append(chunk1)
let chunk2 = NSAttributedString(string: "\nIs this even possible to do strictly using AttributedStrings?")
richText.append(chunk2)
let textView = UITextView(frame: CGRect(x: 0, y: 0, width: 120, height: 400))
textView.backgroundColor = .white
textView.attributedText = richText
PlaygroundPage.current.liveView = textView
这是输出:
但请务必记住,UITextView
的功能(在布局和样式方面)远不如 Web 视图。如果你想变得花哨(就像 Whosebug 对 blockquote 框左边缘的深色线所做的那样),你应该只生成 HTML 和 CSS 并使用网络视图。