UITextview项目符号列表重复项目符号

时间:2018-12-13 03:03:10

标签: swift uitextview nsattributedstring

我试图在我的textview中添加一个项目符号列表,但是项目符号会不断重复每一行。当我转到新行并为该行添加项目符号时,它将为前几行添加项目符号。它显示在图像中。

let textviewValues = textView.text.components(separatedBy: .newlines)
let bullet = "•  "
let strings = textviewValues.map { return bullet + $0 }
var attributes = [NSAttributedString.Key: Any]()
attributes[.font] = UIFont.preferredFont(forTextStyle: .body)
attributes[.foregroundColor] = UIColor.darkGray
let paragraphStyle = NSMutableParagraphStyle()
paragraphStyle.headIndent = (bullet as NSString).size(withAttributes: attributes).width
attributes[.paragraphStyle] = paragraphStyle
let string = strings.joined(separator: "\n")
textView.attributedText = NSAttributedString(string: string, attributes: attributes)

结果:

enter image description here

0 个答案:

没有答案