自定义含Placeholder的UITextView

在我们的项目中经常会用到UITextView文本输入框,但是系统的textView没有想textFieldView一样有placeholder属性来方便我们的使用。今天把项目中自己写的一个自定义控件分享给大家

效果图
  • 先上代码
属性设计
//
//  ZZYPlaceholderTextView.m
//  VTEAM
//
//  Created by admin on 16/7/12.
//  Copyright © 2016年 断剑. All rights reserved.
//

#import "ZZYPlaceholderTextView.h"

@interface ZZYPlaceholderTextView()

@property (nonatomic, weak) UILabel * placeholderLabel;

@end

@implementation ZZYPlaceholderTextView

- (void)awakeFromNib
{
    [self setUp];
}

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

- (void)setUp
{
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(textChange:) name:UITextViewTextDidChangeNotification object:self];
    CGFloat left = (self.leftMargin)?self.leftMargin:5;
    CGFloat top = (self.topMargin)?self.topMargin:5;
    CGFloat width = CGRectGetWidth(self.frame) - 2 * self.leftMargin;
    CGFloat height = (self.placeHeight)?self.placeHeight:21;
    UILabel * placeholderLabel = [[UILabel alloc]initWithFrame:CGRectMake(left, top, width,height)];
    placeholderLabel.textColor = (self.placeholderColor)?self.placeholderColor:[UIColor lightGrayColor];
    placeholderLabel.font = (self.placeholderFont)?self.placeholderFont:self.font;
    placeholderLabel.text = self.placeholder;
    [self addSubview:placeholderLabel];
    self.placeholderLabel = placeholderLabel;
}

- (void)textChange:(NSNotification *)nofi
{
    if (self.placeholder.length == 0 || [self.placeholder isEqualToString:@""]) {
        self.placeholderLabel.hidden = YES;
    }
    
    if (self.text.length > 0) {
        self.placeholderLabel.hidden = YES;
    }
    else
    {
        self.placeholderLabel.hidden = NO;
    }
    
}

- (void)setPlaceholder:(NSString *)placeholder
{
    _placeholder = placeholder;
    
    if (placeholder.length == 0 || [self.placeholder isEqualToString:@""]) {
        self.placeholderLabel.hidden = YES;
    }
    else
    {
        self.placeholderLabel.text = placeholder;
        self.placeholderLabel.hidden = NO;
    }
    
}

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


@end
  • 思路分析

    • 在UITExtView内部创建一个label,在外部为label设置各种属性
  • 通过UITextView系统检测内部文字改变的通知,动态控制label的显示和隐藏状态

  • 使用方法

  • xib使用

    • 设置UITextView的类
    • 拖线设置属性
  • 代码用法

 ZZYPlaceholderTextView * placeView = [[ZZYPlaceholderTextView alloc]initWithFrame:CGRectMake(20, 350, self.view.frame.size.width - 40 , 100)];
   placeView.placeholder = @"请输入描述文字";
   [self.view addSubview:placeView];
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 发现 关注 消息 iOS 第三方库、插件、知名博客总结 作者大灰狼的小绵羊哥哥关注 2017.06.26 09:4...
    肇东周阅读 12,261评论 4 61
  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 173,718评论 25 709
  • 今晚培姐讲课,关于跟另一半相处,我静不下心来听,断断续续的看一些; 到了提问题时候这才凑上来,整个过程是开始...
    妍心12388阅读 310评论 0 0
  • 说到男人的出轨,大家都会条件反射般想起文章,想起林丹,想起陈思诚。其实男人的出轨何止于此。 民政部印发《2015年...
    猫大人233阅读 557评论 0 0
  • 下午妈妈准备带侄女出去玩,当她把8个月大的小侄女放在车座上,由于不知道保险带没系好,导致小侄女摔下来了,哇...
    心安当下阅读 186评论 1 1