Kautham Krishna
Kautham Krishna

Reputation: 967

Change the colour of the bullets in UITextView

I am using a UITextView to display a text which I get from a API call. From the text I need to identify the special character ","(comma) and replace it with a newline escape sequence and green coloured bullet points.

I done it using the .stringByReplacingOccurrencesOfString(",", withString: "\n•"). the bullet is obtained from Edit-> Emoji&Symbols. It works well.

But I don't know how to change the colour of the bullet. Is it possible to change the colour of the bullet? If so, how?

The colour code is 0x53B0A2.

I am using Xcode 7.1.1, Swift 2.0.

Upvotes: 0

Views: 1832

Answers (3)

mazy
mazy

Reputation: 688

Used Hamza Ansari answer and made it for 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
        }
    }
}

Usage:

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

Upvotes: 1

Daxesh Nagar
Daxesh Nagar

Reputation: 1415

In 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

Upvotes: 1

Hamza Ansari
Hamza Ansari

Reputation: 3084

you can use this function:

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

    }
}

just pass the string and it will return attributed string:

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

Upvotes: 2

Related Questions