iphone自定义UITextView的placeholder
来源:互联网 发布:莫烦 tensorflow bili 编辑:程序博客网 时间:2024/05/21 17:45
大家都知道UITextField才有placeholder属性,UITextView 并没有placeholder,那么怎么模拟UITextfield使UITextView也有placeholder。
思路是:继承uitextview,判断当text为空时就让[super text]显示placeholder。
代码如下(我引用了arc):
UIPlaceholderTextView.h
#import <UIKit/UIKit.h>@interface UIPlaceholderTextView : UITextView@property(nonatomic, strong) NSString *placeholder; //占位符-(void)addObserver;//添加通知-(void)removeobserver;//移除通知@end
UIPlaceholderTextView.m
#import "UIPlaceholderTextView.h"@interface UIPlaceholderTextView ()@property (nonatomic, strong) UIColor* textColor;- (void) beginEditing:(NSNotification*) notification;- (void) endEditing:(NSNotification*) notification;@end@implementation UIPlaceholderTextView@synthesize placeholder;@synthesize textColor;- (id) initWithFrame:(CGRect)frame { if ((self = [super initWithFrame:frame])) { [self awakeFromNib]; } return self;}//当用nib创建时会调用此方法- (void)awakeFromNib { textColor = [UIColor redColor]; [self addObserver];}-(void)addObserver{ [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(beginEditing:) name:UITextViewTextDidBeginEditingNotification object:self]; [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(endEditing:) name:UITextViewTextDidEndEditingNotification object:self];}-(void)removeobserver{ [[NSNotificationCenter defaultCenter] removeObserver:self];}#pragma mark -#pragma mark Setter/Getters- (void) setPlaceholder:(NSString *)aPlaceholder { placeholder = aPlaceholder; [self endEditing:nil];}- (NSString *) text { NSString* text = [super text]; if ([text isEqualToString:placeholder]) return @""; return text;}- (void) beginEditing:(NSNotification*) notification { if ([super.text isEqualToString:placeholder]) { super.text = nil; //字体颜色 [super setTextColor:textColor]; } }- (void) endEditing:(NSNotification*) notification { if ([super.text isEqualToString:@""] || self.text == nil) { super.text = placeholder; //注释颜色 [super setTextColor:[UIColor lightGrayColor]]; }}
在ViewController中用时,1、在xib上加一个uitextview连接上,2、直接创建initframe
但是不要忘了,placeholder,和添加通知,移除通知。
- (void)viewDidLoad{ [super viewDidLoad]; self.textView.placeholder = @"请添写你的信息……"; // Do any additional setup after loading the view, typically from a nib.}-(void)viewDidAppear:(BOOL)animated{ [super viewDidAppear:YES]; [self.textView addObserver];}-(void)viewDidDisappear:(BOOL)animated{ [super viewDidDisappear:YES]; [self.textView removeobserver];}
效果图:
欢迎大家,有更好的方法提出!
- iphone自定义UITextView的placeholder
- 自定义 UITextView 的 placeholder 属性
- UITextView自定义封装(带placeHolder)
- UITextView 的placeholder
- UITextView 的placeholder
- UITextView的placeholder属性
- UITextView的placeholder属性
- UITextView的placeholder
- UITextView的字数限制 及 添加自定义PlaceHolder
- UITextView的placeholder猥琐做法
- UITextView 实现placeholder的方法
- UITextView 实现placeholder的方法
- 解决UITextView的placeholder属性
- UITextView 实现placeholder的方法
- iOS 实现UITextView的placeholder
- UITextView设置placeholder的方法
- UITextView 实现placeholder的方法
- UITextView 实现placeholder的方法
- JFreeChart的使用
- 带你走进EJB--EJB容器
- Objective-C编程基础读书笔记(5)
- spring +hibernate 启动优化【转】
- 常用DOC命令
- iphone自定义UITextView的placeholder
- 【rzxt】巧用电池小工具 电量问题全掌握
- 【转】Java虚拟机学习 - 对象访问 - 【关于栈 跟 堆】
- android捕获ListView中每个item点击事件
- div width=auto 与 width = 100% 区别
- struts.xml配置json
- js笔记之一
- jstring与c++字符串类型转换函数
- codeforces 78E - Evacuation 最大流