I want to clear previous text written on UITextView before new text is written on it. I did like this.
textView.text = @"";
textView.text = @"something";
But, previous text is not cleared. It overlaps with current text. Textview is non-editable.
UITextView supports the display of text using custom style information and also supports text editing. You typically use a text view to display multiple lines of text, such as when displaying the body of a large text document.
UITextView supports the display of text using custom style information and also supports text editing. You typically use a text view to display multiple lines of text, such as when displaying the body of a large text document. This class supports multiple text styles through use of the attributedText property.
To dismiss the keyboard, send the resignFirstResponder () message to the text view that is currently the first responder. Doing so causes the text view object to end the current editing session (with the delegate object’s consent) and hide the keyboard.
You need to implement the UITextViewDelegate and the method, textViewDidBeginEditing
. The following code sets the textView's text to @""
(nothing) when it starts editing.
- (void) textViewDidBeginEditing:(UITextView *) textView {
[textView setText:@""];
}
Here is a code for swift
func textViewDidBeginEditing(textView: UITextView) {
txtView.text = ""
txtView.textColor = UIColor.blackColor()
}
func textViewDidEndEditing(textView: UITextView) {
if txtView.text.isEmpty {
txtView.text = "Write your comment."
txtView.textColor = UIColor.blackColor()
}
}
func textView(textView: UITextView, shouldChangeTextInRange range: NSRange, replacementText text: String) -> Bool {
if text == "\n" // Recognizes enter key in keyboard
{
textView.resignFirstResponder()
return false
}
return true
}
Note : give delegate to your textview
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With