textView实现placehodle
来源:互联网 发布:java代码表白 编辑:程序博客网 时间:2024/05/21 20:21
.H文件里面需要实现:
#import <UIKit/UIKit.h>
@interface UIPHTextView : UITextView
{
NSString *placeholder;
UIColor *placeholderColor;
@private
UILabel *placeHolderLabel;
}
@property(nonatomic,retain) UILabel *placeHolderLabel;
@property(nonatomic,retain) NSString *placeholder;
@property(nonatomic,retain) UIColor *placeholderColor;
-(void)textChanged:(NSNotification*)notification;
@end
.M需要实现:
#import "UIPHTextView.h"
@implementation UIPHTextView
- (instancetype)init{
self = [super init];
if (self) {
[super awakeFromNib];
[self setPlaceholder:@""];
[self setPlaceholderColor:[UIColor lightGrayColor]];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(textChanged:) name:UITextViewTextDidChangeNotification object:nil];
}
returnself;
}
- (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];
}
returnself;
}
- (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( [[selfplaceholder] length] >0 )
{
if (placeHolderLabel == nil )
{
placeHolderLabel = [[UILabelalloc] initWithFrame:CGRectMake(8,8,self.bounds.size.width - 16,0)];
placeHolderLabel.lineBreakMode =UILineBreakModeWordWrap;
placeHolderLabel.numberOfLines =0;
placeHolderLabel.font = [UIFontsystemFontOfSize:14];
placeHolderLabel.backgroundColor = [UIColorclearColor];
placeHolderLabel.textColor =self.placeholderColor;
placeHolderLabel.alpha =0;
placeHolderLabel.tag =999;
[selfaddSubview:placeHolderLabel];
}
placeHolderLabel.text =self.placeholder;
[placeHolderLabelsizeToFit];
[selfsendSubviewToBack:placeHolderLabel];
}
if( [[selftext] length] ==0 && [[selfplaceholder] length] >0 )
{
[[selfviewWithTag:999]setAlpha:1];
}
[superdrawRect:rect];
}
@end
ViewController.m里面需要实现:
#import "ViewController.h"
#import "UIPHTextView.h"
@interface ViewController ()<UITextViewDelegate>
{
UIView *_bgView;
UIPHTextView *_textView;
}
@end
@implementation ViewController
- (void)viewDidLoad {
[superviewDidLoad];
[self_createView];
}
- (void)_createView{
if (_bgView ==nil) {
_bgView = [[UIViewalloc]initWithFrame:CGRectMake(0,10, self.view.frame.size.width,200)];
_bgView.backgroundColor = [UIColorgreenColor];
[self.viewaddSubview:_bgView];
}
_textView = [[UIPHTextViewalloc] initWithFrame:CGRectMake(5,5, self.view.frame.size.width - 10, 200 -85)];
_textView.placeholder =@"说点什么...";
_textView.font = [UIFontsystemFontOfSize:15];
_textView.delegate =self;
_textView.showsVerticalScrollIndicator =NO;
_textView.placeholderColor = [UIColorgrayColor];
[_bgViewaddSubview:_textView];
}
- (void)didReceiveMemoryWarning {
[superdidReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
- textView实现placehodle
- TextView 滚动如何实现
- Android TextView实现超链接
- TextView实现滚动效果
- 个性TextView自己实现
- TextView实现跑马灯
- TextView实现字体滚动
- TextView 实现自动换行
- TextView实现下划线
- TextView实现跑马灯
- TextView实现Rich Text
- TextView 实现 滚动条
- TextView 实现 placeholder 备忘
- 实现TextView文本下划线
- textview的自定义实现
- textView实现placeholder效果
- 实现TextView滚动
- TextView 实现 placeholder
- HTML5 weui笔记
- Python 字符串格式化
- Java中日期类,日历类的使用
- 工厂方法模式
- 实现登录界面的“记住我”功能 JAVA
- textView实现placehodle
- STL vector用法
- 关于Redis数据过期策略
- Android依赖注入:Dagger、RoboGuice和ButterKnife比较
- 友盟页面爬虫工具
- Til the Cows Come Home (POJ 1860)
- Collect More Jewels
- 详解mysql int类型的长度值问题
- Android资源配置arrays.xml的适配器Adapter