2016-03-15 uitextview placehold

1:

    _descrViewab = [[UITextView alloc]initWithFrame:CGRectMake(15, CGRectGetMaxY(contentlab.frame)+20, SCREEN_WIDTH-30, 150)];

    _descrViewab.delegate = self;

    //    _descrView.layer.borderColor = (__bridge CGColorRef)([UIColor blackColor]);

    //    _descrViewa.layer.borderWidth = 1;

    _descrViewab.layer.cornerRadius = 5;

    //返回键的类型

    _descrViewab.returnKeyType = UIReturnKeyDefault;

    //    _descrViewa.contentSize = CGSizeMake(SCREEN_WIDTH-60, 150);

    //键盘类型

    

    //获得焦点

    //    [_descrViewa becomeFirstResponder];

    _descrViewab.keyboardType = UIKeyboardTypeDefault;

    [self.view addSubview:_descrViewab];

    

    _placeholderLaba = [[UILabel alloc]initWithFrame:CGRectMake(5, 5, CGRectGetWidth(_descrViewab.frame)-5, 100)];

    _placeholderLaba.backgroundColor = [UIColor clearColor];

    _placeholderLaba.textColor = [UIColor grayColor];

    

    

    _placeholderLaba.text = @"限二百字文字描述";

    

    _placeholderLaba.numberOfLines=0;

    [_placeholderLaba sizeToFit];

    _placeholderLaba.font = _descrViewab.font;

    [_descrViewab addSubview:_placeholderLaba];

 2:代理

- (void)textViewDidBeginEditing:(UITextView *)textView

{

    _placeholderLaba.text = @"";

}

//结束编辑

- (void)textViewDidEndEditing:(UITextView *)textView

{

    

    [_descrViewab resignFirstResponder];

    if (_descrViewab.text.length==0) {

        _placeholderLaba.text =@"限二百字文字描述";

        

    }

    else{

        _placeholderLaba.text = @"";

    }

}

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

- (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:@"\n"]) {

        //禁止输入换行

        [textView resignFirstResponder];

        return NO;

    }

    else

    {

        return YES;

    }

}

 3:点击任何地方键盘消失

:

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

{

    [super touchesBegan:touches withEvent:event];

    [self.view endEditing:YES];

    //隐藏键盘

    [_descrViewab resignFirstResponder];

    

    

    //    [self.editingTextField  resignFirstResponder] ;

    

}

 

 

4:

限制字数可以是: 汉字各种等

    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(textViewEditChanged:) name:UITextViewTextDidChangeNotification object:_descrViewab];  viewdidload

 

 

- (void)dealloc

 

{

    

    [[NSNotificationCenter defaultCenter] removeObserver:self];

    [[NSNotificationCenter defaultCenter]removeObserver:self

                                                   name:@"UITextViewTextDidChangeNotification"

                                                 object:_descrViewab];

    

}

 

 

 

// 监听文本改变

-(void)textViewEditChanged:(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 > MaxNumberOfDescriptionCharss) {

                

                textView.text = [toBeString substringToIndex:MaxNumberOfDescriptionCharss];

                UIAlertView *alert = [[UIAlertView alloc] initWithTitle:nil message:@"不能超过200个字!" delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];

                [alert show];

            }

            

        }

        

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

        

        else{

            

            

            

        }

        

    }

    

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

    

    else{

        

        if (toBeString.length > MaxNumberOfDescriptionCharss) {

            

            textView.text = [toBeString substringToIndex:MaxNumberOfDescriptionCharss];

            UIAlertView *alert = [[UIAlertView alloc] initWithTitle:nil message:@"不能超过200个字!" delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];

            [alert show];

            

        }

        

    }

    

}

 

 

 

 

转载于:https://www.cnblogs.com/gzz2016/p/5280433.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值