UITextView使用的一点总结

创建textView: 

_textView = [[UITextView alloc] initWithFrame:CGRectMake(0, 0,100,50)];
[_textView setBackgroundColor:[UIColor clearColor]]; 
[[UITextView appearance] setTintColor:[UIColor whiteColor]]; //设置光标颜色
[_textView setTextColor:[UIColor colorwithTSDefine:color_iPad_remote_txt_1]];
[_textView setDelegate:self];
[_textView setFont:[UIFont systemFontOfSize:15.0f]];
_textView.scrollEnabled = YES;//是否可以拖动

当textView输入中文的时候,需要判断是否输入完整的中文的时候需要做如下判断,例如我的程序中就需要判断每次确定输入中文以后做一些事情,

#pragma mark - UITextViewDelegate
// 编辑内容有修改
- (void)textViewDidChange:(UITextView *)textView
{
    if ([_btnRemoteInputRealTime isSelected]) {
       // 判断是否完成了中文的输入
        if (textView.markedTextRange == nil) {
             NSLog(@"textView text:%@", textView.text);
            // do something with the new input chinese
        }
    }
}
// 完成编辑
- (void)textViewDidEndEditing:(UITextView *)textView
{
    if ([_btnRemoteInputRealTime isSelected]) {
        [_btnInputHand setHidden:NO];
        [_imgViewInputRealTime setHidden:YES];
    }
}
// 
- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text
{
    NSLog(@"%@, %@",textView.text, text);
    return YES;
}

// 其余的delegate方法再次不做详细说明了,罗列如下:
- (BOOL)textViewShouldBeginEditing:(UITextView *)textView;
- (BOOL)textViewShouldEndEditing:(UITextView *)textView;
- (void)textViewDidBeginEditing:(UITextView *)textView;
- (void)textViewDidChange:(UITextView *)textView;
- (BOOL)textView:(UITextView *)textView shouldInteractWithURL:(NSURL *)URL inRange:(NSRange)characterRange NS_AVAILABLE_IOS(7_0);
- (BOOL)textView:(UITextView *)textView shouldInteractWithTextAttachment:(NSTextAttachment *)textAttachment inRange:(NSRange)characterRange NS_AVAILABLE_IOS(7_0);


你可能感兴趣的:(UITextView使用的一点总结)