在UITextView中更改项目符号的颜色

时间:2015-12-29 06:29:16

标签: ios cocoa-touch uitextview special-characters nsattributedstring

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

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

但我不知道如何改变子弹的颜色。是否有可能改变子弹的颜色?如果是这样,怎么样?

颜色代码为0x53B0A2

我正在使用Xcode 7.1.1, Swift 2.0。

3 个答案:

答案 0 :(得分:2)

你可以使用这个功能:

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

    }
}

只需传递字符串,它将返回属性字符串:

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

答案 1 :(得分:1)

在Swift 3.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

答案 2 :(得分:0)

使用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
    }()