focus a NSTextField

后端 未结 5 2092
深忆病人
深忆病人 2020-12-08 08:55

I have a NSTextField and I want to set its content if I klick on a button and than set the cursor on this textfield at the end of the text so if someone klicks the button he

5条回答
  •  轮回少年
    2020-12-08 09:04

    Some time ago I did similar task. My solution was write down small category method for partial text selection for NSTextField. This method does job like original selectText: does (with setting focus to control).

    @implementation NSTextField( SelExt )
    
    -(void)selectTextRange:(NSRange)range
    {
       NSText *textEditor = [self.window fieldEditor:YES forObject:self];
       if( textEditor ){
          id cell = [self selectedCell];
          [cell selectWithFrame:[self bounds] inView:self 
                editor:textEditor delegate:self
                start:range.location length:range.length];
       }
    }
    
    @end
    

    with this method you may place text selection where you want and start typing right after that.

提交回复
热议问题