UITextView,订单号与标题和内容之间的距离 - Swift

时间:2016-06-23 10:24:45

标签: swift uitextview

示例,我有一个这样的字符串:

" 1。标题一。\ n1.1。这是内容之一......这是第二行。\ n2。标题二。\ n这是标题二的内容。"

当我在UITextView中显示如下:

enter image description here

但我想在UITextView中显示它:

enter image description here

我怎么做?

2 个答案:

答案 0 :(得分:0)

您可以在每个新行\t

之后使用\n

" 1. \ tTitle One。\ n1.1。\ t这是第一行的第一行......这是第二行。\ n2。\ t标题二。\ n \ t这是标题二的内容"

答案 1 :(得分:0)

使用属性字符串。但它相当冗长:

let indent: CGFloat = 36.0

let bodyParagraphStyle = NSMutableParagraphStyle()
bodyParagraphStyle.firstLineHeadIndent = indent
bodyParagraphStyle.headIndent = indent

let headerParagraphStyle = bodyParagraphStyle.mutableCopy() as! NSMutableParagraphStyle
headerParagraphStyle.firstLineHeadIndent = 0
headerParagraphStyle.tabStops = [NSTextTab(textAlignment: .Left, location: indent, options: [:])]

let bodyAttributes = [NSParagraphStyleAttributeName: bodyParagraphStyle]
let headerAttributes = [NSParagraphStyleAttributeName: headerParagraphStyle]

let text = NSMutableAttributedString()
text.appendAttributedString(NSAttributedString(string: "1\tTitle 1\n", attributes: headerAttributes))
text.appendAttributedString(NSAttributedString(string: "1.1\tThis is line one of content one\n", attributes: headerAttributes))
text.appendAttributedString(NSAttributedString(string: "This is line two\n", attributes: bodyAttributes))
text.appendAttributedString(NSAttributedString(string: "This is line three\n", attributes: bodyAttributes))

self.textView.attributedText = text