在我們工作中經常會填寫一些文字的時候涡扼,這時候大多數的地方都需要我們來設置輸入的字符數目稼跳,需要了解這方面的不妨可以看看這篇文章。
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一下
歡迎大家交流