自定义UITextView控件

在项目中遇到了一个大量使用UITextView的控件,但是与系统自带的UITextView又有点不一样,需要增加提示语和字数限制,于是自己封装了一个简单的自定义UITextView.

一、封装一个CustomTextView类

1、新建一个类,继承于UIView,取名为CustomTextView。

2、在CustomTextView的头文件中添加如下代码:

#import <UIKit/UIKit.h>

@interface CustomTextView : UIView

@property (nonatomic, assign) NSInteger maxLength;      //可输入字数个数
@property (nonatomic, strong) NSString *placeholder;    //提示语

@end

3、在CustomTextView的源文件中添加如下代码:

#define kCornerRadius 5.0

#import "CustomTextView.h"

@interface CustomTextView ()<UITextViewDelegate>
{
    UILabel *labPlaceholder;
    UILabel *numWords;
}

@end

@implementation CustomTextView

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {

        UIView *background = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)];
        background.layer.cornerRadius = kCornerRadius;
        background.layer.borderWidth = 1.0f;
        background.layer.borderColor = [UIColor groupTableViewBackgroundColor].CGColor;
        [self addSubview:background];

        UITextView *textView = [[UITextView alloc] initWithFrame:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height-20)];
        textView.delegate = self;
        [background addSubview:textView];

        labPlaceholder = [[UILabel alloc] initWithFrame:CGRectMake(5, 5, 200, 20)];
        labPlaceholder.textColor = [UIColor grayColor];
        labPlaceholder.font = [UIFont systemFontOfSize:15.0];
        [background addSubview:labPlaceholder];

        numWords = [[UILabel alloc] initWithFrame:CGRectMake(self.frame.size.width-75, CGRectGetMaxY(textView.frame), 70, 20)];
        numWords.textAlignment = NSTextAlignmentRight;
        numWords.font = [UIFont systemFontOfSize:15.0];
        numWords.textColor = [UIColor grayColor];
        [background addSubview:numWords];

    }
    return self;
}

- (void)setMaxLength:(NSInteger)maxLength {
    _maxLength = maxLength;
    numWords.text = [NSString stringWithFormat:@"0/%ld",(long)_maxLength];
}

- (void)setPlaceholder:(NSString *)placeholder
{
    _placeholder = placeholder;
    labPlaceholder.text = _placeholder;
}

- (void)textViewDidChange:(UITextView *)textView {
    numWords.text = [NSString stringWithFormat:@"%ld/%ld",(long)textView.text.length,(long)_maxLength];
    labPlaceholder.hidden = (textView.text.length >0);
}

- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text
{
    NSInteger length = textView.text.length - range.length + text.length;
    return length <= _maxLength;
}

@end

二、使用CustomTextView

1、在您需要的类中添加头文件:

#import "CustomTextView.h"

2、使用CustomTextView,只需添加如下代码:

- (void)viewDidLoad {
    [super viewDidLoad];

    CustomTextView *customTextView1 = [[CustomTextView alloc] initWithFrame:CGRectMake(10, 20, self.view.frame.size.width-20, 100)];
    customTextView1.placeholder = @"测试1";
    customTextView1.maxLength = 100;
    [self.view addSubview:customTextView1];

    CustomTextView *customTextView2 = [[CustomTextView alloc] initWithFrame:CGRectMake(10, 140, self.view.frame.size.width-20, 100)];
    customTextView2.placeholder = @"测试2";
    customTextView2.maxLength = 100;
    [self.view addSubview:customTextView2];
}

三、效果图

自定义UITextView控件_第1张图片

你可能感兴趣的:(ios,UITextView)