新浪微博开发之十八(文本框)
来源:互联网 发布: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
- 新浪微博开发之十八(文本框)
- 新浪微博开发之十九(文本框工具栏)
- 新浪微博开发之二十八(原创微博frame)
- 新浪微博开发之php开发
- C++ MFC / VS2013 之十八(常用控件:静态文本框)
- 新浪微博开发之十四(AFNetworking的使用 )
- 新浪微博开发之十五(MBProgressHUD的使用)
- 新浪微博开发之十六(封装网络工具)
- 新浪微博开发之十七(图片工具)
- 新浪微博开发之二十七(全局变量)
- 新浪微博开发之三十五(微博frame)
- 新浪微博开发之三十六(微博view)
- IOS开发之新浪微博OAuth2
- Android新浪微博开发(三)完结篇之调用新浪微博API实现信息展示
- 新浪微博开发之微博主页的实现
- 新浪微博客户端开发之发布微博
- 新浪微博开发之项目分析与设计
- 新浪微博开发之授权的实现(一)
- NodeJS之异步编程(一)参数传递
- 浙江大学PAT_乙级_1038. 统计同成绩学生(20)
- 虚拟存储管理
- 【HTML】文字效果
- 使用NIO的直接缓存区复制文件
- 新浪微博开发之十八(文本框)
- poj2965 The Pilots Brothers' refrigerator
- Take advantage of TCP/IP options to optimize data transmission
- Express启动方式区别
- win7(64)+httpd2.4.12+mysql5.5.20+php5.6.7环境配置记录
- LeetCode 112: Path Sum
- 安卓快速集成支付功能
- node.js学习记录(1)
- 新浪微博开发之十九(文本框工具栏)