码迷,mamicode.com
首页 > 其他好文 > 详细

自定义textView限制字数

时间:2016-02-15 11:59:51      阅读:186      评论:0      收藏:0      [点我收藏+]

标签:

  

 

 

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];

    }

    

}

 

自定义textView限制字数

标签:

原文地址:http://www.cnblogs.com/qq463833124/p/5190089.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!