如果我想更改在textview中输入的特定单词的颜色,我该怎么做?

时间:2015-09-06 22:20:22

标签: ios swift uitextview swift2

根据我的阅读,我需要使用rangeOfString来搜索textview条目,但我不确定如何去做。是否可以实时更改在textview中输入的文本的颜色,例如,如果有人写了“蓝色”,我可以在键入它的那一刻将单词更改为蓝色。如果是这样我怎么会这样做?我对编码很新,甚至更新。

2 个答案:

答案 0 :(得分:1)

您必须在文本视图中使用属性文本,然后使用textView(textView: UITextView, shouldChangeCharactersInRange range: NSRange, replacementString string: String) -> Bool方法,只要文本视图文本中的文字发生更改,就会触发该方法。在那里应用你自己的逻辑,以确定彩色文本将落入何种范围以及将如何发生......

确保您的控制器符合UITextViewDelegate协议,并使textView委派您的控制器。

示范:

class ViewController: UIViewController, UITextViewDelegate {

    @IBOutlet weak var textView: UITextView!
    override func viewDidLoad() {
        super.viewDidLoad()

        self.textView.delegate = self // important! Otherwise the textView will not know where to call the delegate functions!
    }

    func textView(textView: UITextView, shouldChangeTextInRange range: NSRange, replacementText text: String) -> Bool {

        // first make sure that the text field is the one we want to actually change
        if textView == self.textView{

            let nsString = textView.text as NSString // we explicitly cast the Swift string to NSString so that we can use rangeOfString for example 
            let stringLength = textView.text.characters.count
            // Arbitrarily check if the string in the text field is not empty
            // Apply your own logic for when to update the string
            if stringLength > 0{

                let text = NSMutableAttributedString(string: textView.text)
                // Currently the range is assumed to be the whole text (the range covers the whole string)
                // You'll have to apply your own logic here
                text.addAttribute(NSForegroundColorAttributeName, value: UIColor.redColor(), range: NSMakeRange(0, stringLength))

                textView.attributedText = text

            }
        }
        return true

    }

}

例如,而不是使用上面的颜色整个文本

text.addAttribute(NSForegroundColorAttributeName,value:UIColor.redColor(),range:NSMakeRange(0,stringLength))

第一次出现"你好"红色:

text.addAttribute(NSForegroundColorAttributeName, value: UIColor.redColor(), range: nsString.rangeOfString("hello"))

请注意,我明确地将textView的文字投放到NSString,以便我们可以使用范围函数,例如(rangeOfString()

答案 1 :(得分:0)

对swift进行了更改,其中count似乎不再适用于String。我对the_critic(https://stackoverflow.com/users/1066899/the-critic)给出的答案稍作修改。感谢所有帮助过的人

类ViewController:UIViewController,UITextViewDelegate {

@IBOutlet weak var textView: UITextView!
override func viewDidLoad() {
    super.viewDidLoad()

    self.textView.delegate = self // important! Otherwise the textView will not know where to call the delegate functions!
}

func textView(textView: UITextView, shouldChangeTextInRange range: NSRange, replacementText text: String) -> Bool {

    // first make sure that the text field is the one we want to actually change
    if textView == self.textView{

        let nsString = textView.text as NSString // we explicitly cast the Swift string to NSString so that we can use rangeOfString for example 
         let stringLength = textView.text.characters.count
        // Arbitrarily check if the string in the text field is not 

空             //为更新字符串时应用自己的逻辑             if stringLength> 0 {

            let text = NSMutableAttributedString(string: textView.text)
            // Currently the range is assumed to be the whole text (the range covers the whole string)
            // You'll have to apply your own logic here
            text.addAttribute(NSForegroundColorAttributeName, value: UIColor.redColor(), range: NSMakeRange(0, stringLength))

            textView.attributedText = text

        }
    }
    return true

}

}