AJ分享,必须精品

一:效果

二:代码:

由于系统自带的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

//
// 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


// 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 之微博项目实战(11)发送微博自定义TextView实现带占位文字的更多相关文章

  1. AJ学IOS 之微博项目实战(13)发送微博调用相机里面的图片以及调用相机

    AJ分享,必须精品 一:效果 二:代码 相机部分就简单多了,几行代码调用而已,但是如果你要是想实现更多丰富的功能,需要自己写.利用AssetsLibrary.framework,利用这个框架可以获得手 ...

  2. AJ学IOS 之微博项目实战(12)发送微博自定义工具条代理实现点击事件

    AJ分享,必须精品 一:效果 二:封装好的工具条 NYComposeToolbar.h 带代理方法 #import <UIKit/UIKit.h> typedef enum { NYCom ...

  3. [iOS微博项目 - 3.2] - 发送微博

    github: https://github.com/hellovoidworld/HVWWeibo   A.使用微博API发送微博 1.需求 学习发送微博API 发送文字微博 发送带有图片的微博   ...

  4. AJ学IOS(17)UI之纯代码自定义Cell实现新浪微博UI

    AJ分享,必须精品 先看效果图 编程思路 代码创建Cell的步骤 1> 创建自定义Cell,继承自UITableViewCell 2> 根据需求,确定控件,并定义属性 3> 用get ...

  5. AJ学IOS(43)之网易彩票底部自定义TabBar实现切换

    AJ分享,必须精品 效果: 代码: NYTabBarController // // NYTabBarController.m // 彩票lottery // // Created by apple ...

  6. AJ学IOS 之微博项目实战(2)微博主框架-自定义导航控制器NavigationController

    AJ分享,必须精品 一:添加导航控制器 上一篇博客完成了对底部的TabBar的设置,这一章我们完成自定义导航控制器(NYNavigationController). 为啥要做自定义呢,因为为了更好地封 ...

  7. AJ学IOS 之微博项目实战(8)用AFNetworking和SDWebImage简单加载微博数据

    AJ分享,必须精品 一:效果 没有图文混排,也没有复杂的UI,仅仅是简单的显示出微博数据,主要介绍AFNetworking和SDWebImage的简单用法 二:加载数据AFNetworking AFN ...

  8. 猫猫学iOS 之微博项目实战(2)微博主框架-自己定义导航控制器NavigationController

    猫猫分享,必须精品 原创文章,欢迎转载.转载请注明:翟乃玉的博客 地址:http://blog.csdn.net/u013357243?viewmode=contents 一:加入导航控制器 上一篇博 ...

  9. AJ学IOS(13)UI之UITableView学习(下)汽车名牌带右侧索引

    AJ分享,必须精品 先看效果图 代码 ViewController #import "NYViewController.h" #import "NYCarGroup.h& ...

随机推荐

  1. 【简说Python WEB】flask-mail电子邮件异步Asynchronous

    系统环境:Ubuntu 18.04.1 LTS Python使用的是虚拟环境:virutalenv Python的版本:Python 3.6.9 flask-mail电子邮件异步Asynchronou ...

  2. Ubuntu的BEEP去哪里了?

    一直知道ubuntu的beep不响应了,但是一直都没太关注过它怎么了. 今天关注了一下,发现网上都是在问怎么关掉它的,时间还是在07年左右. 搜索到了一些帖子,有一些是说没有找到恢复的方法,还有一些, ...

  3. rbac权限(2)

    models.py from django.db import models class User(models.Model): name=models.CharField(max_length=32 ...

  4. poj1088 滑雪 dp+dfs记忆化

    简单的搜索,不必多说了,初始状态下每个点能到达的长度是1,它本身.还有,注意关掉文件重定向,被坑好多次了. 代码如下: #include<cstdio> #include<algor ...

  5. AQS机制

    一,Lock接口 锁是用来控制多个线程访问共享资源的方式,一般来说,一个锁能够防止多个线程同时访问共享资源(但是有些锁可以允许多个线程并发的访问共享资源,比如读写锁).在Lock接口出现之前,Java ...

  6. OpenLDAP 多主复制(基于docker容器模式部署)

    **本文主要讲述在docker环境下如何进行 OpenLDAP 多主复制,至于 OpenLDAP 原理可以先参考这篇文章了解:https://cloud.tencent.com/developer/a ...

  7. Docker镜像拉取慢的解决方法

    镜像加速器配置: 下文配置引用于阿里云说明文档:https://cr.console.aliyun.com/cn-hangzhou/instances/mirrors 1. 安装/升级Docker客户 ...

  8. A TensorBoard plugin for visualizing arbitrary tensors in a video as your network trains.

    Beholder is a TensorBoard plugin for viewing frames of a video while your model trains. It comes wit ...

  9. 83 项开源视觉 SLAM 方案够你用了吗?

    作者:吴艳敏 来源:83 项开源视觉 SLAM 方案够你用了吗? 前言 1. 本文由知乎作者小吴同学同步发布于https://zhuanlan.zhihu.com/p/115599978/并持续更新. ...

  10. Eclipse打包jar

    对一个包打jar包 右键包名-Export-Jar File-选择所在包的class文件(注意),如果选择java文件会失败-然后Finish 检查jar包是否正确,使用如jd-gui这样的反编译工具 ...