新浪微博开发之十八(文本框)

来源:互联网 发布:php 常量定义和使用 编辑:程序博客网 时间:2024/05/16 15:04
//
//  MyTextView.h
//  新浪微博
//
//  Created by jose on 15-3-26.
//  Copyright (c) 2015年 jose. All rights reserved.
//  自定义输入文本框


#import <UIKit/UIKit.h>


@interface MyTextView : UITextView
//提示文本
@property(nonatomic,copy)NSString *placeholder;
//提示文本的颜色
@property(nonatomic,strong)UIColor *placeholdercolor;

@end


************************************************************************************************************************

***********************************************************************************************************************

***********************************************************************************************************************

//
//  MyTextView.m
//  新浪微博
//
//  Created by jose on 15-3-26.
//  Copyright (c) 2015年 jose. All rights reserved.
//


#import "MyTextView.h"
#import "UIView+Extension.h"


@interface MyTextView()
//用于显示输入的文本
@property(nonatomic,weak)UILabel *placehoderlabel;
@end
@implementation MyTextView


-(id)initWithFrame:(CGRect)frame{
    self=[super initWithFrame:frame];
    if (self) {
        //清空背景颜色
        self.backgroundColor=[UIColor clearColor];
        UILabel *label=[[UILabel alloc]init];
        //设置支持多行格式
        label.numberOfLines=0;
        label.backgroundColor=[UIColor clearColor];
        [self addSubview:label];
        //使用全局变量来记录
        self.placehoderlabel=label;
        //设置默认的占位字颜色
        self.placeholdercolor=[UIColor lightGrayColor];
        self.font=[UIFont systemFontOfSize:14];
        //注册一个通知中心,监听字体的改变
        [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(TextDidChange) name:UITextViewTextDidChangeNotification object:self];
    }
    return self;
}


//监听文本的改变
-(void)TextDidChange{
    if (self.text.length==0) {
        //显示占位文字
        self.placehoderlabel.hidden=NO;
    }
    else{
        //隐藏占位文字
        self.placehoderlabel.hidden=YES;
    }
    //self.placehoderlabel.hidden=(self.text.length!=0);
}
#pragma mark 重写方法
//设置占位字
-(void)setPlaceholder:(NSString *)placeholder{
    //如果是copy策略,setter最好这样写
    _placeholder=[placeholder copy];
    //设置文字
    self.placehoderlabel.text=placeholder;
    //重新计算文字的frame
    [self setNeedsLayout];
}
//设置占位字的颜色
-(void)setPlaceholdercolor:(UIColor *)placeholdercolor{
    _placeholdercolor=placeholdercolor;
    self.placehoderlabel.textColor=placeholdercolor;
}




-(void)layoutSubviews{
    [super layoutSubviews];
    self.placehoderlabel.y=8;
    self.placehoderlabel.x=5;
    self.placehoderlabel.width=self.width-2*self.placehoderlabel.x;
    //计算文字高度
    CGSize maxsize=CGSizeMake(self.placehoderlabel.width, MAXFLOAT);
    CGSize placehodersize=[self.placeholder sizeWithFont:self.placehoderlabel.font constrainedToSize:maxsize];
    self.placehoderlabel.height=placehodersize.height;
}
//设置字体的大小
-(void)setFont:(UIFont *)font{
    //该属性是继承来的,因此需要调用父类的方法
    [super setFont:font];
    //设置字体的大小
    self.placehoderlabel.font=font;
    //重新计算子控件的frame
    [self setNeedsLayout];
}
//设置文本的显示
-(void)setText:(NSString *)text{
    [super setText:text];
    [self TextDidChange];
}


@end

0 0
原创粉丝点击