Can I select a specific block of text in a UITextField?

前端 未结 7 1171
盖世英雄少女心
盖世英雄少女心 2020-11-28 13:43

I have a UITextField in my iPhone app. I know how to make the text field select all of its text, but how can change the selection? Say I wanted to select the last 5 characte

7条回答
  •  一向
    一向 (楼主)
    2020-11-28 13:58

    These both work for me:

    [UITextField selectAll:self];
    

    and:

    UITextField.selectedRange = NSMakeRange(0, 5);
    

    but only if the text field is the first responder. (In other words, only if the keyboard is showing.) So, you need to precede either of the select methods with this:

    [UITextField becomeFirstResponder];
    

    if you want the text to appear selected.

提交回复
热议问题