码迷,mamicode.com
首页 > 移动开发 > 详细

ios开发之--实现TextView提示文字并且输入字数限制

时间:2017-11-17 16:25:04      阅读:250      评论:0      收藏:0      [点我收藏+]

标签:desc   idc   substring   length   nal   long   ios开发   set   oid   

实现字数控制和限制,并展示当前输入的字符串长度,以及按钮的点击状态,代码如下:

1,设置代理

@interface FKViewController ()<UITextViewDelegate>
@property (weak, nonatomic) IBOutlet UITextView *FKTextView;
@property (weak, nonatomic) IBOutlet UILabel *descLab;
@property (weak, nonatomic) IBOutlet UIButton *sendBtn;
@property (weak, nonatomic) IBOutlet UILabel *stringlenghtLab;

2,设置初始状态

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    
    self.view.backgroundColor = BackgroundColor;
    self.navigationController.navigationBarHidden = YES;
    CustomNavigationBar *nav = [[CustomNavigationBar alloc]initWithFrame:CGRectMake(0, 0, KscreenW, NavHeight) withTitle:@"意见反馈" withLeftBtnHidden:NO withRightBtn:YES];
    nav.delegate = self;
    [self.view addSubview:nav];
    
    self.FKTextView.delegate = self;
    self.descLab.userInteractionEnabled = NO;
    self.sendBtn.userInteractionEnabled = NO;
    self.sendBtn.backgroundColor = [UIColor lightGrayColor];
}

3,代理方法的实现

-(void)textViewDidChange:(UITextView *)textView
{
    self.descLab.hidden = YES;
    self.sendBtn.userInteractionEnabled = YES;
    self.sendBtn.backgroundColor = MainColor;
    //实时显示字数
    self.stringlenghtLab.text = [NSString stringWithFormat:@"%ld/100",(long)textView.text.length];
    
    //字数限制
    if (textView.text.length >= 100) {
        textView.text = [textView.text substringToIndex:100];
    }
    
    //取消安润点击权限,并显示文字
    if (textView.text.length == 0) {
        self.descLab.hidden = NO;
        self.sendBtn.userInteractionEnabled = NO;
        self.sendBtn.backgroundColor = [UIColor lightGrayColor];
    }
}

字符串长度为0时,按钮返回初始状态,有输入字符时,显示为可点击状态,很简单,仅做记录用!

ios开发之--实现TextView提示文字并且输入字数限制

标签:desc   idc   substring   length   nal   long   ios开发   set   oid   

原文地址:http://www.cnblogs.com/hero11223/p/7851607.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!