自定义UITextView

这里我们创建一个继承自UIView,内嵌UITextView的类


  • HJTextView.h

//
//  HJTextView.h
//  HJTextView
//
//  Created by 黄健 on 16/5/27.
//  Copyright © 2016年 黄健. All rights reserved.
//

#import <UIKit/UIKit.h>

@interface HJTextView : UIView

@property (nonatomic, assign) int maxLength;// 最大字数
@property (nonatomic, retain) UIFont *font;// 字体大小,默认14
@property (nonatomic, retain) UIColor *textColor;// 字体颜色
@property (nonatomic, retain) UIColor *placeholderColor;// 提示文字颜色
@property (nonatomic) UIEdgeInsets contentEdgeInsets;// 内间距

@property (nonatomic, copy) NSString *text;// 文本域内容
@property (nonatomic, copy) NSString *placeholder;// 提示文字

@end

  • HJTextView.m

//
//  HJTextView.m
//  HJTextView
//
//  Created by 黄健 on 16/5/27.
//  Copyright © 2016年 黄健. All rights reserved.
//

#import "HJTextView.h"

// 视图的宽高
#define kWidth  self.bounds.size.width
#define kHeight self.bounds.size.height

// 默认内间距
#define kPadding 2

// 文本域宽度
#define kTextViewWidth _textView.bounds.size.width


@interface HJTextView() <UITextViewDelegate>
{
    @private
        UITextView *_textView;// 内嵌文本域
        UILabel *_tipLabel;// 提示文字标签
        UILabel *_numLabel;// 字数限制标签
}

@end

@implementation HJTextView

- (instancetype)initWithFrame:(CGRect)frame
{
    if (self == [super initWithFrame:frame])
    {
        [self initView];
    }
    return self;
}

- (void)initView
{
    self.backgroundColor = [UIColor whiteColor];

    _textView                 = [[UITextView alloc] init];
    _textView.font            = [UIFont systemFontOfSize:14];
    _textView.frame           = CGRectMake(kPadding, kPadding, kWidth - 2 * kPadding, kHeight - 2 * kPadding);
    _textView.delegate        = self;
    _textView.backgroundColor = [UIColor clearColor];
    [self addSubview:_textView];
}

- (void)setFrame:(CGRect)frame
{
    [super setFrame:frame];
    [self initView];
    [self setNeedsDisplay];
}

- (void)setMaxLength:(int)maxLength
{
    _maxLength     = maxLength;

    _numLabel      = [[UILabel alloc] init];
    _numLabel.font = [UIFont systemFontOfSize:13];
    CGFloat w = 100;
    CGFloat h = _numLabel.font.lineHeight + 2;
    CGFloat x = kWidth - w - 7;
    CGFloat y = kHeight - h - 5;
    _numLabel.frame         = CGRectMake(x, y, w, h);
    _numLabel.textAlignment = NSTextAlignmentRight;
    _numLabel.textColor     = [UIColor colorWithWhite:0.8 alpha:1.0];
    [self addSubview:_numLabel];

    _numLabel.text = [NSString stringWithFormat:@"0/%d", maxLength];
}

- (void)setTextColor:(UIColor *)textColor
{
    _textColor = textColor;

    _textView.textColor = textColor;
}

- (void)setPlaceholderColor:(UIColor *)placeholderColor
{
    _placeholderColor = placeholderColor;

    _tipLabel.textColor = _placeholderColor;
    _numLabel.textColor = _placeholderColor;
}

- (void)setPlaceholder:(NSString *)placeholder
{
    _placeholder = placeholder;

    _tipLabel               = [[UILabel alloc] init];
    _tipLabel.font          = _textView.font;
    _tipLabel.text          = _placeholder;
    _tipLabel.textColor     = _placeholderColor ? _placeholderColor : [UIColor colorWithWhite:0.8 alpha:1.0];
    _tipLabel.numberOfLines = 0;
    [_textView addSubview:_tipLabel];
    [_textView sendSubviewToBack:_tipLabel];

    CGSize contentSize = [self contentSize:_placeholder withFont:_tipLabel.font andMaxWith:kTextViewWidth - 5];

    _tipLabel.frame = CGRectMake(5, 8, contentSize.width, contentSize.height);
}

- (CGSize)contentSize:(NSString *)content withFont:(UIFont *)font andMaxWith:(CGFloat)maxWidth
{
    return [content boundingRectWithSize:CGSizeMake(maxWidth, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:font} context:nil].size;
}

- (void)setText:(NSString *)text
{
    _textView.text   = text;
    _tipLabel.hidden = text.length > 0;
}

- (NSString *)text
{
    return _textView.text;
}

- (void)setFont:(UIFont *)font
{
    _textView.font = font;
    _tipLabel.font = font;
}

- (void)setContentEdgeInsets:(UIEdgeInsets)contentEdgeInsets
{
    CGFloat x = contentEdgeInsets.left;
    CGFloat y = contentEdgeInsets.top;
    CGFloat w = kWidth - x - contentEdgeInsets.right;
    CGFloat h = kHeight - y - contentEdgeInsets.bottom;

    _textView.frame = CGRectMake(x, y, w, h);
}

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

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

@end

  • 测试

HJTextView *hjTextView = [[HJTextView alloc] initWithFrame:CGRectMake(20, 50, screenW - 45, 90)];
[self.view addSubview:hjTextView];

hjTextView.font = [UIFont systemFontOfSize:14];
hjTextView.maxLength = 20000;
hjTextView.contentEdgeInsets = UIEdgeInsetsMake(2, 5, 25, 5);
hjTextView.placeholder = @"请输入内容";

  • 运行

这里写图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值