【问题标题】:Is it possible to create UITextRange manually for first character?是否可以为第一个字符手动创建 UITextRange?
【发布时间】:2014-03-12 10:08:56
【问题描述】:

是否可以实例化UITextRange 和UITextField 的第一个字符? 我需要访问UITextField 的第一个字符的矩形,有人可以帮我吗?

提前致谢

【问题讨论】:

    标签: ios objective-c uitextrange


    【解决方案1】:

    您可以使用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]) 中工作。也许文本字段必须是第一响应者?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-12-21
    • 2022-08-04
    • 1970-01-01
    • 1970-01-01
    • 2015-09-04
    • 2015-12-20
    • 2012-02-25
    相关资源
    最近更新 更多