前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >自定义UITextView

自定义UITextView

作者头像
hrscy
发布2018-08-30 12:19:36
5320
发布2018-08-30 12:19:36
举报
文章被收录于专栏:hrscyhrscy

自定义UITextView,带有placeholeder,可以设置placeholeder文字的大小和颜色。

如图:

自定义UITextView

集成简单,只需在所用到的控制器中导入YMTextView即可,下面是示例代码:

在ViewController.m中

代码语言:javascript
复制
#import "ViewController.h"
#import "YMTextView.h"

@interface ViewController ()
//输入控件
@property (nonatomic, weak) YMTextView *textView;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setupLabel];
    
    //设置输入控件
    [self setupTextView];
    
}

-(void)setupLabel {
    UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 40, self.view.frame.size.width, 44)];
    titleLabel.text = @"自定义textView控件";
    titleLabel.textAlignment = NSTextAlignmentCenter;
    titleLabel.textColor = [UIColor blackColor];
    [self.view addSubview:titleLabel];
}

//添加输入控件
-(void)setupTextView {
    // 在这个控制器中,textView的contentInset.top默认会等于64
    YMTextView *textView = [[YMTextView alloc] init];
    textView.frame = CGRectMake(0, 100, self.view.frame.size.width, 200);
    textView.backgroundColor = [UIColor lightGrayColor];
    textView.font = [UIFont systemFontOfSize:15];
    //设置占位文字
    textView.placeholder = @"这里是占位文字...";
    //设置占位文字颜色
//    textView.placeholderColor = [UIColor redColor];
    [self.view addSubview:textView];
    self.textView = textView;
    //监听通知
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(textDidChange) name:UITextViewTextDidChangeNotification object:textView];
}
/**
 *  监听文字改变
 */
-(void)textDidChange {
    
    if (self.textView.hasText) {
        NSLog(@"文字发生改变----%@",self.textView.text);
    }
}

-(void)dealloc {
    //移除通知
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end

在YMTextView.h中:

代码语言:javascript
复制
#import <UIKit/UIKit.h>

@interface YMTextView : UITextView
/**
 *  占位文字
 */
@property (nonatomic, copy) NSString *placeholder;
/**
 *  占位文字颜色
 */
@property (nonatomic, strong) UIColor *placeholderColor;

@end

在YMTextView.m中:

代码语言:javascript
复制
#import "YMTextView.h"

@implementation YMTextView


- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // 当UITextView的文字发生改变时,UITextView自己会发出一个UITextViewTextDidChangeNotification通知
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(textDidChange) name:UITextViewTextDidChangeNotification object:self];
    }
    return self;
}

/**
 *  监听文字改变
 */
-(void)textDidChange {
    
    //重绘
    [self setNeedsDisplay];
    
}

-(void)setPlaceholder:(NSString *)placeholder {
    _placeholder = placeholder;
    // setNeedsDisplay会在下一个消息循环时刻,调用drawRect:
    [self setNeedsDisplay];
}

-(void)setText:(NSString *)text {
    [super setText:text];
    // setNeedsDisplay会在下一个消息循环时刻,调用drawRect:
    [self setNeedsDisplay];
}

-(void)setFont:(UIFont *)font {
    [super setFont:font];
    // setNeedsDisplay会在下一个消息循环时刻,调用drawRect:
    [self setNeedsDisplay];
}

- (void)drawRect:(CGRect)rect {
    
    // 如果有输入文字,就直接返回,不画占位文字
    if (self.hasText) return;
    //设置文字属性
    NSMutableDictionary *attributes = [NSMutableDictionary dictionary];
    attributes[NSFontAttributeName] = self.font;
    attributes[NSForegroundColorAttributeName] = self.placeholderColor ? self.placeholderColor : [UIColor grayColor];
    //画文字
    CGFloat x = 5;
    CGFloat width = rect.size.width -2 * x;
    CGFloat y = 8;
    CGFloat height = rect.size.height - 2 * y;
    CGRect placeholderRect = CGRectMake(x, y, width, height);
    [self.placeholder drawInRect:placeholderRect withAttributes:attributes];
}

-(void)dealloc {
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

@end

代码地址

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2016.01.24 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 代码地址
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档