iOS-UITextView占位文字placeholder
来源:互联网 发布:淘宝运费险最多赔多少 编辑:程序博客网 时间:2024/06/08 13:32
自定义类,继承UITextView,实现类似于UITextField的占位文字效果,用户输入文本后占位文字消失.
使用方法仅需两行代码:
XWPlaceholderTextView *placholderTextView = [XWPlaceholderTextView shareWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight - 64) withPlaceholder:@"我们将根据您反馈的意见和问题, 提升产品的体验感!您的声音很重要"]; [self.view addSubview:placholderTextView];
.h
//// XWPlaceholderTextView.h// Spread//// Created by 邱学伟 on 16/4/18.// Copyright © 2016年 邱学伟. All rights reserved.// 有占位文字的UITextView#import <UIKit/UIKit.h>@interface XWPlaceholderTextView : UITextView/** * 占位文字 * 我们将根据您反馈的意见和问题, 提升产品的体验感! */@property (nonatomic, copy) NSString *placeholderStr;/** * 工厂方法 * * @param frame textView的尺寸 * @param placeholderStr 占位文字 */+(instancetype)shareWithFrame:(CGRect)frame withPlaceholder:(NSString *)placeholderStr;/** * 初始化 */-(instancetype)initWithPlaceholder:(NSString *)placeholderStr;@end
.m
//// XWPlaceholderTextView.m// Spread//// Created by 邱学伟 on 16/4/18.// Copyright © 2016年 邱学伟. All rights reserved.//#import "XWPlaceholderTextView.h"//间隔#define kMargin 7//字体大小#define kFontSize 17@interface XWPlaceholderTextView ()<UITextViewDelegate>/** * 占位文本框 */@property (nonatomic, strong) UILabel *placeholderLB;@end@implementation XWPlaceholderTextView#pragma mark - 懒加载-(UILabel *)placeholderLB{ if (_placeholderLB == nil) { _placeholderLB = [[UILabel alloc] init]; _placeholderLB.textColor = [UIColor lightGrayColor]; [_placeholderLB setNumberOfLines:0]; } return _placeholderLB;}//初始化方法-(instancetype)initWithFrame:(CGRect)frame withPlaceholder:(NSString *)placeholderStr{ self = [super initWithFrame:frame]; if (self) { //设置占位文本才会调用getter/setter方法 self.placeholderStr = placeholderStr; self.delegate = self;// self.layer.cornerRadius = kMargin; self.font = [UIFont systemFontOfSize:kFontSize]; } return self;}+(instancetype)shareWithFrame:(CGRect)frame withPlaceholder:(NSString *)placeholderStr{ XWPlaceholderTextView *XWText = [[XWPlaceholderTextView alloc] initWithFrame:frame withPlaceholder:placeholderStr]; return XWText;}#pragma mark - UITextViewDelegate- (void)textViewDidChange:(UITextView *)textView { self.placeholderLB.hidden = textView.text.length;}#pragma mark - setter方法-(void)setPlaceholderStr:(NSString *)placeholderStr{ if (_placeholderStr != placeholderStr) { _placeholderStr = placeholderStr; //只有在设置占位文字时,才加载占位label [self addSubview:self.placeholderLB]; //设置占位文本 [self.placeholderLB setText:_placeholderStr]; //占位文本Lable的尺寸 NSDictionary *attributes = @{ NSFontAttributeName:[UIFont systemFontOfSize:kFontSize] }; //获取一段文本在规定长宽的尺寸 CGRect placeholderLBSize = [_placeholderStr boundingRectWithSize:CGSizeMake(self.frame.size.width - kMargin * 2, 0) options:NSStringDrawingUsesLineFragmentOrigin |NSStringDrawingUsesFontLeading attributes:attributes context:nil]; [self.placeholderLB setFrame:CGRectMake(kMargin, kMargin, placeholderLBSize.size.width, placeholderLBSize.size.height)]; }}/*// Only override drawRect: if you perform custom drawing.// An empty implementation adversely affects performance during animation.- (void)drawRect:(CGRect)rect { // Drawing code}*/@end
0 0
- iOS-UITextView占位文字placeholder
- iOS 实现 UITextView placeHolder的占位文字
- UITextView的Placeholder占位文字
- iOS UITextView的PlaceHolder 占位文字的实现
- iOS之UITextView实现placeHolder占位文字的最佳方法
- 史上最全的iOS之UITextView实现placeHolder占位文字的N种方法
- iOS开发:UITextView实现placeHolder占位文字的N种方法
- iOS 史上最全的 UITextView 实现 placeHolder 占位文字 的N种方法
- 史上最全的iOS之UITextView实现placeHolder占位文字的N种方法
- iOS占位文字placeholder设置技巧
- ios中实现带占位符placeholder的UITextview
- UITextView实现占位文字
- iOS 怎么给UITextView添加占位符文字
- ios 自定义带占位文字(placeholder)的TextView
- iOS---自定义带占位文字placeholder的TextView
- IOS UITextView加上placeholder
- iOS UITextView加上placeholder
- IOS UITextView加上placeholder
- Android API Guides---View Animation
- 反射
- Lintcode 二叉树后序遍历 非递归形式
- 并查集+Set-BZOJ-1604-[Usaco2008 Open]Cow Neighborhoods 奶牛的邻居
- 查询相册和摄像头的使用权限
- iOS-UITextView占位文字placeholder
- A strange lift
- myeclipse 远程调控 tomcat 配置
- Scrapy之信号
- Previous operation has not finished; run 'cleanup' if it was interrupted
- Trie树
- android常用权限
- 仿Atom的activate-power-mode插件的Android studio插件
- EasyUI datagrid 与 pagination 动态赋值的联用