ios自定義TextView坑质,實時監(jiān)控輸入字數的改變合武,計數以及超出時鍵盤自動隱藏!

在我們工作中經常會填寫一些文字的時候涡扼,這時候大多數的地方都需要我們來設置輸入的字符數目稼跳,需要了解這方面的不妨可以看看這篇文章。
1.在做這些之前吃沪,我們要知道TextView都有哪些屬性汤善,
2.我們會用到哪些,這些東西都需要我們一天天的學習積累。
現在我給大家看下我寫的一個demo(僅供參考)
效果圖如下:

屏幕快照 2016-07-12 17.02.11.png

首先自定義一個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 監(jiān)測
 */
- (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.reibang.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一下
歡迎大家交流

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
  • 序言:七十年代末红淡,一起剝皮案震驚了整個濱河市不狮,隨后出現的幾起案子,更是在濱河造成了極大的恐慌在旱,老刑警劉巖摇零,帶你破解...
    沈念sama閱讀 218,122評論 6 505
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現場離奇詭異颈渊,居然都是意外死亡遂黍,警方通過查閱死者的電腦和手機,發(fā)現死者居然都...
    沈念sama閱讀 93,070評論 3 395
  • 文/潘曉璐 我一進店門俊嗽,熙熙樓的掌柜王于貴愁眉苦臉地迎上來雾家,“玉大人,你說我怎么就攤上這事绍豁⌒具郑” “怎么了?”我有些...
    開封第一講書人閱讀 164,491評論 0 354
  • 文/不壞的土叔 我叫張陵竹揍,是天一觀的道長敬飒。 經常有香客問我,道長芬位,這世上最難降的妖魔是什么无拗? 我笑而不...
    開封第一講書人閱讀 58,636評論 1 293
  • 正文 為了忘掉前任,我火速辦了婚禮昧碉,結果婚禮上英染,老公的妹妹穿的比我還像新娘。我一直安慰自己被饿,他們只是感情好四康,可當我...
    茶點故事閱讀 67,676評論 6 392
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著狭握,像睡著了一般闪金。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上论颅,一...
    開封第一講書人閱讀 51,541評論 1 305
  • 那天哎垦,我揣著相機與錄音,去河邊找鬼恃疯。 笑死漏设,一個胖子當著我的面吹牛,可吹牛的內容都是我干的澡谭。 我是一名探鬼主播,決...
    沈念sama閱讀 40,292評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼蛙奖!你這毒婦竟也來了潘酗?” 一聲冷哼從身側響起,我...
    開封第一講書人閱讀 39,211評論 0 276
  • 序言:老撾萬榮一對情侶失蹤雁仲,失蹤者是張志新(化名)和其女友劉穎仔夺,沒想到半個月后,有當地人在樹林里發(fā)現了一具尸體攒砖,經...
    沈念sama閱讀 45,655評論 1 314
  • 正文 獨居荒郊野嶺守林人離奇死亡缸兔,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內容為張勛視角 年9月15日...
    茶點故事閱讀 37,846評論 3 336
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發(fā)現自己被綠了吹艇。 大學時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片惰蜜。...
    茶點故事閱讀 39,965評論 1 348
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖受神,靈堂內的尸體忽然破棺而出抛猖,到底是詐尸還是另有隱情,我是刑警寧澤鼻听,帶...
    沈念sama閱讀 35,684評論 5 347
  • 正文 年R本政府宣布财著,位于F島的核電站,受9級特大地震影響撑碴,放射性物質發(fā)生泄漏。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點故事閱讀 41,295評論 3 329
  • 文/蒙蒙 一粱年、第九天 我趴在偏房一處隱蔽的房頂上張望驼壶。 院中可真熱鬧,春花似錦廉嚼、人聲如沸玫镐。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,894評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽恐似。三九已至,卻和暖如春傍念,著一層夾襖步出監(jiān)牢的瞬間矫夷,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 33,012評論 1 269
  • 我被黑心中介騙來泰國打工憋槐, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留双藕,地道東北人。 一個月前我還...
    沈念sama閱讀 48,126評論 3 370
  • 正文 我出身青樓阳仔,卻偏偏與公主長得像忧陪,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當晚...
    茶點故事閱讀 44,914評論 2 355

推薦閱讀更多精彩內容