zoukankan      html  css  js  c++  java
  • UITextField点击选中文字

    1、先创建UITextField

    - (void)viewDidLoad {
        [super viewDidLoad];
        // Do any additional setup after loading the view.
        self.view.backgroundColor = [UIColor greenColor];
        
        
        UITextField *TextF = [[UITextField alloc] initWithFrame:CGRectMake(10, 150, 200, 40)];
        
        TextF.borderStyle = UITextBorderStyleRoundedRect;
        TextF.text = @"11";
        TextF.delegate = self;
        [self.view addSubview:TextF];
        
        [TextF becomeFirstResponder];
        
        
        
    }
    

     2、不要在textFieldShouldBeginEditing里面实现,因为endDocument取出来为nil.

    在textFieldDidBeginEditing里面实现

    -(void) textFieldDidBeginEditing:(UITextField *)textField
    {
        NSLog(@"%@",textField.selectedTextRange);
        UITextPosition *endDocument = textField.endOfDocument;//获取 text的 尾部的 TextPositext
        
        UITextPosition *end = [textField positionFromPosition:endDocument offset:0];
        UITextPosition *start = [textField positionFromPosition:end offset:-textField.text.length];//左-右+
        textField.selectedTextRange = [textField textRangeFromPosition:start toPosition:end];
    }
    

    3、另外可以利用shouldChangeCharactersInRange实现补全选中的功能

    哦,如果反复点击textfiled出现第一次选中,第二次选中的状态的话,如果想一直被选中

    - (BOOL)textFieldShouldEndEditing:(UITextField *)textField{
        UITextPosition *beginDocument = textField.beginningOfDocument;
        UITextPosition *end = [textField positionFromPosition:beginDocument offset:0];
        UITextPosition *start = [textField positionFromPosition:beginDocument offset:0];//左-右+
        textField.selectedTextRange = [textField textRangeFromPosition:start toPosition:end];
        return YES;
    }
    
    ps:
    [textField performSelector:@selector(selectAll:) withObject: textField];
    也可以有选中效果,至于效果,因需求而异
    

      

    textFieldShouldEndEditing

    中实现

  • 相关阅读:
    Memcached Tip 1:使用Memcached Providers
    MVC TIP8:为控制器增加有参构造函数(为了注入等其它用途)
    压力测试的轻量级具体做法
    Memcached Tip 2:Session同步
    ASP.NET性能优化之分布式Session
    ASP.NET性能优化之让浏览器缓存动态网页
    最精简领域驱动设计开发模版(针对WPF)
    MOQ TIP1:简介加基础
    ASP.NET性能优化之减少请求
    MOQ TIP2:匹配参数
  • 原文地址:https://www.cnblogs.com/GJ-ios/p/6847664.html
Copyright © 2011-2022 走看看