微博项目之自定义带palceholder的textview

微博项目地址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;
}
当然别忘了注销通知,这里就不写了,好了,就这么简单,直接就可以拿着用啦。
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 发现 关注 消息 iOS 第三方库、插件、知名博客总结 作者大灰狼的小绵羊哥哥关注 2017.06.26 09:4...
    肇东周阅读 12,262评论 4 61
  • 繁星划破嘿幕的夜 坠落进人间的街灯 皎月沉入碧潭,泛起点点涟漪 惊醒离人的相思梦 街上的霓虹灯闪耀着 化作一千汪...
    木木朴阅读 153评论 1 2
  • 文章部分内容摘自2015年12月24日易木世界创始人及顺德木业商会秘书长杨昌先生给易木世界的《一封家书》,彼时易木...
    木雅人阅读 1,063评论 1 1
  • 下班,提着包走在忙碌的街上。夜幕降临,华灯初上,车水马龙,上班族行色匆匆,一脸疲倦。抬头看,万家灯火,偌大的城市...
    猫与老鼠阅读 203评论 0 0
  • 闲逛在陌生城市一个不知名小区附近的早市巷子里,巷子被绿树装饰,两侧满是铺子,肉类、蔬果、点心......琳琅...
    张啸就是张啸阅读 355评论 0 1