ios自定义TextView,实时监控输入字数的改变,计数以及超出时键盘自动隐藏!

在我们工作中经常会填写一些文字的时候,这时候大多数的地方都需要我们来设置输入的字符数目,需要了解这方面的不妨可以看看这篇文章。
1.在做这些之前,我们要知道TextView都有哪些属性,
2.我们会用到哪些,这些东西都需要我们一天天的学习积累。
现在我给大家看下我写的一个demo(仅供参考)
效果图如下:

ios自定义TextView,实时监控输入字数的改变,计数以及超出时键盘自动隐藏!_第1张图片
屏幕快照 2016-07-12 17.02.11.png

首先自定义一个TextView(GZTextView的.h代码)

#import 
@interface GZTextView : UITextView
@property (nonatomic, strong) UILabel * GZplaceHolderLabel;
@property (nonatomic, copy) NSString * GZplaceholder;
@property (nonatomic, strong) UIColor * GZplaceholderColor;
/**
 *  检测当输入时改变字体颜色
 *  @param notification 监测
 */
- (void)GZtextChanged:(NSNotification * )notification;
@end

GZTextView的.m代码

#import "GZTextView.h"
#define GZRGBCOLOR(r,g,b) [UIColor colorWithRed:r/255.0  green:g/255.0 blue:b/255.0 alpha:1]
@implementation GZTextView
-(instancetype)initWithFrame:(CGRect)frame{    
 if (self = [super initWithFrame:frame]) {
    [self setPlaceholder:@"轻斟浅醉17"];/*可写可不写*/
    self.layer.cornerRadius = 10.0f;
    self.layer.borderWidth = 1;
    self.GZplaceholderColor = GZRGBCOLOR(0x89, 0x89, 0x89);
    self.editable = YES;
   [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(GZtextChanged:)   name:UITextViewTextDidChangeNotification object:nil];
 }
 return self;
  }
   -(void)setPlaceholder:(NSString *)placeholder{
    if (_GZplaceholder != placeholder) {  
        _GZplaceholder = placeholder;
        [self.GZplaceHolderLabel removeFromSuperview];  
        self.GZplaceHolderLabel = nil; 
        [self setNeedsDisplay];
    }  
}
- (void)GZtextChanged:(NSNotification *)notification{  
    if ([[self GZplaceholder] length] == 0) {
       return;
    }
    if ([[self text] length] == 0) {
        [[self viewWithTag:666] setAlpha:1.0];
    }
    else{
        [[self viewWithTag:666] setAlpha:0];
    }
}
-(void)drawRect:(CGRect)rect{
       [super drawRect:rect];   
    if ([[self GZplaceholder] length] > 0) {
        if (_GZplaceHolderLabel == nil) {
            _GZplaceHolderLabel = [[UILabel    alloc]initWithFrame:CGRectMake(8, 8, self.bounds.size.width - 16, 0)];
            _GZplaceHolderLabel.lineBreakMode = NSLineBreakByWordWrapping;
        _GZplaceHolderLabel.numberOfLines = 0;
        _GZplaceHolderLabel.font = self.font;
        _GZplaceHolderLabel.backgroundColor = [UIColor clearColor];
        _GZplaceHolderLabel.textColor = self.GZplaceholderColor;
        _GZplaceHolderLabel.alpha = 0;
        _GZplaceHolderLabel.tag = 666;
        [self addSubview:_GZplaceHolderLabel];
    }
    _GZplaceHolderLabel.text = self.GZplaceholder;
    [_GZplaceHolderLabel sizeToFit];
    [self sendSubviewToBack:_GZplaceHolderLabel];
   }
    if ([[self text] length] == 0 && [[self GZplaceholder] length] >0) {
        [[self viewWithTag:666] setAlpha:1.0];
    } 
}
@end

接下来是到我们的控制器页面显示

#import "ViewController.h"
#import "GZTextView.h"
#define GZScreenWidth [UIScreen mainScreen].bounds.size.width
@interface ViewController ()
@property(strong, nonatomic)GZTextView * textView;
@property(strong, nonatomic)UILabel * numLabel;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.title = @"刚子的UITextView";
_textView = [[GZTextView alloc]initWithFrame:CGRectMake(20, 100,  GZScreenWidth - 40, 150)];
_textView.backgroundColor = [UIColor whiteColor];
_textView.delegate = self;
_textView.font = [UIFont systemFontOfSize:14.f];
_textView.textColor = [UIColor blackColor];
_textView.textAlignment = NSTextAlignmentLeft;
_textView.GZplaceholder = @"请输入少于30字的介绍";
[self.view addSubview:_textView];
UILabel *GZLab = [[UILabel alloc]initWithFrame:CGRectMake(GZScreenWidth * 0.1, _textView.frame.origin.y + 170, GZScreenWidth * 0.8, 150)];
GZLab.numberOfLines = 0 ;
GZLab.textColor = [UIColor orangeColor];
[self.view addSubview:GZLab];
GZLab.text = @"新浪微博:轻斟浅醉17\n\ngithub: https://github.com/Gang679 \n\n:http://www.jianshu.com/users/ab83189244d9/latest_articles";
_numLabel = [[UILabel alloc] initWithFrame:CGRectMake(CGRectGetMaxX(_textView.frame)-90, CGRectGetMaxY(_textView.frame)+6, 80, 21)];
_numLabel.textAlignment = NSTextAlignmentRight;
_numLabel.text = @"30";
_numLabel.backgroundColor = [UIColor whiteColor];
[self.view addSubview:_numLabel];    
}
#pragma mark textField的字数限制
//在这个地方计算输入的字数
- (void)textViewDidChange:(UITextView *)textView
{
    NSInteger wordCount = textView.text.length;
self.numLabel.text = [NSString stringWithFormat:@"%ld/30",  (long)wordCount];
   [self wordLimit:textView];
}
#pragma mark 超过30字不能输入
-(BOOL)wordLimit:(UITextView *)text{
if (text.text.length < 30) {
    NSLog(@"%ld",text.text.length);
    self.textView.editable = YES;
}
else{
    self.textView.editable = NO; 
}
return nil;
}

源码入口:https://github.com/Gang679/GZTextView github star一下
欢迎大家交流

你可能感兴趣的:(ios自定义TextView,实时监控输入字数的改变,计数以及超出时键盘自动隐藏!)