【发布时间】:2014-03-12 10:08:56
【问题描述】:
是否可以实例化UITextRange 和UITextField 的第一个字符?
我需要访问UITextField 的第一个字符的矩形,有人可以帮我吗?
提前致谢
【问题讨论】:
标签: ios objective-c uitextrange
是否可以实例化UITextRange 和UITextField 的第一个字符?
我需要访问UITextField 的第一个字符的矩形,有人可以帮我吗?
提前致谢
【问题讨论】:
标签: ios objective-c uitextrange
您可以使用UITextInput提供的方法手动创建UITextRange:
UITextPosition *firstCharacterPosition = [textField beginningOfDocument];
UITextPosition *secondCharacterPosition = [textField positionFromPosition:firstCharacterPosition offset:1];
UITextRange *firstCharacterRange = [textField textRangeFromPosition:firstCharacterPosition toPosition:secondCharacterPosition];
NSString *firstCharacter = [textField textInRange:firstCharacterRange];
【讨论】:
resignFirstResponder 的子类中覆盖UITextField 中使用此代码时,firstCharacter 几乎为空。我在哪里使用此代码正常工作?
[textField addTarget:self action:@selector(textFieldDidEdit:) forControlEvents:UIControlEventEditingChanged]) 中工作。也许文本字段必须是第一响应者?