首页 > 其他 > 详细

TextView和textField 定位光标问题

时间:2015-10-26 16:56:08      阅读:217      评论:0      收藏:0      [点我收藏+]

 

有时候需要定位光标位置,但是测试之后发现textField并没有响应方法能监听到光标位置的改变,但是textField可以定义光标位置的变化.

当 有textField需求要监听光标位置变化时,可以利用textView来代替

#import "ViewController.h"

@interface ViewController () <UITextViewDelegate>

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    UITextView *textView = [[UITextView alloc] initWithFrame:CGRectMake(40, 40, 400, 500)];
    
    textView.backgroundColor = [UIColor purpleColor];
    textView.font = [UIFont systemFontOfSize:20];
    textView.delegate = self;
    textView.scrollEnabled = NO;
    [self.view addSubview:textView];
   
    
}

- (void)textViewDidChangeSelection:(UITextView *)textView {
    NSLog(@"%ld", textView.selectedRange.location);
}


@end

 实现textView的代理方法即可

TextView和textField 定位光标问题

原文:http://www.cnblogs.com/cdp-snail/p/4911559.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!