标签:
@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;
}
标签:
原文地址:http://www.cnblogs.com/linxiu-0925/p/5411356.html