微博项目地址https://github.com/shidayangli/SinaWeiBo.git
微博项目上传github好久了,由于项目太多东西,竟不知从何说起,最近要开始找工作了,回顾一下,想到哪里说哪里吧。今天先说说自定义一个textV
iew,带有placeholder的。大家都用过微博,在发微博的那个界面,很明显是一个textView,但是textView又没有placeholder属性,所以选择自定义来解决问题。看效果。
未命名.gif
在写这个自定义控件之前,我先提一点,就是我们在开发中经常遇见会定义控件的frame情况,可是这个frame赋值起来有些费劲,如果你只想改变x值的话,还得定义一个新的rect然后传值,所以我给UIView写了一个category,会很大程度上方便你的开发,先给大家看这段代码那,相信我,磨刀不误砍柴工!
UIView+Extension.h
#import <UIKit/UIKit.h>
@interface UIView (Extension)
@property(nonatomic, assign)CGFloat x;
@property(nonatomic, assign)CGFloat y;
@property(nonatomic, assign)CGFloat width;
@property(nonatomic, assign)CGFloat height;
@property(nonatomic, assign)CGSize size;
@property(nonatomic, assign)CGPoint origin;
@property(nonatomic, assign)CGFloat centerX;
@property(nonatomic, assign)CGFloat centerY;
@end
UIView+Extension.m
#import "UIView+Extension.h"
@implementation UIView (Extension)
-(void)setX:(CGFloat)x
{
CGRect frame = self.frame;
frame.origin.x = x;
self.frame = frame;
}
-(void)setY:(CGFloat)y
{
CGRect frame = self.frame;
frame.origin.y = y;
self.frame = frame;
}
-(CGFloat)x
{
return self.frame.origin.x;
}
-(CGFloat)y
{
return self.frame.origin.y;
}
-(void)setWidth:(CGFloat)width{
CGRect frame = self.frame;
frame.size.width = width;
self.frame = frame;
}
-(void)setHeight:(CGFloat)height
{
CGRect frame = self.frame;
frame.size.height = height;
self.frame = frame;
}
-(CGFloat)width
{
return self.frame.size.width;
}
-(CGFloat)height
{
return self.frame.size.height;
}
-(void)setSize:(CGSize)size
{
CGRect frame = self.frame;
frame.size = size;
self.frame = frame;
}
-(CGSize)size
{
return self.frame.size;
}
-(void)setOrigin:(CGPoint)origin{
CGRect frame = self.frame;
frame.origin = origin;
self.frame = frame;
}
-(CGPoint)origin
{
return self.frame.origin;
}
-(void)setCenterX:(CGFloat)centerX
{
CGPoint point = self.center;
point.x = centerX;
self.center = point;
}
-(CGFloat)centerX
{
return self.center.x;
}
-(void)setCenterY:(CGFloat)centerY
{
CGPoint point = self.center;
point.y = centerY;
self.center = point;
}
-(CGFloat)centerY
{
return self.center.y;
}
@end
代码很简单易懂,就是有点长,下面进入正题,一般添加placeholder有好几种方法,有添加一个UILabel在textView上,然后监听文字变化,或者是使用NSString的方法直接将文字画上去,这里并无好坏之分,只有适合不适合,我选用的是采用NSString的方法,就是在textView的 drawRect:
方法中用NSString的drawInRect:placeholderRect withAttributes:
方法画出,代码如下:
-(void)drawRect:(CGRect)rect
{
if (self.hasText) {
[super drawRect:rect];
}else{
//采用画的方式得到placeholder文字
NSMutableDictionary *attr = [NSMutableDictionary dictionary];
attr[NSFontAttributeName] = self.font;
attr[NSForegroundColorAttributeName] = [UIColor grayColor];
CGRect placeholderRect = CGRectMake(5, 8, rect.size.width - 10, rect.size.height - 16);
[self.placeholder drawInRect:placeholderRect withAttributes:attr];
}
}
if语句是判断现在输入框里是否有文字,如果有就不再画placeholder了。再设置通知,监听自己发出的通知就好。
-(instancetype)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(textChanged) name:UITextViewTextDidChangeNotification object:self];
}
return self;
}