2016-02-23 控件之label textview 混合使用

  

 

    1:_descrView = [[UITextView alloc]initWithFrame:CGRectMake(15, CGRectGetMaxY(describelab.frame)+10, SCREEN_WIDTH-30, 150)];

    _descrView.delegate = self;

   

    _descrView.layer.cornerRadius = 5;

    //返回键的类型

    _descrView.returnKeyType = UIReturnKeyDefault;

    

    //键盘类型

    _descrView.keyboardType = UIKeyboardTypeDefault;

    [scrollv addSubview:_descrView];

    //gzz0222 高度刚开始为40

    _placeholderLab = [[UILabel alloc]initWithFrame:CGRectMake(5, 5, CGRectGetWidth(_descrView.frame)-10, 20)];

    _placeholderLab.backgroundColor = [UIColor clearColor];

    _placeholderLab.textColor = [UIColor grayColor];

    _placeholderLab.text = @"详细描述您的问题,包括身体状况等限二百字内";

    _placeholderLab.font = _descrView.font;

    _placeholderLab.numberOfLines = 0;

    _placeholderLab.lineBreakMode=NSLineBreakByWordWrapping;//gzz0223

    [_placeholderLab sizeToFit];

    _placeholderLab.font = SYSTEMFONT(14);

    [_descrView addSubview:_placeholderLab];

 

 

2:textview 代理方法

- (void)textViewDidBeginEditing:(UITextView *)textView

{

    _placeholderLab.text = @"";

}

//结束编辑

- (void)textViewDidEndEditing:(UITextView *)textView

{

    

    [_descrView resignFirstResponder];

    if (self.descrView.text.length==0) {

        _placeholderLab.text =@"详细描述您的问题,包括身体状况等限二百字内";

        

    }

    else{

        _placeholderLab.text = @"";

    }

}

 

//控制输入文字的长度和内容,可通调用以下代理方法实现 gzz0203

- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text

{

//    if (range.location>=200)

//    {

//        //控制输入文本的长度

//        UIAlertView *alve = [[UIAlertView alloc]initWithTitle:@"提示" message:@"输入内容最多200" delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];

//        [alve show];

//        

//        return  NO;

//    }

    if ([text isEqualToString:@" "]) {

        //禁止输入换行

        [textView resignFirstResponder];

        return NO;

    }

    else

    {

        return YES;

    }

}

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event

{

    [super touchesBegan:touches withEvent:event];

    [self.view endEditing:YES];

    //隐藏键盘

    [_descrView resignFirstResponder];

    

}

/*============ textfield textview  delegate=============================*/

 

 

 

 

3:监听文本字数  汉字 字母 数字 全部

 

    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(textViewEditChanged11:) name:UITextViewTextDidChangeNotification object:_descrView];

 

 

- (void)dealloc

 

{

    

    [[NSNotificationCenter defaultCenter] removeObserver:self];

    [[NSNotificationCenter defaultCenter]removeObserver:self

                                                   name:@"UITextViewTextDidChangeNotification"

                                                 object:_descrView];

    

}

// 监听文本改变

-(void)textViewEditChanged11:(NSNotification *)obj{

    

    UITextView *textView = (UITextView *)obj.object;

    

    

    

    NSString *toBeString = textView.text;

    

    NSString *lang = [[UITextInputMode currentInputMode] primaryLanguage]; // 键盘输入模式

    

    if ([lang isEqualToString:@"zh-Hans"]) { // 简体中文输入,包括简体拼音,健体五笔,简体手写

        

        UITextRange *selectedRange = [textView markedTextRange];

        

        //获取高亮部分

        

        UITextPosition *position = [textView positionFromPosition:selectedRange.start offset:0];

        

        // 没有高亮选择的字,则对已输入的文字进行字数统计和限制

        

        if (!position) {

            

            if (toBeString.length >200) {

                

                textView.text = [toBeString substringToIndex:200];

                UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示" message:@"输入内容最多200" delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];

                [alert show];

            }

            

        }

        

        // 有高亮选择的字符串,则暂不对文字进行统计和限制

        

        else{

            

            

            

        }

        

    }

    

    // 中文输入法以外的直接对其统计限制即可,不考虑其他语种情况

    

    else{

        

        if (toBeString.length > 200) {

            

            textView.text = [toBeString substringToIndex:200];

            UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示" message:@"输入内容最多200" delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];

            [alert show];

            

        }

        

    }

    

}

 

 

 

原文地址:https://www.cnblogs.com/gzz2016/p/5209362.html