AJ学IOS 之微博项目实战(11)发送微博自定义TextView实现带占位文字
2015-11-29 00:45
946 查看
AJ分享,必须精品
UITextField:
1.文字永远是一行,不能显示多行文字
2.有placehoder属性设置占位文字
3.继承自UIControl
4.监听行为
1> 设置代理
2> addTarget:action:forControlEvents:
3> 通知:UITextFieldTextDidChangeNotification
UITextView:
1.能显示任意行文字
2.不能设置占位文字
3.继承自UIScollView
4.监听行为
1> 设置代理
2> 通知:UITextViewTextDidChangeNotification
一:效果
二:代码:
由于系统自带的UITextField:和UITextView:不能满足我们的需求,所以我们需要自己设计一个。UITextField:
1.文字永远是一行,不能显示多行文字
2.有placehoder属性设置占位文字
3.继承自UIControl
4.监听行为
1> 设置代理
2> addTarget:action:forControlEvents:
3> 通知:UITextFieldTextDidChangeNotification
UITextView:
1.能显示任意行文字
2.不能设置占位文字
3.继承自UIScollView
4.监听行为
1> 设置代理
2> 通知:UITextViewTextDidChangeNotification
NYTextView.h
[code]// // Created by apple on 14-10-20. // Copyright (c) 2014年 heima. All rights reserved. // 增强:带有占位文字 #import <UIKit/UIKit.h> @interface NYTextView : UITextView /** 占位文字 */ @property (nonatomic, copy) NSString *placeholder; /** 占位文字的颜色 */ @property (nonatomic, strong) UIColor *placeholderColor; @end
NYTextView.m
[code] // Created by apple on 14-10-20. // Copyright (c) 2014年 heima. All rights reserved. // #import "NYTextView.h" @implementation NYTextView - (id)initWithFrame:(CGRect)frame { self = [super initWithFrame:frame]; if (self) { // 不要设置自己的delegate为自己 // self.delegate = self; // 通知 // 当UITextView的文字发生改变时,UITextView自己会发出一个UITextViewTextDidChangeNotification通知 [NYNotificationCenter addObserver:self selector:@selector(textDidChange) name:UITextViewTextDidChangeNotification object:self]; } return self; } - (void)dealloc { [NYNotificationCenter removeObserver:self]; } /** * 监听文字改变 */ - (void)textDidChange { // 重绘(重新调用) [self setNeedsDisplay]; } - (void)setPlaceholder:(NSString *)placeholder { _placeholder = [placeholder copy]; [self setNeedsDisplay]; } - (void)setPlaceholderColor:(UIColor *)placeholderColor { _placeholderColor = placeholderColor; [self setNeedsDisplay]; } - (void)setText:(NSString *)text { [super setText:text]; // setNeedsDisplay会在下一个消息循环时刻,调用drawRect: [self setNeedsDisplay]; } - (void)setFont:(UIFont *)font { [super setFont:font]; [self setNeedsDisplay]; } - (void)drawRect:(CGRect)rect { // [NYRandomColor set]; // UIRectFill(CGRectMake(20, 20, 30, 30)); // 如果有输入文字,就直接返回,不画占位文字 if (self.hasText) return; // 文字属性 NSMutableDictionary *attrs = [NSMutableDictionary dictionary]; attrs[NSFontAttributeName] = self.font; attrs[NSForegroundColorAttributeName] = self.placeholderColor?self.placeholderColor:[UIColor grayColor]; // 画文字 // [self.placeholder drawAtPoint:CGPointMake(5, 8) withAttributes:attrs]; CGFloat x = 5; CGFloat w = rect.size.width - 2 * x; CGFloat y = 8; CGFloat h = rect.size.height - 2 * y; CGRect placeholderRect = CGRectMake(x, y, w, h); [self.placeholder drawInRect:placeholderRect withAttributes:attrs]; } @end
相关文章推荐
- AJ学IOS 之微博项目实战(10)微博cell中图片的显示以及各种填充模式简介
- AJ学IOS 之微博项目实战(9)微博模型之时间相关重要操作,判断刚刚,昨天,今年等等
- AJ学IOS 之控制器view显示中view的父子关系及controller的父子关系_解决屏幕旋转不能传递事件问题
- AJ学IOS 之CoreLocation反地理编码小Demo输入经纬度得到城市
- AJ学IOS 之CoreLocation地理编码小Demo输入城市得到经纬度
- AJ学IOS 之CoreLocation指南针小应用
- AJ学IOS 之CoreLocation基本使用
- AJ学IOS 之第一次打开Xcode_git配置,git简单学习
- AJ学IOS 之微博项目实战(8)用AFNetworking和SDWebImage简单加载微博数据
- AJ学IOS 之微博项目实战(6)导航控制器NavigationController 的滑动回退功能实现
- AJ学IOS 之微博项目实战(5)微博自定义搜索框searchBar
- AJ学IOS 之微博项目实战(4)微博自定义tabBar中间的添加按钮
- AJ学IOS 之小知识之_xcode插件的删除方法_自动提示图片插件KSImageNamed有时不灵_分类或宏之类不能自动提示,
- AJ学IOS 之小知识之xcode6自动提示图片插件 KSImageNamed的安装
- AJ学IOS 之小知识iOS启动动画_Launch Screen的运用
- AJ学IOS 之微博项目实战(2)微博主框架-自定义导航控制器NavigationController
- AJ学IOS 之微博项目实战(1)微博主框架-子控制器的添加
- AJ学IOS(46)之网易彩票幸运大转盘
- AJ学IOS(45)之常用的小功能比如打电话、打开网址、发邮件、发短信打开其他应用。
- AJ学IOS(44)之网易彩票自定义图片在右边的Button_弹出view_ios6,7简单适配