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

前端 未结 7 1128
盖世英雄少女心
盖世英雄少女心 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 14:07

    To select the name of a file without the file extension, use this:

    -(void) tableViewCellDidBeginEditing:(UITableViewTextFieldCell*) cell
    {
        NSInteger fileNameLengthWithoutExt = [self.filename length] - [[self.filename pathExtension] length];
        UITextField* textField = cell.textField;
        UITextPosition* start = [textField beginningOfDocument];
        UITextPosition* end = [textField positionFromPosition:start offset: fileNameLengthWithoutExt - 1]; // the -1 is for the dot separting file name and extension
        UITextRange* range = [textField textRangeFromPosition:start toPosition:end];
        [textField setSelectedTextRange:range];
    }
    
    0 讨论(0)
提交回复
热议问题