iOS实现TextView提示文字并且输入字数限制
2016-08-19 21:28
886 查看
实现TextView提示文字并且输入字数限制的反馈意见功能-----非常简洁明了!如图实现页面,效果如下:
1.输入文字的时候提示文字消失,TextView没有文字的时候提示文字显示;
2.右下角实时显示字数;
3.字数到达指定限制后,TextView不能输入更多,可以删除;
4.提交按钮在TextView不为空的时候按钮为绿色且可点击;TextView为空时,为灰色状态且不可点击。
.m中的代码如下:
#import "YJFeedBackViewController.h"
@interface YJFeedBackViewController ()<UITextViewDelegate>
@property (weak,
nonatomic) IBOutlet
UILabel *placeHolder;
@property (weak,
nonatomic) IBOutlet
UIButton *commitButton;
@property (weak,
nonatomic) IBOutlet
UITextView *feedBackTextView;
@property (weak,
nonatomic) IBOutlet
UILabel *stirngLenghLabel;
@end
@implementation YJFeedBackViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.title =
@"反馈意见";
self.automaticallyAdjustsScrollViewInsets =
NO;
self.feedBackTextView.delegate =
self;
self.placeHolder.userInteractionEnabled =
NO;
self.commitButton.userInteractionEnabled =
NO;
self.feedBackTextView.layer.borderWidth =
0.5;
self.feedBackTextView.layer.borderColor = [UIColor
lightGrayColor].CGColor;
// Do any additional setup after loading the view from its nib.
}
//正在改变
- (void)textViewDidChange:(UITextView *)textView
{
FDLog(@"%@", textView.text);
self.placeHolder.hidden =
YES;
//允许提交按钮点击操作
self.commitButton.backgroundColor =
FDMainColor;
self.commitButton.userInteractionEnabled =
YES;
//实时显示字数
self.stirngLenghLabel.text = [NSString
stringWithFormat:@"%lu/100", (unsigned
long)textView.text.length];
//字数限制操作
if (textView.text.length >=
100) {
textView.text = [textView.text
substringToIndex:100];
self.stirngLenghLabel.text =
@"100/100";
}
//取消按钮点击权限,并显示提示文字
if (textView.text.length ==
0) {
self.placeHolder.hidden =
NO;
self.commitButton.userInteractionEnabled =
NO;
self.commitButton.backgroundColor = [UIColor
lightGrayColor];
}
}
1.输入文字的时候提示文字消失,TextView没有文字的时候提示文字显示;
2.右下角实时显示字数;
3.字数到达指定限制后,TextView不能输入更多,可以删除;
4.提交按钮在TextView不为空的时候按钮为绿色且可点击;TextView为空时,为灰色状态且不可点击。
.m中的代码如下:
#import "YJFeedBackViewController.h"
@interface YJFeedBackViewController ()<UITextViewDelegate>
@property (weak,
nonatomic) IBOutlet
UILabel *placeHolder;
@property (weak,
nonatomic) IBOutlet
UIButton *commitButton;
@property (weak,
nonatomic) IBOutlet
UITextView *feedBackTextView;
@property (weak,
nonatomic) IBOutlet
UILabel *stirngLenghLabel;
@end
@implementation YJFeedBackViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.title =
@"反馈意见";
self.automaticallyAdjustsScrollViewInsets =
NO;
self.feedBackTextView.delegate =
self;
self.placeHolder.userInteractionEnabled =
NO;
self.commitButton.userInteractionEnabled =
NO;
self.feedBackTextView.layer.borderWidth =
0.5;
self.feedBackTextView.layer.borderColor = [UIColor
lightGrayColor].CGColor;
// Do any additional setup after loading the view from its nib.
}
//正在改变
- (void)textViewDidChange:(UITextView *)textView
{
FDLog(@"%@", textView.text);
self.placeHolder.hidden =
YES;
//允许提交按钮点击操作
self.commitButton.backgroundColor =
FDMainColor;
self.commitButton.userInteractionEnabled =
YES;
//实时显示字数
self.stirngLenghLabel.text = [NSString
stringWithFormat:@"%lu/100", (unsigned
long)textView.text.length];
//字数限制操作
if (textView.text.length >=
100) {
textView.text = [textView.text
substringToIndex:100];
self.stirngLenghLabel.text =
@"100/100";
}
//取消按钮点击权限,并显示提示文字
if (textView.text.length ==
0) {
self.placeHolder.hidden =
NO;
self.commitButton.userInteractionEnabled =
NO;
self.commitButton.backgroundColor = [UIColor
lightGrayColor];
}
}
相关文章推荐
- ios开发之--实现TextView提示文字并且输入字数限制
- iOS - 实现TextView提示文字并且输入字数限制
- iOS TextView限制输入字数且屏蔽Emoji表情符号
- textfield/textview的子类,实现功能:几种不同类型的最大输入字符串字数限制,剩余可输入字符数,emoji表情过滤
- 【IOS】TextView里限制输入字数的方法
- iOS TextView限制输入字数且屏蔽Emoji表情符号
- iOS【textView或者textField文本输入字数限制】
- Android AutoCompleteTextView控件实现类似百度搜索提示,限制输入数字长度
- editText提示输入文字字数,同时限制字数和清空输入内容
- EditText 设置字数限制,超过字数限制时 toast 提示并且输入无效
- iOS 怎么限制textView和textField的输入字数
- 我的Android进阶之旅------>Android之AutoCompleteTextView输入汉字拼音首字母实现过滤提示(支持多音字)
- inupt textarea提示文字(点击消失,不输入恢复)及限制字数
- Android实现限制EditText输入文字的数量
- android AutoCompleteTextView 实现输入提示,类似百度支持输入拼音提示中文(gray)
- EditText限制输入字数,并且禁止输入表情符号
- AutoCompleteTextView输入汉字拼音首字母实现过滤提示
- AutoCompleteTextView输入汉字拼音首字母实现过滤提示(支持多音字,Filterable的使用)
- AutoCompleteTextView输入汉字拼音首字母实现过滤提示(支持多音字)
- TextView里限制输入字数的方法