码迷,mamicode.com
首页 > 其他好文 > 详细

UIText光标用法

时间:2015-11-27 21:35:39      阅读:146      评论:0      收藏:0      [点我收藏+]

标签:

1.获取当前光标位置

UITextRange *range = _textField.selectedTextRange;

2.选取开始到最后的文字

    UITextPosition *start = [self.textField beginningOfDocument]; //开始的光标
    UITextPosition *end = [self.textField endOfDocument]; //文字末尾的贯标
    UITextRange *range = [self.textField textRangeFromPosition:start toPosition:end];
//共选取了所有的文字

3.光标左移(右移)

    UITextRange *range = _textField.selectedTextRange;
    UITextPosition *start = [self.textField positionFromPosition:range.start inDirection:(UITextLayoutDirectionLeft) offset:3]; //当前光标左移三位
    UITextPosition *end = [self.textField positionFromPosition:range.start inDirection:(UITextLayoutDirectionLeft) offset:1]; //当前光标左移一位
  //共选取了两个字符

4.光标定位

_textField.selectedTextRange = [self.textField textRangeFromPosition:start toPosition:end];

 5.设置光标颜色

     _textField.tintColor = [UIColor redColor];  //设置_textField 的光标颜色
    
    [_textField becomeFirstResponder];
    [[UITextView appearance] setTintColor:[UIColor greenColor]];//设置所有UITextView中的光标颜色为绿色,但是必须在UITextView对象产生之前,不然无效 (20151127)

 6.当前光标的位置

    UITextRange *range = _textField.selectedTextRange;  //当前光标
    UITextPosition *start = [self.textField beginningOfDocument];  //起点位置
    NSInteger off = [self.textField offsetFromPosition:start toPosition:range.start]; //光标位置

 

UIText光标用法

标签:

原文地址:http://www.cnblogs.com/gulong/p/5001563.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!