自定义控件注意或框架注意:自己暴露在外面的属性,一定要重写setter,保证外界与内部的交互性

一.方案一:通过drawRect:方法将文字画到textView中,监听文字改变用的是通知中心(代理也可以监听文字改变,但是这种方式就成了自己作为自己的代理了,不推荐这种方法)发出的消息

UITextViewTextDidChangeNotification.自己监听通知.

  • 对外界提供两个属性

  • 内部实现
 #import "ChaosPlaceholdTextView.h"

 @implementation ChaosPlaceholdTextView

 - (instancetype)initWithFrame:(CGRect)frame
{
if (self = [super initWithFrame:frame]) { [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(textChange) name:UITextViewTextDidChangeNotification object:nil]; // 外界文字颜色不设置,默认为灰色
self.placeholdColor = [UIColor grayColor];
}
return self;
} - (void)dealloc
{
[[NSNotificationCenter defaultCenter] removeObserver:self];
} - (void)textChange
{
[self setNeedsDisplay];
} // 调用drawRect时,会将之前的图像擦除掉,重新绘制
- (void)drawRect:(CGRect)rect { if ([self hasText]) return; rect.origin.y += ( + );
rect.origin.x += ;
rect.size.width -= * rect.origin.x; NSMutableDictionary *attrs = [NSMutableDictionary dictionary];
attrs[NSForegroundColorAttributeName] = self.placeholdColor;
attrs[NSFontAttributeName] = [UIFont systemFontOfSize:];
[self.placehold drawInRect:rect withAttributes:attrs];
} // 设计框架需注意,给外界提供了属性后,一定重写出行的setter,这样既可以时时监听使用者对属性的更改,还可以跟好的与外界代码进行交互
- (void)setPlacehold:(NSString *)placehold
{
_placehold = placehold;
// 设置了站位文字后,需要重绘一遍
[self setNeedsDisplay];
} - (void)setPlaceholdColor:(UIColor *)placeholdColor
{
_placeholdColor = placeholdColor;
[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
  • 缺点:由于是画上去的,当设置了textView的alwaysBounceHorizontal属性后,不能跟随着scrollView的滑动而滑动,如下图

方案二:通过给TextView添加一个Label,也是通过监听文字改变的通知,修改label的hidden属性.

 #import "ChaosPlaceholdTextView.h"

 @interface ChaosPlaceholdTextView()
/** 占位文字label */
@property (nonatomic, weak) UILabel *placeholderLabel;
@end @implementation ChaosPlaceholdTextView - (UILabel *)placeholderLabel
{
if (!_placeholderLabel) {
// 添加一个用来显示占位文字的label
UILabel *placeholderLabel = [[UILabel alloc] init];
placeholderLabel.numberOfLines = ;
placeholderLabel.x = ;
placeholderLabel.y = ;
[self addSubview:placeholderLabel];
_placeholderLabel = placeholderLabel;
}
return _placeholderLabel;
} - (instancetype)initWithFrame:(CGRect)frame
{
if (self = [super initWithFrame:frame]) {
// 垂直方向上永远有弹簧效果
self.alwaysBounceVertical = YES; // 默认字体
self.font = [UIFont systemFontOfSize:]; // 默认的占位文字颜色
self.placeholderColor = [UIColor grayColor]; // 监听文字改变
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(textDidChange) name:UITextViewTextDidChangeNotification object:nil];
}
return self;
} - (void)dealloc
{
[[NSNotificationCenter defaultCenter] removeObserver:self];
} /**
* 监听文字改变
*/
- (void)textDidChange
{
// 只要有文字, 就隐藏占位文字label
self.placeholderLabel.hidden = self.hasText;
} /**
* 更新占位文字的尺寸
*/
- (void)updatePlaceholderLabelSize
{
CGSize maxSize = CGSizeMake(ChaosScreenW - * self.placeholderLabel.x, MAXFLOAT);
self.placeholderLabel.size = [self.placeholder boundingRectWithSize:maxSize options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName : self.font} context:nil].size;
} #pragma mark - 重写setter
- (void)setPlaceholderColor:(UIColor *)placeholderColor
{
_placeholderColor = placeholderColor; self.placeholderLabel.textColor = placeholderColor;
} - (void)setPlaceholder:(NSString *)placeholder
{
_placeholder = [placeholder copy]; self.placeholderLabel.text = placeholder; [self updatePlaceholderLabelSize];
} - (void)setFont:(UIFont *)font
{
[super setFont:font]; self.placeholderLabel.font = font; [self updatePlaceholderLabelSize];
} - (void)setText:(NSString *)text
{
[super setText:text]; [self textDidChange];
} - (void)setAttributedText:(NSAttributedString *)attributedText
{
[super setAttributedText:attributedText]; [self textDidChange];
} @end

iOS开发小技巧--自定义带有占位文字的TextView(两种方式)的更多相关文章

  1. iOS开发小技巧 - label中的文字添加点击事件

    Label中的文字添加点击事件 GitHub地址:https://github.com/lyb5834/YBAttributeTextTapAction 以前老师讲过类似的功能,自己懒得回头看了,找了 ...

  2. iOS开发小技巧 - UILabel添加中划线

    iOS开发小技巧 遇到的问题: 给Label添加中划线,然后并没有效果 NSString *str = [NSString stringWithFormat:@"合计金额 ¥%.2f&quo ...

  3. iOS开发小技巧 - runtime适配字体

    iOS开发小技巧 - runtime适配字体 版权声明:本文为博主原创文章,未经博主允许不得转载,有问题可联系博主Email: liuyongjiesail@icloud.com 一个iOS开发项目无 ...

  4. iOS开发实用技巧—Objective-C中的各种遍历(迭代)方式

    iOS开发实用技巧—Objective-C中的各种遍历(迭代)方式 说明: 1)该文简短介绍在iOS开发中遍历字典.数组和集合的几种常见方式. 2)该文对应的代码可以在下面的地址获得:https:// ...

  5. iOS开发小技巧

    1. 解析详情页(是webView)遇到的3个问题: 1.图片太大,超出屏幕范围 2.怎么在webView上面添加一行文字 3.文字太小 1.解决方法 webView.scalesPageToFit ...

  6. 简易封装一个带有占位文字的TextView

    在实际iOS应用开发中我们经常会用到类似于下图所示的界面,即带有占位文字的文本框:

  7. 简单说 通过CSS实现 文字渐变色 的两种方式

    说明 这次的重点就在于两个属性, background 属性 mask 属性 这两个属性分别是两种实现方式的关键. 解释 方式一 效果图 代码 <!DOCTYPE html> <ht ...

  8. iOS开发小技巧--TableView中headerView的循环利用,以及自定义的headerView

    一.首先要搞清楚,tableView中有两种headerView,一个是tableHeaderView,另一个是headerView.前者就一个;后者根据session决定个数 headerView的 ...

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

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

随机推荐

  1. 用PS设计等高线效果的背景图片

    有些简单的单网页,如果利用等高线效果的背景图片,再配合合适的背景色,能达到绚丽的效果.如下图所示: 本文就介绍该等高线效果的背景图片是如何制作的.Follow Me!!!! 1.新建文档,尺寸:100 ...

  2. 《2016ThoughtWorks技术雷达峰会----js爆炸下的技术选型》

    JS爆炸下的技术选型  刘尚奇    ThoughtWorks, 高级咨询师 JS每6个星期出现一个新框架,那么如何进行JS的选型.以下从四个方面来分析. 1.工具 NPM for all the t ...

  3. POJ3013 Big Christmas Tree[转换 最短路]

    Big Christmas Tree Time Limit: 3000MS   Memory Limit: 131072K Total Submissions: 23387   Accepted: 5 ...

  4. Flex(flash)检测摄像头的3种状态(是否被占用,没安装摄像头,正常)

    在视频程序的编写过程中,我们经常要使用摄像头,在使用摄像头前有必要对摄像头的现有状态做个检测: 1.被占用 2.没安装摄像头 3.正常 camera=Camera.getCamera();       ...

  5. JS读取写入删除COOKIE的各种操作

    1. .NET后置代码中获取 Response.Redirect("http://www.baidu.com?id=" +Request.Cookies["size&qu ...

  6. c++ 基于wincrypt的DES CBC模式加解密

    des.h #pragma once #include <windows.h> #include <atlstr.h> #include <wincrypt.h> ...

  7. 苹果IOS开发者账号总结--发布应用APP时team name是否可以随意写?

    个人账号(Individual): 费用99美金一年, 该账号在App Store销售者只能显示个人的ID,比如zhitian zhang,单人使用.个人账号只能有一个开发者.100个苹果的iOS设备 ...

  8. js禁止用户右键等操作

    <script type="text/javascript">    document.oncontextmenu=function(){return false};  ...

  9. struts2 异常处理3板斧

    板斧1:找不到action的错误 在struts.xml中参考如下配置 <struts> ... <package name="default" namespac ...

  10. 线程本地变量ThreadLocal源码解读

      一.ThreadLocal基础知识 原始线程现状: 按照传统经验,如果某个对象是非线程安全的,在多线程环境下,对对象的访问必须采用synchronized进行线程同步.但是Spring中的各种模板 ...