-1

My code not working with NSRange location on iOS13, on iOS12 and below it's work. Is there a way to color the text from the letters to be colored until the total letters afterwards? because I only have data starting from the colored letters and the total letters afterwards.

 override func viewDidLoad() {
        super.viewDidLoad()

        let text = "Testing Attributed Strings"
        let attributedString = NSMutableAttributedString(string: text)
        let dataStartIHave = 0
        let dataTotalIHave = 7

        attributedString.addAttribute(.foregroundColor, value: UIColor.red, range: NSRange(location: dataStartIHave, length: dataTotalIHave))

        attributedLabel.attributedText = attributedString
    }

Thanks in advance.

Loren Ramly
  • 1,091
  • 4
  • 13
  • 21
  • Yes it's working, my code wrong because I include attributedLabel.text = text, after apply attributedText, so the text color will black all. So the wrong not NSRange with location. – Loren Ramly Sep 23 '19 at 19:15
  • @LorenRamly so just delete your post – Leo Dabus Sep 23 '19 at 20:13

2 Answers2

3

To get the range from the end of the word until the end of the text reliably you have to convert NSRange to Range<String.Index> and back

let text = "Testing Attributed Strings"
let attributedString = NSMutableAttributedString(string: text)
let dataStartIHave = 0
let dataTotalIHave = 7
let wordRange = NSRange(location: dataStartIHave, length: dataTotalIHave)
let upperBound = Range(wordRange, in: text)!.upperBound
let upperRange = NSRange(upperBound..., in: text)

attributedString.addAttribute(.foregroundColor, value: UIColor.red, range: upperRange)
attributedLabel.attributedText = attributedString

In Swift it's more efficient to get the Range<String.Index> from the word

let text = "Testing Attributed Strings"
let attributedString = NSMutableAttributedString(string: text)
if let testingRange = text.range(of: "Testing") {
    let upperRange = NSRange(testingRange.upperBound..., in: text)       
    attributedString.addAttribute(.foregroundColor, value: UIColor.red, range: upperRange)
}
attributedLabel.attributedText = attributedString
vadian
  • 274,689
  • 30
  • 353
  • 361
  • Yes it's working, my code wrong because I include attributedLabel.text = text, after apply attributedText, so the text color will black all. So the wrong not NSRange with location. – Loren Ramly Sep 23 '19 at 19:16
  • Nevertheless in Swift **never** create `NSRange` objects with integer location and length to apply it directly to the `range` parameter of `NSAttributedString` (or `NSRegularExpression`). – vadian Sep 23 '19 at 19:19
-1

This will work:

    attributedString.addAttribute(.foregroundColor, value: UIColor.red, range: NSRange(location: dataStartIHave, length: dataTotalIHave))
E.Coms
  • 11,065
  • 2
  • 23
  • 35
  • Yes it's working, my code wrong because I include attributedLabel.text = text, after apply attributedText, so the text color will black all. So the wrong not NSRange with location. – Loren Ramly Sep 23 '19 at 19:16