简体   繁体   中英

Setting UITextView Cursor Position

I have multiple uitextviews with their respective light gray placeholder text onLoad. Delegates are properly set.

My goal is, on any uitextview tap, to have the cursor selected to the start of the uitextview if the placeholder is present.

My problem is when I initially tap outside the placeholder text range (the blank space without text), the textviewdidchangeselection delegate doesn't even fire and the cursor begins at the end of the placeholder text. When I initially tap the placeholder text things work properly. When I initially tap the placeholder text and then try to tap outside the placeholder text range, things also work properly.

Any thoughts on why it is behaving the way it is? I tried both .selectedRange and .selectedTextRange to no avail.

func onLoad() {
    textView1.text = placeHolderText1
    textView1.textColor = UIColor.lightGray
    
    textView2.text = placeHolderText2
    textView2.textColor = UIColor.lightGray
    
    textView1.textContainerInset.left = 4
    textView1.layer.cornerRadius = 2
    textView1.layer.masksToBounds = true
    textView1.delegate = self
    
    textView2.textContainerInset.left = 4
    textView2.layer.cornerRadius = 2
    textView2.layer.masksToBounds = true
    textView2.delegate = self
}

func textViewDidChangeSelection(_ textView: UITextView) {
    if self.view.window != nil {
        if textView == textView1 {
            if textView.textColor == UIColor.lightGray && textView.text == placeHolderText1 {
                textView.selectedTextRange = textView.textRange(from: textView.beginningOfDocument, to: textView.beginningOfDocument)
            }
        } else if textView == textView2 {
            if textView.textColor == UIColor.lightGray && textView.text == placeHolderText2 {
                textView.selectedRange = NSMakeRange(0, 0)
            }
        }
    }
}

If I'm not wrong, your final goal is to have a working placeholder? If yes, you can try this code:

//TextView place holder
extension UITextView{
    func setPlaceholder() {
        let placeholderLabel = UILabel()
        placeholderLabel.text = "Enter your hashtags ..."
        placeholderLabel.font = UIFont.italicSystemFont(ofSize: (self.font?.pointSize)!)
        placeholderLabel.sizeToFit()
        placeholderLabel.tag = 222
        placeholderLabel.frame.origin = CGPoint(x: 5, y: (self.font?.pointSize)! / 2)
        placeholderLabel.textColor = UIColor.lightGray
        placeholderLabel.isHidden = !self.text.isEmpty
        self.addSubview(placeholderLabel)
    }

    func checkPlaceholder() {
        let placeholderLabel = self.viewWithTag(222) as! UILabel
        placeholderLabel.isHidden = !self.text.isEmpty
    }
}

with this code in your viewController class that has the UItextViewDelegate:

func textViewDidChange(_ textView: UITextView) {
    textView1.checkPlaceholder()
    textView2.checkPlaceholder()
}

and this code in your viewController class viewDidload:

   textView1.setPlaceholder()
   textView2.setPlaceholder()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM