您的位置:首页 > 产品设计 > UI/UE

UIText光标用法

2015-11-27 19:24 991 查看
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]; //光标位置
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: