当前位置 : 主页 > 手机开发 > harmonyos >

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

来源:互联网 收集:自由互联 发布时间:2023-08-26
实现字数控制和限制,并展示当前输入的字符串长度,以及按钮的点击状态,代码如下: 1,设置代理 @interface FKViewController ()UITextViewDelegate@property (weak, nonatomic) IBOutlet UITextView *FKTextVi

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

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时,按钮返回初始状态,有输入字符时,显示为可点击状态,很简单,仅做记录用!


作者:稻草人11223

上一篇:ios开发之--把秒转换为天时分秒
下一篇:没有了
网友评论