微博項目地址https://github.com/shidayangli/SinaWeiBo.git
微博項目上傳github好久了媳否,由于項目太多東西特恬,竟不知從何說起执俩,最近要開始找工作了,回顧一下癌刽,想到哪里說哪里吧役首。今天先說說自定義一個textV
iew,帶有placeholder的显拜。大家都用過微博衡奥,在發(fā)微博的那個界面,很明顯是一個textView远荠,但是textView又沒有placeholder屬性矮固,所以選擇自定義來解決問題∑┐荆看效果档址。
在寫這個自定義控件之前盹兢,我先提一點,就是我們在開發(fā)中經(jīng)常遇見會定義控件的frame情況辰晕,可是這個frame賦值起來有些費勁,如果你只想改變x值的話确虱,還得定義一個新的rect然后傳值含友,所以我給UIView寫了一個category,會很大程度上方便你的開發(fā)校辩,先給大家看這段代碼那窘问,相信我,磨刀不誤砍柴工宜咒!
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上,然后監(jiān)聽文字變化场晶,或者是使用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語句是判斷現(xiàn)在輸入框里是否有文字扳炬,如果有就不再畫placeholder了吏颖。再設(shè)置通知,監(jiān)聽自己發(fā)出的通知就好恨樟。
-(instancetype)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(textChanged) name:UITextViewTextDidChangeNotification object:self];
}
return self;
}