#import <UIKit/UIKit.h> #import <Foundation/Foundation.h> @interface UIPlaceHolderTextView : UITextView @property (nonatomic, retain) NSString *placeholder; @property (nonatomic, retain) UIColor *placeholderColor; -(void)textChanged:(NSNotification*)notification; @end
#import "UIPlaceHolderTextView.h" @interface UIPlaceHolderTextView () @property (nonatomic, retain) UILabel *placeHolderLabel; @end @implementation UIPlaceHolderTextView - (void)dealloc { [[NSNotificationCenter defaultCenter] removeObserver:self]; #if __has_feature(objc_arc) #else [_placeHolderLabel release]; _placeHolderLabel = nil; [_placeholderColor release]; _placeholderColor = nil; [_placeholder release]; _placeholder = nil; [super dealloc]; #endif } - (void)awakeFromNib { [super awakeFromNib]; // Use Interface Builder User Defined Runtime Attributes to set // placeholder and placeholderColor in Interface Builder. if (!self.placeholder) { [self setPlaceholder:@""]; } if (!self.placeholderColor) { [self setPlaceholderColor:[UIColor lightGrayColor]]; } [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(textChanged:) name:UITextViewTextDidChangeNotification object:nil]; } - (id)initWithFrame:(CGRect)frame { if( (self = [super initWithFrame:frame]) ) { [self setPlaceholder:@""]; [self setPlaceholderColor:[UIColor lightGrayColor]]; [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(textChanged:) name:UITextViewTextDidChangeNotification object:nil]; } return self; } - (void)textChanged:(NSNotification *)notification { if([[self placeholder] length] == 0) { return; } if([[self text] length] == 0) { [[self viewWithTag:999] setAlpha:1]; } else { [[self viewWithTag:999] setAlpha:0]; } } - (void)setText:(NSString *)text { [super setText:text]; [self textChanged:nil]; } - (void)drawRect:(CGRect)rect { if( [[self placeholder] length] > 0 ) { if (_placeHolderLabel == nil ) { _placeHolderLabel = [[UILabel alloc] initWithFrame:CGRectMake(8,8,self.bounds.size.width - 16,0)]; _placeHolderLabel.lineBreakMode = NSLineBreakByWordWrapping; _placeHolderLabel.numberOfLines = 0; _placeHolderLabel.font = self.font; _placeHolderLabel.backgroundColor = [UIColor clearColor]; _placeHolderLabel.textColor = self.placeholderColor; _placeHolderLabel.alpha = 0; _placeHolderLabel.tag = 999; [self addSubview:_placeHolderLabel]; } _placeHolderLabel.text = self.placeholder; [_placeHolderLabel sizeToFit]; [self sendSubviewToBack:_placeHolderLabel]; } if( [[self text] length] == 0 && [[self placeholder] length] > 0 ) { [[self viewWithTag:999] setAlpha:1]; } [super drawRect:rect]; } @end
相关推荐
@interface UITextView (Placeholder) /* 占位文字 */ @property (nonatomic, copy) NSString *placeholder; /* 占位文字颜色 */ @property (nonatomic, strong) UIColor *placeholderColor; @end
UITextView+placeholder,一行代码搞定
使你的UITextView可以设置placeholder,并且可以控制UITextView的输入文本数量。
TextView扩展的placeHoder。比继承方式实现的用来更方便
WJTextVIew-PlaceHolder 像textfield.placeHolder一样,在UITextView中实现PlaceHolder的效果 如果发现一些错误,请发送电子邮件给我。 谢谢!
UITextView 的封装,可以自定义 placeholder、文字的(字体、颜色)、文字(字体、颜色),自适应高度,实时监测输入文字的最大高度,实时监测输入文字的最大个数,可以限制最大输入文字字数
源码UITextView-placeholder,分类已经写好,直接拿来用即可。 示例代码: UITextView *textView = [[UITextView alloc] init]; textView.font = [UIFont systemFontOfSize:18.0]; //注意先设置字体,再设置...
pod 'UITextView+Placeholder' 用法 导入动态框架: 例如,如果您将CocoaPods与use_frameworks!一起使用use_frameworks! 旗。 @import UITextView_Placeholder; 导入静态库: # import < UITextView> 然后创建...
如何让UITextView也有placeholder功能呢?今天给各位分享一个比较猥琐的做法。思路大概是这样的: 把UITextView的text当placeholder使用。 在开始编辑的代理方法里清除placeholder。 在结束编辑的代理方法里在...
WCTextView继承自UITextView,并添加了placeholder(占位文本)的功能
PlaceholderTextView *textView = [[PlaceholderTextView alloc]init]; textView.placeholderLabel.font = [UIFont systemFontOfSize:15]; textView.placeholder = @"请输入文字..."; textView.font = ...
ios下带占位符placeholder的UITextView
这是增加UITextView的placeHolder属性,封装很好的,很容易用
placeholder, uitextview
带有palceholder的UITextView,只需引入LYTextView,然后进行调用即可,可以纯代码引入,也可以在XIB中引入,Demo中都有实例。 可以设定palceholder的4个参数,文字,字体,颜色,高度 改代码也可以自己修改满足自己...
A missing placeholder for UITextView..zip,缺少uitextview的占位符
UITextView 实现UITextField功能
UITextView 和UILable 它本身是没有控制行高的。很多人说用Uiwebview,但我觉得那不好用,要用html布局麻烦,占内存。也有人说drawatpoint,但 发现画中文的时候也很占内存,我就投机取巧一下吧
UITextView的简单封装