textField对占位文本设置属性有限,在项目中需要改变占位文本的属性以及位置,需要自己对控件进行封装

封装方法如下:

在LDTextField.m 文件中:

#import <UIKit/UIKit.h>
typedef enum: NSInteger{ leftAlignment = 1, //占位字符串向左
middleAlignment = 2, //占位字符串居中
rightAlignment = 3 //占位字符串向右 }PlaceHoldTextAlignment; @interface LDTextField : UITextField @property(nonatomic,copy)NSString *placeHoldText; //位置类型
@property(nonatomic,assign)PlaceHoldTextAlignment placeHoldAlignment; //textfield字体颜色
@property(nonatomic,strong)UIColor *ldTextColor;
//textfield字体大小
@property(nonatomic,assign)float ldTextFont;
//textfield占位文本字体颜色
@property(nonatomic,strong)UIColor *ldTextHoldColor;
//textfield占位文本字体大小
@property(nonatomic,assign)float ldTextHoldFont;
//textfield光标颜色
@property(nonatomic,strong)UIColor *ldTintColor;
@end

在LDTextField.m 文件中 

#import "LDTextField.h"
#import "LDCalculationTool.h" @interface LDTextField ()
{
float _textPlaceHoldFont;
}
@end @implementation LDTextField -(void)drawRect:(CGRect)rect{ //占位文本
self.placeholder = self.placeHoldText; //字体颜色
if (self.ldTextColor) {
self.textColor = self.ldTextColor;
}else{
self.textColor = [UIColor blackColor];
} //字体大小
if (self.ldTextFont) {
self.font = [UIFont systemFontOfSize:self.ldTextFont];
}else{
self.font = [UIFont systemFontOfSize:16];
} //光标颜色
if (self.ldTintColor) {
self.textColor = self.ldTextColor;
}else{
self.tintColor = self.textColor;
} //占位文本字体颜色
if (self.ldTextHoldColor) {
[self setValue:self.ldTextHoldColor forKeyPath:@"_placeholderLabel.textColor"];
}else{
[self setValue:UIColorHex(#999999) forKeyPath:@"_placeholderLabel.textColor"];
} //占位文本字体大小
if (self.ldTextHoldFont) {
[self setValue:[UIFont boldSystemFontOfSize:self.ldTextHoldFont] forKeyPath:@"_placeholderLabel.font"];
_textPlaceHoldFont = self.ldTextHoldFont;
}else{
float holdFont = 16;
[self setValue:[UIFont boldSystemFontOfSize:holdFont] forKeyPath:@"_placeholderLabel.font"];
_textPlaceHoldFont = holdFont;
} } //控制placeholder的位置
-(CGRect)placeholderRectForBounds:(CGRect)bounds{
CGSize size;
if ([NSString isNotEmptyString:self.placeHoldText]) {
size = [LDCalculationTool stringSizeWithFont:[UIFont systemFontOfSize:_textPlaceHoldFont] preComputeSize:CGSizeMake(MAXFLOAT, bounds.size.height) withContent:self.placeHoldText];
} if (_placeHoldAlignment == middleAlignment && size.width <= bounds.size.width) {
CGRect inset = CGRectMake((bounds.size.width - size.width)/2, bounds.origin.y, size.width, bounds.size.height);
return inset;
}else if (_placeHoldAlignment == rightAlignment && size.width + 10 <= bounds.size.width){
CGRect inset = CGRectMake(bounds.size.width - size.width - 10, bounds.origin.y, size.width, bounds.size.height);
return inset;
}else{
CGRect inset = CGRectMake(bounds.origin.x+10, bounds.origin.y, bounds.size.width -15, bounds.size.height);
return inset;
} } //-(void)drawPlaceholderInRect:(CGRect)rect{
//
//} #pragma mark ---------------------可以修改占位文字的颜色-----------------------------
/**
以下功能暂时用不到,暂且注释
*/
//-(BOOL)becomeFirstResponder{
//// // 修改占位文字颜色
// [self setValue:[UIColor yellowColor] forKeyPath:@"_placeholderLabel.textColor"];
// return [super becomeFirstResponder];
//}
//
//-(BOOL)resignFirstResponder{
//// // 修改占位文字颜色
// [self setValue:[UIColor grayColor] forKeyPath:@"_placeholderLabel.textColor"];
// return [super resignFirstResponder];
//} @end

对于 LDCalculationTool.h 导入的文件中,只占用了一个方法,方法如下,用于计算占位文本的宽度  

+ (CGSize)stringSizeWithFont:(UIFont *)font preComputeSize:(CGSize)preSize withContent:(NSString *)str{
CGSize stringSize = [str boundingRectWithSize:CGSizeMake(MAXFLOAT, preSize.height) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName: font} context:nil].size; return stringSize;
}

以上封装内容如有补充,请观看的童鞋们发消息告知!!!

 

以下内容对于灵活修改textField中文本以及占位文本属性进行了完整的封装,加入项目中可以节约开发时间。的更多相关文章

  1. 装载:对CSS中的Position、Float属性的一些深入探讨

    对CSS中的Position.Float属性的一些深入探讨   对CSS中的Position.Float属性的一些深入探讨 对于Position.Float我们在平时使用上可以说是使用频率非常高的两个 ...

  2. JS事件 内容选中事件(onselect)选中事件,当文本框或者文本域中的文字被选中时,触发onselect事件,同时调用的程序就会被执行。

    内容选中事件(onselect) 选中事件,当文本框或者文本域中的文字被选中时,触发onselect事件,同时调用的程序就会被执行. 如下代码,当选中用户文本框内的文字时,触发onselect 事件, ...

  3. /etc/resolv.conf文件中的search项作用;如何保持resolv.conf文件内容不被修改

    /etc/resolv.conf文件中的search项作用 resolv.conf文件中有search项时,主机名解析规则顺序: DNS配置文件如下: # cat /etc/resolv.conf ; ...

  4. placeholder 解决UITextField中placeholder和text文本同时显示的问题

    TextField都使用了placeholder属性,但在代码中又设置了text属性,因此ViewController会同时显示placeholder文本和text文本. 这个问题让我彻底崩溃.按道理 ...

  5. 在java项目中加入百度富文本编辑器

    富文本编辑器在项目中很常见,他可以将文本,图片等信息存入数据库,在编辑一些图文混排的信息的时候很有用,比如商城项目的商品详情页,包含很多带有样式的文字和图片. 此前一直使用的百度的富文本编辑器uedi ...

  6. vue2.0项目中使用Ueditor富文本编辑器示例

    最近在vue项目中需要使用富文本编辑器,于是将Ueditor集成进来,作为公共组件. 在线预览:https://suweiteng.github.io/vue2-management-platform ...

  7. springboot中使用kindeditor富文本编辑器实现博客功能

    kindeditor在之前已经用过,现在在springboot项目中使用.并且也在里面使用了图片上传以及回显等功能. 其实主要的功能是图片的处理:kindeditor对输入的内容会作为html标签处理 ...

  8. DOM节点中获取文本易混淆的属性

    DOM 节点中对于获取文本易混淆的属性,innerText, innerHTML, outerHTML, textContent, nodeValue. 一个实例: <!DOCTYPE html ...

  9. Android笔记(十三) Android中的基本组件——文本

    Android中常用的文本组件有 普通文本框(TextView)和编辑框(EditText)两种 EditText是TextView的子类,作用就是在界面上显示文本,区别是EditText允许用户编辑 ...

随机推荐

  1. Yii2 关于yii\db\Connection::open耗时很久的问题(1000ms左右)

    关于这个问题我纠结了很久,每次打开网页yii\db\Connection::open几乎都耗时1000ms. 其实这个问题很好解决:只要把config\db.php配置信息里的localhost,改成 ...

  2. The Karting 2017ccpc网络赛 1008

    The Karting championship will be held on a straight road. There are N keypoints on the road. The pat ...

  3. python增量爬虫pyspider

    1.为了能够将爬取到的数据存入本地数据库,现在本地创建一个MySQL数据库example,然后 在数据库中建立一张表格test,示例如下: DROP TABLE IF EXISTS `test`; C ...

  4. SVG文本

    前面的话 本文将详细介绍SVG文本相关内容 位置属性 在一个SVG文档中,使用<text>元素来设置文本,<text>元素有x.y.dx.dy这四个位置属性 [x和y] 属性x ...

  5. jmeter系列------参数关联

    接口请求中的一个变量需要用上一个接口道服务器返回响应的动态值(上个请求). 遇到这样的情况,我们就需要用到关联 例如用户A发表了一个一条微信朋友圈,用户B想对这条朋友圈进行评论,就需要先获取到这个朋友 ...

  6. Entity Framework Core 2.0 中使用LIKE 操作符

    Entity Framework Core 2.0 中使用LIKE 操作符 不定时更新翻译系列,此系列更新毫无时间规律,文笔菜翻译菜求各位看官老爷们轻喷,如觉得我翻译有问题请挪步原博客地址 本博文翻译 ...

  7. Grails笔记一:windows8.1操作系统cmd下grails命令无效

    最近决定使用intellij idea工具来开发基于Grails框架的应用程序,,在安装好jdk 1.7以及配置好jdk和grails环境变量后进行grails测试,在win8.1的命令行窗口中输入以 ...

  8. Struts2中Action接收参数的方法

    详见:http://blog.yemou.net/article/query/info/tytfjhfascvhzxcyt112 Struts2中Action接收参数的方法主要有以下三种: 1.使用A ...

  9. 教你自己搭建linux邮箱服务器

    详见:http://blog.yemou.net/article/query/info/tytfjhfascvhzxcyt290 现在网络中流行的电子邮件系统主要有Microsoft Exchange ...

  10. poj 1200字符串hash

    题意:给出不同字符个数和子串长度,判断有多少个不同的子串 思路:字符串hash. 用字符串函数+map为什么会超时呢?? 代码: #include <iostream> #include ...