我从服务器获取字符串如下所示,由于隐私政策,我添加了一些静态文本。
“\n1. You are Lorem ipsum doller sit amen Lorem ipsum doller sit amen Lorem ipsum doller sit amen . \n2. You are Lorem ipsum doller sit amen Lorem ipsum doller sit amen Lorem ipsum doller sit amen \n3. You are Lorem ipsum doller sit amen Lorem ipsum doller sit amen Lorem ipsum doller sit amen…\n”
上面的文字我必须显示如下格式。
1. You are Lorem ipsum doller sit amen Lorem ipsum doller sit amen Lorem ipsum doller sit amen . 2. You are Lorem ipsum doller sit amen Lorem ipsum doller sit amen Lorem ipsum doller sit amen 3. You are Lorem ipsum doller sit amen Lorem ipsum doller sit amen Lorem ipsum doller sit amen…
此外,索引号应具有不同的颜色以显示在标签中。
有什么建议么?
首先,该\n字符是换行符,因此将由UILabel为您处理。因此,您需要做的就是foregroundColor在每行的开头设置数字的匹配项。例如
\n
UILabel
foregroundColor
let string = "\n1. You are Lorem ipsum doller sit amen Lorem ipsum doller sit amen Lorem ipsum doller sit amen . \n2. You are Lorem ipsum doller sit amen Lorem ipsum doller sit amen Lorem ipsum doller sit amen \n3. You are Lorem ipsum doller sit amen Lorem ipsum doller sit amen Lorem ipsum doller sit amen…\n" let attributedString = NSMutableAttributedString(string: string) let regex = try! NSRegularExpression(pattern: #"^\d+\."#, options: .anchorsMatchLines) let range = NSRange(location: 0, length: attributedString.length) regex.enumerateMatches(in: string, range: range) { result, _, _ in guard let range = result?.range else { return } attributedString.addAttribute(.foregroundColor, value: UIColor.red, range: range) } label.numberOfLines = 0 label.attributedText = attributedString
这将产生:
如果您想要一个额外的空行,请将所有内容替换\n为\n\n:
\n\n
let string = "\n1. You are Lorem ipsum doller sit amen Lorem ipsum doller sit amen Lorem ipsum doller sit amen . \n2. You are Lorem ipsum doller sit amen Lorem ipsum doller sit amen Lorem ipsum doller sit amen \n3. You are Lorem ipsum doller sit amen Lorem ipsum doller sit amen Lorem ipsum doller sit amen…\n" .trimmingCharacters(in: .whitespacesAndNewlines) .replacingOccurrences(of: "\n", with: "\n\n")
如果您然后重复上述NSMutableAttributedString逻辑,则会产生:
NSMutableAttributedString
如果您希望它看起来像一个带有缩进的真实列表,您可能必须将文本重新格式化为 HTML(这本身就是一个烦人的练习),然后返回 aNSAttributedString并格式化那里的数字:
NSAttributedString
let string = """ <style> li { color: white; font-family: Arial, Helvetica, sans-serif; font-size: 12pt; margin-bottom: 12pt; } </style> <ol> <li>You are Lorem ipsum doller sit amen Lorem ipsum doller sit amen Lorem ipsum doller sit amen .</li> <li>You are Lorem ipsum doller sit amen Lorem ipsum doller sit amen Lorem ipsum doller sit amen</li> <li>You are Lorem ipsum doller sit amen Lorem ipsum doller sit amen Lorem ipsum doller sit amen…</li> </ol> """ guard let data = string.data(using: .utf16) else { return } let attributedString = try! NSMutableAttributedString(data: data, options: [.documentType: NSAttributedString.DocumentType.html], documentAttributes: nil) let regex = try! NSRegularExpression(pattern: #"^\s*\d+\."#, options: .anchorsMatchLines) let range = NSRange(location: 0, length: attributedString.length) regex.enumerateMatches(in: attributedString.string, range: range) { result, _, _ in guard let range = result?.range else { return } attributedString.addAttribute(.foregroundColor, value: UIColor.red, range: range) } label.attributedText = attributedString
如果您了解您的 CSS,您可能倾向于通过<style>for添加颜色ol:before,但这似乎不起作用。(如果你不了解你的 CSS 并且没有遵循我所说的,请忽略它,因为它不起作用,无论如何。)
<style>
ol:before
无论如何,如果你完成所有这些工作,就会产生: