UITextView的Placeholder占位文字
来源:互联网 发布:淘宝买什么产品好 编辑:程序博客网 时间:2024/06/02 02:06
自己写的一个小demo
#import "ZHZTextView.h"#define kMargin 7#define kFontSize 17@interface ZHZTextView ()<UITextViewDelegate>@property (nonatomic, strong)UILabel *placeholderLabel;@end@implementation ZHZTextView- (instancetype)initWithFrame:(CGRect)frame{ self = [super initWithFrame:frame]; if (self) { self.delegate = self; self.layer.cornerRadius = kMargin; self.layer.borderWidth = 1; self.font = [UIFont systemFontOfSize:kFontSize]; } return self;}#pragma mark - UITextViewDelegate- (void)textViewDidChange:(UITextView *)textView { self.placeholderLabel.hidden = textView.text.length;}#pragma mark - getter and setter- (UILabel *)placeholderLabel { if (_placeholderLabel == nil) { _placeholderLabel = [[UILabel alloc] init]; _placeholderLabel.textColor = [UIColor lightGrayColor]; _placeholderLabel.numberOfLines = 0; } return _placeholderLabel;}- (void)setPlaceholder:(NSString *)placeholder { if (_placeholder != placeholder) { //只有在 设置占位文本时 才加载 label [self addSubview:self.placeholderLabel]; //设置占位文本 self.placeholderLabel.text = placeholder; //文本 frame NSDictionary *dict = @{ NSFontAttributeName: [UIFont systemFontOfSize:kFontSize] }; CGRect rect = [placeholder boundingRectWithSize:CGSizeMake(self.frame.size.width - kMargin * 2, 0) options:NSStringDrawingUsesLineFragmentOrigin |NSStringDrawingUsesFontLeading attributes:dict context:nil]; self.placeholderLabel.frame = CGRectMake(kMargin, kMargin, rect.size.width, rect.size.height); }}@end
调用
ZHZTextView *textView = [[ZHZTextView alloc] initWithFrame:CGRectMake(50, 20, 275, 375)]; textView.placeholder = @"是来占位的我是来占位的我是来占位的我是来占位的我是来占位的"; [self.view addSubview:textView];
1 0
- UITextView的Placeholder占位文字
- iOS 实现 UITextView placeHolder的占位文字
- iOS-UITextView占位文字placeholder
- iOS UITextView的PlaceHolder 占位文字的实现
- iOS之UITextView实现placeHolder占位文字的最佳方法
- 史上最全的iOS之UITextView实现placeHolder占位文字的N种方法
- iOS 史上最全的 UITextView 实现 placeHolder 占位文字 的N种方法
- 史上最全的iOS之UITextView实现placeHolder占位文字的N种方法
- iOS开发:UITextView实现placeHolder占位文字的N种方法
- UITextView实现占位文字
- ios中实现带占位符placeholder的UITextview
- ios 自定义带占位文字(placeholder)的TextView
- iOS---自定义带占位文字placeholder的TextView
- UITextView 的placeholder
- UITextView 的placeholder
- UITextView的placeholder属性
- UITextView的placeholder属性
- UITextView的placeholder
- USACO入门以及提交格式
- Android开发相关的Blog推荐——跟随大神的脚步才能成长为大神
- fiddler抓包-get/post请求
- Scala Spark 求众数
- android shape使用说明
- UITextView的Placeholder占位文字
- java相关-- 工作笔记第一天
- hdu 1874畅通工程续(基础Floyd)
- c++中随机数的应用
- oracle dba常用sql
- iOS_开发中遇到的那些问题_4
- 解析传值调用
- A星寻路算法介绍 Introduction to A* Pathfinding
- 第五篇 虚拟机字节码执行引擎