iOS学习之----------自定义UITextView
来源:互联网 发布:网络主播思瑞啪啪视频 编辑:程序博客网 时间:2024/04/30 04:54
1、自定义UITextView(将UITextView和UILabel想结合使用)
使用UITextView的时候需要在text显示一段提示文字,但是UITextView没有像UITextField一样的 placehoder属性对其进行设置,因此需要将将UITextView和UILabel相结合使用,自定义一个UITextView。
1、创建textView类继承于UITextView
2、在initWithFrame:(CGRect)frame中添加UILabel
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
self.backgroundColor = [UIColor clearColor];
// Initialization code
UILabel *placehoderLabel = [[UILabel alloc]init];
[self addSubview:placehoderLabel];
self.placehoderLabel = placehoderLabel;
//设置textView(self)的背景色为clearColor
self.backgroundColor = [UIColor clearColor];
//设置label的背景色为clearColor
placehoderLabel.backgroundColor = [UIColor clearColor];
placehoderLabel.numberOfLines = 0;
}
return self;
}
3、设置label的frame
- (void)layoutSubviews
{
[super layoutSubviews];
self.placehoderLabel.x = 5;
self.placehoderLabel.y = 7;
self.placehoderLabel.width = self.width - self.placehoderLabel.x;
CGSize maxSize = CGSizeMake(self.placehoderLabel.width, MAXFLOAT);
CGSize placehoderSize = [self.placehoder sizeWithFont:self.placehoderLabel.font constrainedToSize:maxSize];
self.placehoderLabel.height = placehoderSize.height;
}
4、定义外部属性,其他人可以对textView的placehoder和placehoderColor进行修改
@property (nonatomic,copy)NSString *placehoder;
@property (nonatomic,strong)UIColor *placehoderColor;
5、重写两个属性的set方法提供给外部的方法可以对这些属性对其进行设置
-(void)setPlacehoderColor:(UIColor *)placehoderColor
{
_placehoderColor = placehoderColor;
self.placehoderLabel.textColor = _placehoderColor;
}
- (void)setPlacehoder:(NSString *)placehoder
{
#warning 如果是copy set最好这样写
_placehoder = [placehoder copy];
self.placehoderLabel.text = placehoder;
//重新刷新frame (当在中途换了文字需要重新计算frame)
[self setNeedsLayout];
}
6、设置监听,监听textView的文本信息改变的监听
NSNotificationCenter *center = [NSNotificationCenter defaultCenter];
//当self的文字发生改变 self就会自动发出一个UITextViewTextDidChangeNotification通知
[center addObserver:self selector:@selector(textDidChange) name:UITextViewTextDidChangeNotification object:self];
7、设置label的显示与隐藏
当有文本信息输入的时候,将label隐藏
- (void)textDidChange
{
self.placehoderLabel.hidden = (self.text.length != 0);
}
8、重写setFont方法防止其他人在使用的时候 修改字体大小
- (void)setFont:(UIFont *)font
{
[super setFont:font];
self.placehoderLabel.font = font;
//重新刷新frame (当在中途换了文字需要重新计算frame)
[self setNeedsLayout];
}
- iOS学习之----------自定义UITextView
- iOS学习笔记之自定义UITextView控件(带有placeholder)
- iOS之UI学习-UITextView
- IOS学习之——UITextView
- ios学习--UItextview
- 初学iOS之UITextView
- IOS学习笔记(四)之UITextField和UITextView控件学习
- OC学习之UITextView
- ios学习笔记-Lecture5:UITextView
- iOS 客户端学习-UITextView
- ios基础控件之UITextView
- ios基本控件之UITextView
- IOS 之 UITextField与UITextView
- ios-UITextview自定义 带placeholder,left间距
- iOS之UITextView的使用——自定义选择文本后的菜单
- iOS开发之UITextView自定义选择内容后的分享菜单
- iOS学习之----------自定义工具条
- IOS学习之自定义UITableViewCell
- XML自动建表/存库
- [知其然不知其所以然-1]input device与UI的交互方式
- GCC 常见错误解析
- hdu 3306(矩阵快速幂)
- Struts2的主要学习内容
- iOS学习之----------自定义UITextView
- Ubuntu14.04和Tiny6410挂载NFS服务!
- Docker网络连接探索
- myeclipse修改背景颜色(主题)
- String和int转化
- 我的新博客空间
- file_get_contents超时设置
- 查找——相邻元素差的绝对值都是1的数组当中的某个数,百度笔试题
- Exception in thread "main" org.hibernate.TransientObjectException: object references an unsaved tran