복붙노트

[SWIFT] 특정 텍스트의 UITextView 변화 텍스트 색상

SWIFT

특정 텍스트의 UITextView 변화 텍스트 색상

해결법


  1. 1.미안 해요, 당신의 메시지를 발견했습니다. 다음은 작업 예 (놀이터에서 테스트)입니다 :

    미안 해요, 당신의 메시지를 발견했습니다. 다음은 작업 예 (놀이터에서 테스트)입니다 :

    import UIKit
    
    
    func apply (string: NSMutableAttributedString, word: String) -> NSMutableAttributedString {
        let range = (string.string as NSString).rangeOfString(word)
        return apply(string, word: word, range: range, last: range)
    }
    
    func apply (string: NSMutableAttributedString, word: String, range: NSRange, last: NSRange) -> NSMutableAttributedString {
        if range.location != NSNotFound {
            string.addAttribute(NSForegroundColorAttributeName, value: UIColor.redColor(), range: range)
            let start = last.location + last.length
            let end = string.string.characters.count - start
            let stringRange = NSRange(location: start, length: end)
            let newRange = (string.string as NSString).rangeOfString(word, options: [], range: stringRange)
            apply(string, word: word, range: newRange, last: range)
        }
        return string
    }
    
    var chordsArray = ["Cmaj", "Bbmaj7"]
    var text = "Cmaj Bbmaj7 I Love Swift Cmaj Bbmaj7 Swift"
    var newText = NSMutableAttributedString(string: text)
    
    for word in chordsArray {
        newText = apply(newText, word: word)
    }
    
    newText
    

  2. 2.경우, 다른 사람이 내가 :) 내 엑스 코드 (9) 놀이터에서 무엇을 얻을 빠른 4에 필요합니다.

    경우, 다른 사람이 내가 :) 내 엑스 코드 (9) 놀이터에서 무엇을 얻을 빠른 4에 필요합니다.

    import UIKit
    import PlaygroundSupport
    class MyViewController : UIViewController
    {
        override func loadView()
        {
            let view = UIView()
            view.backgroundColor = .white
    
            let textView = UITextView()
            textView.frame = CGRect(x: 150, y: 200, width: 200, height: 20)
            textView.text = "@Kam @Jam @Tam @Ham"
            textView.textColor = .black
            view.addSubview(textView)
            self.view = view
    
            let query = "@"
    
            if let str = textView.text {
                let text = NSMutableAttributedString(string: str)
                var searchRange = str.startIndex..<str.endIndex
                while let range = str.range(of: query, options: NSString.CompareOptions.caseInsensitive, range: searchRange) {
                    text.addAttribute(NSAttributedStringKey.foregroundColor, value: UIColor.gray, range: NSRange(range, in: str))
                    searchRange = range.upperBound..<searchRange.upperBound
                }
                textView.attributedText = text
            }
        }
    }
    PlaygroundPage.current.liveView = MyViewController()
    

    나는 빠른 3, 수동으로 다음과 같이 NSRange에 범위 (String.Index)를 변환 할 필요가 있다고 생각합니다.

     let start = str.distance(from: str.startIndex, to: range.lowerBound)
     let len = str.distance(from: range.lowerBound, to: range.upperBound)
     let nsrange = NSMakeRange(start, len)
     text.addAttribute(NSAttributedStringKey.foregroundColor, value: UIColor.gray, range: nsrange)
    

  3. 3.스위프트 4.2 (5)

    스위프트 4.2 (5)

    let string = "* Your receipt photo was not clear or did not capture the entire receipt details. See our tips here.\n* Your receipt is not from an eligible grocery, convenience or club store."
    let attributedString = NSMutableAttributedString.init(string: string)
    let range = (string as NSString).range(of: "See our tips")
    attributedString.addAttribute(NSAttributedString.Key.foregroundColor, value: UIColor.blue, range: range)
    txtView.attributedText = attributedString
    txtView.isUserInteractionEnabled = true
    txtView.isEditable = false
    

    산출

  4. from https://stackoverflow.com/questions/33472171/uitextview-change-text-color-of-specific-text by cc-by-sa and MIT license