以下内容对于灵活修改textField中文本以及占位文本属性进行了完整的封装,加入项目中可以节约开发时间。
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中文本以及占位文本属性进行了完整的封装,加入项目中可以节约开发时间。的更多相关文章
- 装载:对CSS中的Position、Float属性的一些深入探讨
对CSS中的Position.Float属性的一些深入探讨 对CSS中的Position.Float属性的一些深入探讨 对于Position.Float我们在平时使用上可以说是使用频率非常高的两个 ...
- JS事件 内容选中事件(onselect)选中事件,当文本框或者文本域中的文字被选中时,触发onselect事件,同时调用的程序就会被执行。
内容选中事件(onselect) 选中事件,当文本框或者文本域中的文字被选中时,触发onselect事件,同时调用的程序就会被执行. 如下代码,当选中用户文本框内的文字时,触发onselect 事件, ...
- /etc/resolv.conf文件中的search项作用;如何保持resolv.conf文件内容不被修改
/etc/resolv.conf文件中的search项作用 resolv.conf文件中有search项时,主机名解析规则顺序: DNS配置文件如下: # cat /etc/resolv.conf ; ...
- placeholder 解决UITextField中placeholder和text文本同时显示的问题
TextField都使用了placeholder属性,但在代码中又设置了text属性,因此ViewController会同时显示placeholder文本和text文本. 这个问题让我彻底崩溃.按道理 ...
- 在java项目中加入百度富文本编辑器
富文本编辑器在项目中很常见,他可以将文本,图片等信息存入数据库,在编辑一些图文混排的信息的时候很有用,比如商城项目的商品详情页,包含很多带有样式的文字和图片. 此前一直使用的百度的富文本编辑器uedi ...
- vue2.0项目中使用Ueditor富文本编辑器示例
最近在vue项目中需要使用富文本编辑器,于是将Ueditor集成进来,作为公共组件. 在线预览:https://suweiteng.github.io/vue2-management-platform ...
- springboot中使用kindeditor富文本编辑器实现博客功能
kindeditor在之前已经用过,现在在springboot项目中使用.并且也在里面使用了图片上传以及回显等功能. 其实主要的功能是图片的处理:kindeditor对输入的内容会作为html标签处理 ...
- DOM节点中获取文本易混淆的属性
DOM 节点中对于获取文本易混淆的属性,innerText, innerHTML, outerHTML, textContent, nodeValue. 一个实例: <!DOCTYPE html ...
- Android笔记(十三) Android中的基本组件——文本
Android中常用的文本组件有 普通文本框(TextView)和编辑框(EditText)两种 EditText是TextView的子类,作用就是在界面上显示文本,区别是EditText允许用户编辑 ...
随机推荐
- BotVS开发基础—2.7 指标MA
代码 #计算一小时 MA5均线 # 时间 2017-06-29 00:00:00 -2017-06-30 00:00:00 1小时 实盘级Tick # 平台 OCKCoin BTC def main( ...
- “最美天气”版本II
抓取实时天气前需要首先获得所有地区的cityCode,因为在最后queryWeather的时候需要传递这个参数. 最美天气获取全部cityCode及cityName的方法: 1.获取34个省及直辖市的 ...
- java --jfree报表
---恢复内容开始--- 1.使用的报表工具: jfree报表 2.下载网址: http://www.jfree.org/ 下载之后先解压:如下图 下载后:需要的jar包!如下图: 打开:找到以下的两 ...
- jsp窗口关闭的触发函数
功能:当点击窗口[关闭按钮]时调用后台的servlet方法实现附件删除 <script type="text/javascript"> window.onbefo ...
- 在htnl中,<input tyle = "text">除了text外还有几种种新增的表单元素
input标签新增属性 <input list='list_t' type="text" name='user' placeholder='请输入姓名' va ...
- CSS 中的 initial、inherit、unset、revert
在css中,initial(初始).inherit(继承).unset(未设置).revert(还原)这四个关键字可以应用于所有的CSS属性. initial - 初始默认值.IE不支持 inheri ...
- 日志log4j配置详情,日志log具体到你想不到
一.Log4j简介Log4j有三个主要的组件:Loggers(记录器),Appenders (输出源)和Layouts(布局). 1.LoggersLoggers组件在此系统中被分为五个级别:DEBU ...
- [iOS开发]Xcode8兼容iOS7以及低版本Xcode调试高版本iOS系统
现在的项目一般都要兼容iOS7系统,同时也要兼容iOS10,在Xcode8上面,默认情况下无法调试iOS7,因为缺乏调试iOS7需要的配置文件.同时在低版本的Xcode上面(8以下),也无法调试iOS ...
- Unity3D安装破解教程(以Unity5.3.4为例)(转)
Unity3D是由Unity Technologies开发的一个让玩家轻松创建诸如三维视频游戏.建筑可视化.实时三维动画等类型互动内容的多平台的综合型游戏开发工具,是一个全面整合的专业游戏引擎.目 ...
- JavaScript--我发现,原来你是这样的JS(基础概念--灵魂篇,一起来学js吧)
介绍 这是红宝书(JavaScript高级程序设计 3版)的读书笔记第三篇(灵魂篇介绍),有着剩下的第三章的知识内容,当然其中还有我个人的理解.红宝书这本书可以说是难啃的,要看完不容易,挺厚的,要看懂 ...