@interface DMFeedbackViewController ()<UITextViewDelegate,UIAlertViewDelegate>
@property (nonatomic, strong) UITextView *feedbackTextView;//意见反馈输入框
@property (nonatomic, strong) UILabel *mostLabel;//最多还可以输入
@property (nonatomic, strong) UIButton *submitBt;//提交
@property (nonatomic, strong) UILabel *feedbackNoteLabel;//在此输入反馈意见
@end
判断用户是否输入文字,feedbackNoteLabel文字显示或者隐藏
利用通知解决:
[[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(textViewChange) name:UITextViewTextDidChangeNotification object:nil];
//释放通知
-(void)dealloc
{
// debugMethod();
[[NSNotificationCenter defaultCenter]removeObserver:self name:UITextViewTextDidChangeNotification object:nil];
}
-(void)textViewChange
{
if (_feedbackTextView.text.length== 0) {
[_feedbackNoteLabel setHidden:NO];
}else
{
[_feedbackNoteLabel setHidden:YES];
}
}
由于设计需要,uitextview的输入框特别长,提交按钮在最底部,为了方便用户体验,添加了拖拽也就是滑动手势
UIPanGestureRecognizer *panGestureRecognizer= [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(handlePanGestures:)];
panGestureRecognizer.minimumNumberOfTouches = 1;
panGestureRecognizer.maximumNumberOfTouches = 5;
[self.view addGestureRecognizer:panGestureRecognizer];
#pragma mark --滑动空白区域收起键盘
-(void)handlePanGestures:(UIPanGestureRecognizer *)sender{
[self.view endEditing:YES];
}
####您最多还可以输入多个字的代理方法
- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text
{
NSString *temp = [textView.text stringByReplacingCharactersInRange:range withString:text];
if (temp.length== 0) {
//输入文字为0,提交按钮触摸事件隐藏
[_submitBt setBackgroundColor:DMRGB(235, 235, 240)];
[_submitBt setTitleColor:DMRGB(210, 210, 210) forState:UIControlStateNormal];
_submitBt.userInteractionEnabled = NO;
}
else
{
[_submitBt setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
_submitBt.backgroundColor = [UIColor colorWithRed:65.0/255 green:109.0/255 blue:218.0/255 alpha:1];
_submitBt.userInteractionEnabled = YES;
}
NSString *str = [NSString stringWithFormat:@"%lu",100-temp.length];
_mostLabel.attributedText =[self getString:[NSString stringWithFormat:@"您最多还可以输入%lu个字",100-temp.length] WithFontSize:DMFontSize14 WithTextColer:DMRGB(153, 153, 153) otherTextColer:DMRGB(237, 95, 95) WithRange:NSMakeRange(8,str.length)];
if (temp.length >= 100) { //如果输入超过规定的字数100,就不再让输入
return NO;
}
return YES;
}
-(NSMutableAttributedString*)getString:(NSString*)str WithFontSize:(CGFloat)fontSize WithTextColer:(UIColor *)color otherTextColer:(UIColor *)othercolor WithRange:(NSRange)strRange
{
NSMutableAttributedString *attriString = [[NSMutableAttributedString alloc] initWithString:str];
NSRange mainRange=NSMakeRange(0, str.length);
[attriString setAttributes: @{NSFontAttributeName: [UIFont systemFontOfSize:fontSize],
NSForegroundColorAttributeName: color} range:mainRange];
[attriString setAttributes: @{NSFontAttributeName: [UIFont systemFontOfSize:fontSize],
NSForegroundColorAttributeName: othercolor} range:strRange];
return attriString;
}
本来以为解决了,结果测试发现了一个问题:当输入拼音的时候,那个提示语100并没有被减去。
百度了资料说:
这样在输入全部是英文的情况下是可以的. 但是当输入是中文时, 由于shouldChangeCharactersInRange判断的是当前键盘的字符数,最终采用了通知方法解决。
方法如下:
把那个shouldChangeCharactersInRange方法注释掉。代码如下:
- (void)initParameters
{
[super initParameters];
[[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(textViewChange) name:UITextViewTextDidChangeNotification object:nil];
//关于输入拼音和英文状态下如何正确的统计输入的文字字数
[[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(textViewEditChange:) name:UITextViewTextDidChangeNotification object:_feedbackTextView];
}
-(void)dealloc
{
// debugMethod();
[[NSNotificationCenter defaultCenter]removeObserver:self name:UITextViewTextDidChangeNotification object:nil];
[[NSNotificationCenter defaultCenter]removeObserver:self name:UITextViewTextDidChangeNotification object:_feedbackTextView];
}
#pragma mark 通知 方法
-(void)textViewChange
{
if (_feedbackTextView.text.length== 0) {
[_feedbackNoteLabel setHidden:NO];
}else
{
[_feedbackNoteLabel setHidden:YES];
}
}
//关于输入拼音和英文状态下如何正确的统计输入的文字字数
-(void)textViewEditChange:(NSNotification *)obj{
UITextView *textView = (UITextView *)obj.object;
NSString *temp = textView.text;
if (temp.length== 0) {
[_submitBt setBackgroundColor:DMRGB(235, 235, 240)];
[_submitBt setTitleColor:DMRGB(210, 210, 210) forState:UIControlStateNormal];
_submitBt.userInteractionEnabled = NO;
}
else
{
[_submitBt setTitleColor:DMRGB(255, 255, 255) forState:UIControlStateNormal];
_submitBt.backgroundColor = [UIColor colorWithRed:240.0/255 green:77.0/255 blue:77.0/255 alpha:1];
_submitBt.userInteractionEnabled = YES;
}
NSString *lang = [[UITextInputMode currentInputMode] primaryLanguage]; // 键盘输入模式
if ([lang isEqualToString:@"zh-hans"]) { // 简体中文输入,包括简体拼音,健体五笔,简体手写
NSString *str = [NSString stringWithFormat:@"%lu",100-temp.length];
_mostLabel.attributedText =[self getString:[NSString stringWithFormat:@"您最多还可以输入%lu个字",100-temp.length] WithFontSize:DMFontSize14 WithTextColer:DMSecondaryTextColor otherTextColer:DMRGB(237, 95, 95) WithRange:NSMakeRange(8,str.length)];
UITextRange *selectedRange = [textView markedTextRange];
//获取高亮部分
UITextPosition *position = [textView positionFromPosition:selectedRange.start offset:0];
if (!position) { //如果没有高亮选择的字,则对已经输入的文字进行字数统计和限制
if (temp.length>100) {
textView.text = [temp substringToIndex:100];
}
}
else{ //有高亮选择的字,不对文字进行字数统计和限制
}
}
else{// 中文输入法以外的直接对其统计限制即可,不考虑其他语种情况
NSString *str = [NSString stringWithFormat:@"%lu",100-temp.length];
_mostLabel.attributedText =[self getString:[NSString stringWithFormat:@"您最多还可以输入%lu个字",100-temp.length] WithFontSize:DMFontSize14 WithTextColer:DMSecondaryTextColor otherTextColer:DMRGB(237, 95, 95) WithRange:NSMakeRange(8,str.length)];
if (temp.length>100) { //超过最大字符100,不让键盘输入
textView.text = [temp substringToIndex:100];
}
}
}就可以了。
最后一步:关于emoji表情不能被输入,怎么解决?
如下:
#pragma mark - 代理 //用户完成编辑后所提示的事
-(void)textViewDidEndEditing:(UITextView *)textView{
if ([self stringContainsEmoji:self.feedbackTextView.text]) {
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"不能带有表情符号,请输入中文" message:nil delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
[alertView show];
}
}
#pragma mark 判断NSString字符串是否包含emoji表情
- (BOOL)stringContainsEmoji:(NSString *)string{
__block BOOL returnValue = NO;
[string enumerateSubstringsInRange:NSMakeRange(0, [string length])
options:NSStringEnumerationByComposedCharacterSequences
usingBlock:^(NSString *substring, NSRange substringRange, NSRange enclosingRange, BOOL *stop) {
const unichar hs = [substring characterAtIndex:0];
if (0xd800 <= hs && hs <= 0xdbff) {
if (substring.length > 1) {
const unichar ls = [substring characterAtIndex:1];
const int uc = ((hs - 0xd800) * 0x400) + (ls - 0xdc00) + 0x10000;
if (0x1d000 <= uc && uc <= 0x1f77f) {
returnValue = YES;
}
}
} else if (substring.length > 1) {
const unichar ls = [substring characterAtIndex:1];
if (ls == 0x20e3) {
returnValue = YES;
}
} else {
if (0x2100 <= hs && hs <= 0x27ff) {
returnValue = YES;
} else if (0x2B05 <= hs && hs <= 0x2b07) {
returnValue = YES;
} else if (0x2934 <= hs && hs <= 0x2935) {
returnValue = YES;
} else if (0x3297 <= hs && hs <= 0x3299) {
returnValue = YES;
} else if (hs == 0xa9 || hs == 0xae || hs == 0x303d || hs == 0x3030 || hs == 0x2b55 || hs == 0x2b1c || hs == 0x2b1b || hs == 0x2b50) {
returnValue = YES;
}
}
}];
return returnValue;
}