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

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

效果图如下:


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

#import <UIKit/UIKit.h>
@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 ()<UITextViewDelegate>
@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上星级一下
欢迎大家交流





  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值