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

IOS开发之——意见反馈UITextView的使用

时间:2016-04-20 09:59:34      阅读:321      评论:0      收藏:0      [点我收藏+]

标签:

@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;
    
}

IOS开发之——意见反馈UITextView的使用

标签:

原文地址:http://www.cnblogs.com/linxiu-0925/p/5411356.html

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