UITextView添加placeholder

//本文采用的代码量比较少的一种写法,测试中未出现bug
#import "ViewController.h"
#import "Masonry.h"

@interface ViewController ()
@property (nonatomic, strong) UITextView *textView;
@end
@implementation ViewController
- (void)viewDidLoad {
    [self.view addSubview:self.textView];
    [self.textView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.view).offset(100);
        make.height.equalTo(@48);
        make.right.equalTo(self.inputView).offset(-20);
        make.left.equalTo(self.inputView).offset(20);
    }];

}
-(UITextView *)textView{
    if (!_textView) {
        _textView = [[UITextView alloc] init];
        _textView.font = [UIFont systemFontOfSize:14];
        _textView.textColor = MIN_BLACKTITLE_COLOR;
        UILabel *placeHolderLabel = [[UILabel alloc] init];
        placeHolderLabel.text = @"请输入您的评论";
        placeHolderLabel.textColor = [UIColor lightGrayColor];
        placeHolderLabel.font = [UIFont systemFontOfSize:14];
        [placeHolderLabel sizeToFit];
        [_textView addSubview:placeHolderLabel];
        [_textView setValue:placeHolderLabel forKey:@"_placeholderLabel"];
    }
    return _textView;
}
@end
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 实现方法:通过监听UITextView的文本变化,重绘UITextView将placeholder绘制出来。 支持...
    大哥陈阅读 2,019评论 0 53
  • 在实际项目开发中经常有需要在UITextView中添加提示用户输入的一些文字,类似UITextField的Plac...
    海到尽头天为岸阅读 687评论 0 4
  • 使用UITextView 代理方法
    出门请右拐阅读 252评论 0 0
  • 离开我的家乡连云港已经四年了。 记得多年以前,我广西的几个同学因心中慕着这孙猴子都要念念留恋的山海风光,不远万里的...
    曰日如歌阅读 391评论 0 1
  • “儿子,我的腿疼了有一阵子了,你什么时候有时间陪我去医院。”她犹豫了很久,还是拿起来电话。 我最近很忙,你还是等我...
    米灿灿88阅读 448评论 0 1