2

Is it possible to instantiate UITextRange and for first character of UITextField? i need to access to rect of UITextField 's first character, can anyone help me?

Thanks in advance

Mojtaba
  • 514
  • 9
  • 19

1 Answers1

13

You can manually create a UITextRange with the methods provided by UITextInput:

UITextPosition *firstCharacterPosition = [textField beginningOfDocument];
UITextPosition *secondCharacterPosition = [textField positionFromPosition:firstCharacterPosition offset:1];
UITextRange *firstCharacterRange = [textField textRangeFromPosition:firstCharacterPosition toPosition:secondCharacterPosition];
NSString *firstCharacter = [textField textInRange:firstCharacterRange];
Austin
  • 5,625
  • 1
  • 29
  • 43
  • when i use this code in `resignFirstResponder` overrided in subclass of `UITextField` , `firstCharacter` almost is null. where i use from this code for working properly? – Mojtaba Mar 17 '14 at 10:35
  • This code worked inside an editing changed event (`[textField addTarget:self action:@selector(textFieldDidEdit:) forControlEvents:UIControlEventEditingChanged]`). Maybe the text field has to be first responder? – Austin Mar 17 '14 at 13:46