Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I check out-of-sight characters in UITextField?

I have a UITextField that shrinks and expands when user input text. If the textfield's width reach the screen width, I want it to be right-aligned so user can see the last input characters. In other circumstance, I want it to be left-aligned.

Because the textfield's maximum width is not exactly the same with screen, I need to find a way to check if it has characters out of visible area.

Is there anyway to achieve this?

like image 338
Zhu Shengqi Avatar asked Sep 04 '15 07:09

Zhu Shengqi


1 Answers

You could try checking the width of the string that is in the field and comparing it to the width of the field itself. It would look something like this -

CGSize textSize = [self.field.text sizeWithAttributes:@{NSFontAttributeName:self.field.font}];

// If the text is larger than the field
if (textSize.width > self.field.bounds.size.width) {
    // There is text that is not visible in the field. 
}

This is fairly rough, but should get you close.

like image 142
Eagle11 Avatar answered Sep 20 '22 00:09

Eagle11