seguedestination
seguedestination

Reputation: 419

How to detect text in UITextView is empty?

It is easy for UITextField to check empty text...But I have a custom UITextView.If I have input text like @" "; We don't know what I input,but only can read the length of the text. How can I detect the empty text like "\n" , "", " "?Any idea?

Upvotes: 0

Views: 1423

Answers (4)

L33MUR
L33MUR

Reputation: 4058

For Swift 5.1

    if textView.text.trimmingCharacters(in: .whitespacesAndNewlines).isEmpty {
        // TextView is empty (or only has whitespaces and newlines) then ...
        // buttonSend.isEnabled = false
    } else {
        // TextView has some text, then ...
        // buttonSend.isEnabled = true
    }

Upvotes: 1

AZEE Tech
AZEE Tech

Reputation: 1

You can check through counting characters if there will no character in uitextview then characters count will be 0 and we can check on it's base:

if(yourTextView.text.characters.count == 0)
    {
        //if empty then
    }
    else
    {
       //if not empty then
    }

Upvotes: -1

tuledev
tuledev

Reputation: 10317

You can check it:

if(![[textField.text stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]] length]) {
   //string is all whitespace or newline

}

Upvotes: 3

Wain
Wain

Reputation: 119031

Use - (NSString *)stringByTrimmingCharactersInSet:(NSCharacterSet *)set with the white space character set and check the length of the result string.

Upvotes: 0

Related Questions