我有这个功能可以将项目符号添加到 textView
让子弹:字符串=“●”
func setAttributedValueForBullets(bullet: String, positionWhereTheCursorIs: Int?) {
var textRange = selectedRange
let selectedText = NSMutableAttributedString(attributedString: attributedText)
if let line = positionWhereTheCursorIs {
textRange.location = line
}
selectedText.mutableString.replaceCharacters(in: textRange, with: bullet)
let paragraphStyle = createParagraphAttribute()
selectedText.addAttributes([NSParagraphStyleAttributeName: paragraphStyle], range: NSMakeRange(textRange.location, bullet.length))
self.attributedText = selectedText
self.selectedRange = textRange
}
它在将项目符号插入只有一行这样的段落时起作用
我希望它看起来像第一张图片,没有项目符号中的空格和文本的开头
我也尝试过使用
selectedText.insert(bullet, at: textRange.location)
代替
selectedText.addAttributes([NSParagraphStyleAttributeName: paragraphStyle], range: NSMakeRange(textRange.location, bullet.length))