@interface BSPublishTextView : UITextView
/** 对外属性占位字符 placeholder */
@property (nonatomic, copy) NSString *placeholder;
 
/** 对外属性占位符颜色 */
@property (nonatomic, strong) UIColor *placeholderColor;
@end

@interface BSPublishTextView ()
/** 占位字符 label */
@property (nonatomic, weak) UILabel *placeholderLabel;
@end
 
@implementation BSPublishTextView
- (UILabel *)placeholderLabel{
    if (!_placeholderLabel) {
        UILabel *placeholderLabel = [[UILabel alloc]init];
        placeholderLabel.numberOfLines = 0;
     CGRect frame = placeholderLabel.frame;
        frame.origin.x = 4;
        frame.origin.y = 7;
        placeholderLabel.frame = frame;
        [self addSubview:placeholderLabel];
        _placeholderLabel = placeholderLabel;
    }
    return _placeholderLabel;
}
 
- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // 永久垂直弹簧效果
        self.alwaysBounceVertical = YES;
        self.font = [UIFont systemFontOfSize:15];
        self.placeholderColor = [UIColor grayColor];
     
        // 注册textView文字发生改变通知  UITextViewTextDidChangeNotification
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(textChange) name:UITextViewTextDidChangeNotification object:nil];    
    }
    return self;
}
 
//通知调用方法
- (void)textChange{
    self.placeholderLabel.hidden = self.hasText;
}
 
- (void)dealloc{
    [[NSNotificationCenter defaultCenter]removeObserver:self];
}
 
- (void)layoutSubviews{
    [super layoutSubviews];
    //在 layoutSubviews 拿到 UITextView的宽度一定是正确的 ,这里如果外界即使更改了UITextView的宽度这里也是能算正确的
    //先设置placeholderLabel占位字符的宽度
    CGRect frame = self.placeholderLabel.frame;
    frame.size.width = self.frame.size.width - 2 * self.placeholderLabel.frame.origin.x;
    self.placeholderLabel.frame = frame;
 
    // placeholderLabel占位字符的大小size随里面的内容大小而变化
    [self.placeholderLabel sizeToFit];
}
 
// setNeedsDisplay方法 : 会在恰当的时刻自动调用drawRect:方法
// setNeedsLayout方法 : 会在恰当的时刻调用layoutSubviews方法
 
- (void)setPlaceholderColor:(UIColor *)placeholderColor{
    _placeholderColor = placeholderColor;
    self.placeholderLabel.textColor = placeholderColor;
}
 
- (void)setPlaceholder:(NSString *)placeholder{
    _placeholder = [placeholder copy];
    self.placeholderLabel.text = placeholder;
    [self setNeedsLayout]; //会在恰当的时刻调用layoutSubviews方法 这句代码可不写
}
 
- (void)setFont:(UIFont *)font{
    [super setFont:font];
    self.placeholderLabel.font = font;
    [self setNeedsLayout];  //会在恰当的时刻调用layoutSubviews方法 这句代码可不写
}
 
- (void)setText:(NSString *)text{
    [super setText:text];
    [self textChange];
}
 
- (void)setAttributedText:(NSAttributedString *)attributedText{
    [super setAttributedText:attributedText];
    [self textChange];
}
@end

iOS中用UILabel实现UITextView的占位文字的更多相关文章

  1. iOS UITextView placeHolder占位文字的N种方法实现方法

    方法一 1.把UITextView的text属性当成“placeholder”使用. 2.在开始编辑的代理方法里清除“placeholder”. 3.在结束编辑的代理方法里根据条件设置“placeho ...

  2. iOS开发中设置UITextField的占位文字的颜色,和光标的颜色

    在iOS开发中,对于很多初学者而言,很有可能碰到需要修改UITextField的占位文字的颜色,以及当UITextField成为第一响应者后光标的颜色,那么下面小编就介绍一下修改占位文字和光标的颜色. ...

  3. iOS开发小技巧--自定义带有占位文字的TextView(两种方式)

    自定义控件注意或框架注意:自己暴露在外面的属性,一定要重写setter,保证外界与内部的交互性 一.方案一:通过drawRect:方法将文字画到textView中,监听文字改变用的是通知中心(代理也可 ...

  4. UITextView设置占位文字

    这里只介绍一种,这种方便扩展,可以占位文字颜色. 我们继承一个UITextView: #import <UIKit/UIKit.h> @interface MyTextView : UIT ...

  5. iOS - UITextView实现placeHolder占位文字

      iOS之UITextView实现placeHolder占位文字的N种方法 前言 iOS开发中,UITextField和UITextView是最常用的文本接受类和文本展示类的控件.UITextFie ...

  6. iOS开发小技巧--即时通讯项目:使用富文本在UILabel中显示图片和文字;使用富文本占位显示图片

    Label借助富文本显示图片 1.即时通讯项目中语音消息UI的实现,样式如图: 借助富文本在UILabel中显示图片和文字 // 1.创建一个可变的富文本 NSMutableAttributedStr ...

  7. iOS中用UIWebView的loadHTMLString后图片和文字失调解决方法

    iOS中用UIWebView的loadHTMLString后图片和文字失调,图片过大,超过屏幕,文字太小.或者图片太小.文字太大,总之就是不协调. 我们的需求是让图片的大小跟着屏幕的变化而变化.就是动 ...

  8. AJ学IOS 之微博项目实战(11)发送微博自定义TextView实现带占位文字

    AJ分享,必须精品 一:效果 二:代码: 由于系统自带的UITextField:和UITextView:不能满足我们的需求,所以我们需要自己设计一个. UITextField: 1.文字永远是一行,不 ...

  9. iOS不得姐项目--登录模块的布局,设置文本框占位文字颜色,自定义内部控件竖直排列的按钮

    一.登录模块的布局 将一整部分切割成若干部分来完成,如图分成了三部分来完成 设置顶部状态栏为白色的方法 二.设置文本框占位文字颜色 <1>方法一与方法二实现原理是同一种,都是通过设置pla ...

随机推荐

  1. open_links_per_instance 和 open_links 参数说明

    1.1  OPEN_LINKS Property Description Parameter type Integer Default value 4 Modifiable No --即修改需要重启实 ...

  2. Android的ImageView介绍-android学习之旅(二十二)

    ImageView简介 imageView继承于View,主要用于显示图片,凡是Drawable对象都可以用它显示. ImageView直接派生了ImageButton和ZoomButton等组件. ...

  3. DAA和CMAC

    数据认证算法(DAA)  Data Authentication Algorithm DAA建立在DES之上,该算法比较陈旧,人们已经发现了这个算法的安全弱点,目前已经被废止. DAA采用DES运算的 ...

  4. Java-IO之PipedReader和PipedWriter

    PipedReader和PipedWriter与PipedInputStream和PipedOutputStream一样,都可以用于管道通信.PipedWriter是字符管道输出流,继承于Writer ...

  5. UNIX环境高级编程——线程属性之并发度

    并发度控制着用户级线程可以映射的内核线程或进程的数目.如果操作系统的实现在内核级的线程和用户级的线程之间保持一对一的映射,那么改变并发度并不会有什么效果,因为所有的用户级线程都可能被调度到.但是,如果 ...

  6. java 判断是否是周末

    package untitled7; import java.util.Date; import java.text.SimpleDateFormat; import java.util.Calend ...

  7. Torch的安装和学习

    Long long ago, 就已经安装好Torch,这里再记录一下.Torch是Facebook开发的用于AI的科学计算框架,可广泛运用于机器学习的很多算法.相比Caffe,其接口运用更加方便,使用 ...

  8. Linux Debugging(三): C++函数调用的参数传递方法总结(通过gdb+反汇编)

    上一篇文章<Linux Debugging:使用反汇编理解C++程序函数调用栈>没想到能得到那么多人的喜爱,因为那篇文章是以32位的C++普通函数(非类成员函数)为例子写的,因此只是一个特 ...

  9. 9.2.2、Libgdx的输入处理之事件处理

    (官网:www.libgdx.cn) 事件处理可以更加准确的获取用户的输入.事件处理提供了一种可以通过用户接口进行交互的方法.比如按下.释放一个按钮. 输入处理 事件处理通过观察者模式来完成.首先,需 ...

  10. java,http的post和get

    使用Java发送GET.POST请求 --节选自<疯狂Java讲义>    URL的openConnection()方法将返回一个URLConnection对象,该对象表示应用程序和 UR ...