更改 UITextView 中项目符号的颜色

Change the colour of the bullets in UITextView

我正在使用 UITextView 来显示从 API 调用中获得的文本。我需要从文本中识别特殊字符“,”(逗号)并将其替换为换行转义序列和绿色项目符号点。

我使用 .stringByReplacingOccurrencesOfString(",", withString: "\n•") 完成了它。子弹是从Edit-> Emoji&Symbols获得的。它运作良好。

但是我不知道如何改变子弹的颜色。是否可以更改子弹的颜色?如果是这样,如何?

颜色代码为0x53B0A2

我正在使用 Xcode 7.1.1,Swift 2.0。

你可以使用这个功能:

func attributedTextForString(text:String)->NSAttributedString{
    let r = text.stringByReplacingOccurrencesOfString(",", withString: "\n•") as NSString

    let attributedString = NSMutableAttributedString(string: r as String)

    let greenColorAttribure = [NSForegroundColorAttributeName: UIColor(red: 83/255.0, green: 176/255.0, blue: 162/255.0, alpha: 1.0)]

    do {

        let regex = try NSRegularExpression(pattern: "•", options: NSRegularExpressionOptions.CaseInsensitive)

        regex.enumerateMatchesInString(r as String, options: [], range: NSMakeRange(0, r.length), usingBlock: { (result, flags, pointer) -> Void in

            if let result = result{
                attributedString.addAttributes(greenColorAttribure, range:result.range)
            }
        })
        return attributedString

    }catch{
        return attributedString

    }
}

只需传递字符串,它就会return归因于字符串:

        let yourText = "hekkli sdfhos afs , sdfsf sfsfjms , sdfsf skldf, kshfg "
        let coloredBulletString = attributedTextForString(yourText)
        textView.attributedText = coloredBulletString

在Swift3.1

  let  DelevieryDateString = "●" + "This is a list item!"

    let DelevieryDateStr: NSMutableAttributedString =  NSMutableAttributedString(string: DelevieryDateString)

    DelevieryDateStr.addAttribute(NSForegroundColorAttributeName,
                                    value: UIColor.green, //color code what you want
                                    range: NSRange(
                                        location:0, // Find the location of the bullet and replace it
                                        length: 1))
lblitem.attributedText = DelevieryDateStr

使用 Hamza Ansari 答案并为 Swift 4.2:

extension UITextView {
    func setBulletList(text:String, bullet: String, bulletColor: UIColor, attributes: [NSAttributedString.Key: Any]) {
        let r0 = text.replacingOccurrences(of: ",", with: "\n" + bullet)
        let r = bullet + r0

        let attributedString = NSMutableAttributedString(string: r)

        attributedString.addAttributes(attributes, range: NSMakeRange(0, attributedString.length))

        let greenColorAttribure = [NSAttributedString.Key.foregroundColor: bulletColor]

        do {

            let regex = try NSRegularExpression(pattern: bullet, options: NSRegularExpression.Options.caseInsensitive)

            regex.enumerateMatches(in: r as String, options: [], range: NSMakeRange(0, r.count), using: { (result, flags, pointer) -> Void in

                if let result = result{
                    attributedString.addAttributes(greenColorAttribure, range:result.range)
                }
            })

            self.attributedText = attributedString

        } catch {
            self.attributedText = attributedString
        }
    }
}

用法:

lazy var textView: UITextView = {
        let view = UITextViewFixed()

        view.setBulletList(text: "Tags,Users,Jobs", bullet: "●  ", bulletColor: UIColor.red, attributes: [
            NSAttributedString.Key.foregroundColor: UIColor.black,
            NSAttributedString.Key.font: UIFont.systemFont(ofSize: 17)
            ])

        return view
    }()