我正在努力让自己熟悉CoreText
,我开始自己做笔记应用程序。
我与AttributedStrings
一起使用了正则表达式,并且我试图基本上模仿我现在输入的StackOverflow
文本框的功能。
我有所有常见的事情,如:
粗体
斜体
emphasis blocks
但我正在努力制作一个块引用/代码块。
像这样的东西,它会断开它自己的线条并创建一个无论文本多长时间都会到达边缘的框。
And it changes the background color
这是否可以严格使用AttributedStrings?我已经看到了一些注入HTML / CSS的旧例子,但我希望我可以使用一些特殊的NSAttributedStringKey
组合来完成它。
答案 0 :(得分:3)
是的,这是可能的。这是Swift 3中的示例游乐场:
Root cause of ServletException.
java.net.UnknownHostException: authservice.dd.aa.int
at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:184)
at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392)
at java.net.Socket.connect(Socket.java:589)
at org.company.authservice.util.httpclient.protocol.bouncycastle.StandardNamesTlsSocket.connect(StandardNamesTlsSocket.java:269)
at org.company.authservice.util.httpclient.protocol.Java5SSLSocketWrapper.connect(Java5SSLSocketWrapper.java:64)
Truncated. see log file for complete stacktrace
这是输出:
但重要的是要记住,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
的功能(布局和样式)远不如Web视图。如果你想得到花哨的东西(比如stackoverflow在blockquote框的左边缘有更深的颜色线),你应该只生成HTML和CSS并使用网页视图。