分享

iphone自定义UITextView的placeholder

 爽行天下丶 2015-06-08

大家都知道UITextField才有placeholder属性,UITextView 并没有placeholder,那么怎么模拟UITextfield使UITextView也有placeholder。

思路是:继承uitextview,判断当text为空时就让[super text]显示placeholder。

代码如下(我引用了arc):

UIPlaceholderTextView.h

  1. #import <UIKit/UIKit.h>  
  2.   
  3. @interface UIPlaceholderTextView : UITextView  
  4. @property(nonatomic, strong) NSString *placeholder;     //占位符  
  5.   
  6. -(void)addObserver;//添加通知  
  7. -(void)removeobserver;//移除通知  
  8. @end  

UIPlaceholderTextView.m

  1. #import "UIPlaceholderTextView.h"  
  2.   
  3. @interface UIPlaceholderTextView ()  
  4.   
  5. @property (nonatomic, strong) UIColor* textColor;  
  6.   
  7. - (void) beginEditing:(NSNotification*) notification;  
  8. - (void) endEditing:(NSNotification*) notification;  
  9.   
  10. @end  
  11.   
  12. @implementation UIPlaceholderTextView  
  13. @synthesize placeholder;  
  14. @synthesize textColor;  
  15. - (id) initWithFrame:(CGRect)frame {  
  16.     if ((self = [super initWithFrame:frame])) {  
  17.         [self awakeFromNib];  
  18.     }  
  19.     return self;  
  20. }  
  21. //当用nib创建时会调用此方法  
  22. - (void)awakeFromNib {  
  23.     textColor = [UIColor redColor];  
  24.     [self addObserver];  
  25. }  
  26. -(void)addObserver  
  27. {  
  28.     [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(beginEditing:) name:UITextViewTextDidBeginEditingNotification object:self];  
  29.     [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(endEditing:) name:UITextViewTextDidEndEditingNotification object:self];  
  30. }  
  31. -(void)removeobserver  
  32. {  
  33.     [[NSNotificationCenter defaultCenter] removeObserver:self];  
  34. }  
  35.   
  36. #pragma mark -  
  37. #pragma mark Setter/Getters  
  38. - (void) setPlaceholder:(NSString *)aPlaceholder {  
  39.     placeholder = aPlaceholder;  
  40.     [self endEditing:nil];  
  41. }  
  42.   
  43. - (NSString *) text {  
  44.     NSString* text = [super text];  
  45.     if ([text isEqualToString:placeholder]) return @"";  
  46.     return text;  
  47. }  
  48.   
  49. - (void) beginEditing:(NSNotification*) notification {  
  50.     if ([super.text isEqualToString:placeholder]) {  
  51.         super.text = nil;  
  52.         //字体颜色  
  53.         [super setTextColor:textColor];  
  54.     }  
  55.       
  56. }  
  57.   
  58. - (void) endEditing:(NSNotification*) notification {  
  59.     if ([super.text isEqualToString:@""] || self.text == nil) {  
  60.         super.text = placeholder;  
  61.         //注释颜色  
  62.         [super setTextColor:[UIColor lightGrayColor]];  
  63.     }  
  64. }  

在ViewController中用时,1、在xib上加一个uitextview连接上,2、直接创建initframe

但是不要忘了,placeholder,和添加通知,移除通知。

  1. - (void)viewDidLoad  
  2. {  
  3.     [super viewDidLoad];  
  4.     self.textView.placeholder = @"请添写你的信息……";  
  5.       
  6.     // Do any additional setup after loading the view, typically from a nib.  
  7. }  
  8. -(void)viewDidAppear:(BOOL)animated  
  9. {  
  10.     [super viewDidAppear:YES];  
  11.     [self.textView addObserver];  
  12. }  
  13. -(void)viewDidDisappear:(BOOL)animated  
  14. {  
  15.     [super viewDidDisappear:YES];  
  16.     [self.textView removeobserver];  
  17. }  

微笑


效果图:

     


欢迎大家,有更好的方法提出!


    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多