oc -- 自定义textview

前言:系统自带的textview是没有添加占位文字的功能的,我们可以通过自定义即可实现此功能

  • 自定义textview
#import <UIKit/UIKit.h>

@interface TextView : UITextView
/** 占位文字 */
@property(nonatomic, copy) NSString *placeholder;
/** 占位文字颜色 */
@property(nonatomic, strong) UIColor *placeholderColor;
@end
#import "TextView.h"

@implementation TextView

- (instancetype)initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:frame]) {
        // 设置默认字体
        self.font = [UIFont systemFontOfSize:17];
        // 设置默认颜色
        self.placeholderColor = [UIColor grayColor];
        // 使用通知监听文字改变
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(textDidChange:) name:UITextViewTextDidChangeNotification object:self];
    }
    return self;
}
- (void)textDidChange:(NSNotification *)note
{
    // 会重新调用drawRect:方法
    [self setNeedsDisplay];
}
- (void)dealloc
{
    //移除通知
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}
// 每次调用drawRect:方法,都会将以前画的东西清除掉
- (void)drawRect:(CGRect)rect
{
    // 如果有文字,就直接返回,不需要画占位文字
    if (self.hasText) return;
    // 属性
    NSMutableDictionary *attrs = [NSMutableDictionary dictionary];
    attrs[NSFontAttributeName] = self.font;
    attrs[NSForegroundColorAttributeName] = self.placeholderColor;
    // 画文字 光标与文字不一致,设置尺寸
    rect.origin.x = 5;
    rect.origin.y = 8;
    rect.size.width -= 2 * rect.origin.x;
    [self.placeholder drawInRect:rect withAttributes:attrs];
}
- (void)layoutSubviews
{
    [super layoutSubviews];
    
    [self setNeedsDisplay];
}
#pragma mark - setter
//重写setter方法调用setNeedsDisplay方法刷新
- (void)setPlaceholder:(NSString *)placeholder
{
    _placeholder = [placeholder copy];
    
    [self setNeedsDisplay];
}
- (void)setPlaceholderColor:(UIColor *)placeholderColor
{
    _placeholderColor = placeholderColor;
    
    [self setNeedsDisplay];
}
- (void)setFont:(UIFont *)font
{
    [super setFont:font];
    
    [self setNeedsDisplay];
}
- (void)setText:(NSString *)text
{
    [super setText:text];
    
    [self setNeedsDisplay];
}
- (void)setAttributedText:(NSAttributedString *)attributedText
{
    [super setAttributedText:attributedText];
    
    [self setNeedsDisplay];
}


@end

  • 在外部调用
#import "ViewController.h"
#import "TextView.h"
@interface ViewController ()
@property(nonatomic, strong) TextView *textView;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    TextView *textView = [[TextView alloc] init];
    textView.frame = self.view.bounds;
    //设置可以上下拖动
    textView.alwaysBounceVertical = YES;
    textView.delegate = self;
    textView.placeholder = @"我是占位文字....我是占位文字....我是占位文字....我是占位文字....我是占位文字....我是占位文字....哈哈哈";
    [self.view addSubview:textView];
    self.textView = textView;
    
  
}


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


@end

有没有帮到你呢?😁
(欢迎大家对不合适的地方进行指正,看完觉得有帮到你给点个赞👍吧)

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 173,532评论 25 708
  • 现在刚好处在这个尴尬的年龄 心里会开始在关心爸妈 却不愿说出口 想多陪陪爸妈 却又无法控制自己经常去按手机玩电脑和...
    北七海阅读 215评论 0 1
  • 梁静茹有一首歌叫《三寸日光》 里面的歌词是这样写的: 希望我爱的人健康 个性很善良 大大手掌能包容我小小的倔强 你...
    Alice_cyy阅读 743评论 7 3
  • 第一次尝试使用简书,就来谈谈我平生最大的爱好手帐好了 手帐本的选择很多,跟圈里那些手帐大神相比,学习了2年仍在学习...
    沫沫的沫就是_旖沫_的沫阅读 1,487评论 0 5
  • 余干旅舍 刘长卿 摇落暮天迥,青枫霜叶稀。 孤城向水闭,独鸟背人飞。 渡口月初上,邻家渔未归。 乡心正欲绝,...
    如如在阅读 459评论 0 0