ViewController.m
#import "JYZTextView.h"
#define kTextBorderColor RGBCOLOR(227,224,216)
#undef RGBCOLOR
#define RGBCOLOR(r,g,b) [UIColor colorWithRed:r/255.0 green:g/255.0 blue:b/255.0 alpha:1]
@interface ViewController ()<UITextViewDelegate>
@property(strong, nonatomic)JYZTextView * textView;
@property(strong, nonatomic)UILabel * numLabel;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
//自定义textView
_textView = [[JYZTextView alloc]initWithFrame:CGRectMake(30, 100, self.view.frame.size.width - 60, 100)];
_textView.backgroundColor = [UIColor whiteColor];
_textView.delegate = self;
_textView.font = [UIFont systemFontOfSize:14.f];
_textView.textColor = [UIColor blackColor];
_textView.textAlignment = NSTextAlignmentLeft;
_textView.placeholder = @"请输入大于10少于300百字的评价";
[self.view addSubview:_textView];
_numLabel = [[UILabel alloc] initWithFrame:CGRectMake(CGRectGetMaxX(_textView.frame)-90, CGRectGetMaxY(_textView.frame)+6, 80, 21)];
_numLabel.textAlignment = NSTextAlignmentRight;
_numLabel.text = @"300";
_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/300",(long)wordCount];
[self wordLimit:textView];
}
#pragma mark 超过300字不能输入
-(BOOL)wordLimit:(UITextView *)text{
if (text.text.length < 300) {
NSLog(@"%ld",text.text.length);
self.textView.editable = YES;
}
else{
self.textView.editable = NO;
}
return nil;
}
JYZTextView.h
@interface JYZTextView : UITextView
@property (nonatomic, strong) UILabel * placeHolderLabel;
@property (nonatomic, copy) NSString * placeholder;
@property (nonatomic, strong) UIColor * placeholderColor;
/**
* 检测当输入时改变字体颜色
*
* @param notification 监测
*/
- (void)textChanged:(NSNotification * )notification;
JYZTextView.m
-(instancetype)initWithFrame:(CGRect)frame{
if (self = [super initWithFrame:frame]) {
[self setPlaceholder:@""];
[self setPlaceholderColor:[UIColor lightGrayColor]];
self.layer.cornerRadius = 4.0f;
self.layer.borderColor = kTextBorderColor.CGColor;
self.layer.borderWidth = 0.5;
self.placeholderColor = RGBCOLOR(0x89, 0x89, 0x89);
self.editable = YES;
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(textChanged:) name:UITextViewTextDidChangeNotification object:nil];
}
return self;
}
-(void)setPlaceholder:(NSString *)placeholder{
if (_placeholder != placeholder) {
_placeholder = placeholder;
[self.placeHolderLabel removeFromSuperview];
self.placeHolderLabel = nil;
[self setNeedsDisplay];
}
}
- (void)textChanged:(NSNotification *)notification{
if ([[self placeholder] length] == 0) {
return;
}
if ([[self text] length] == 0) {
[[self viewWithTag:999] setAlpha:1.0];
}
else{
[[self viewWithTag:999] setAlpha:0];
}
}
-(void)drawRect:(CGRect)rect{
[super drawRect:rect];
if ([[self placeholder] length] > 0) {
if (_placeHolderLabel == nil) {
_placeHolderLabel = [[UILabel alloc]initWithFrame:CGRectMake(8, 8, self.bounds.size.width - 16, 0)];
_placeHolderLabel.lineBreakMode = NSLineBreakByWordWrapping;
_placeHolderLabel.numberOfLines = 0;
_placeHolderLabel.font = self.font;
_placeHolderLabel.backgroundColor = [UIColor clearColor];
_placeHolderLabel.textColor = self.placeholderColor;
_placeHolderLabel.alpha = 0;
_placeHolderLabel.tag = 999;
[self addSubview:_placeHolderLabel];
}
_placeHolderLabel.text = self.placeholder;
[_placeHolderLabel sizeToFit];
[self sendSubviewToBack:_placeHolderLabel];
}
if ([[self text] length] == 0 && [[self placeholder] length] >0) {
[[self viewWithTag:999] setAlpha:1.0];
}
}