以下内容对于灵活修改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允许用户编辑 ...
随机推荐
- ssh环境的搭建,基于注解和配置文件使用
搭建spring.Struts2.hibernate三大框架的环境 这里分两部分来讲:一.用myeclipse 2014 快速搭建环境,非常快捷, 大部分配置文件信息系统都帮我们写好,建议老手使用 二 ...
- Visual Studio Code 配置C++环境
Visual Studio Code (简称 VS Code / VSC) 是一款免费开源的现代化轻量级代码编辑器,支持几乎所有主流的开发语言的语法高亮.智能代码补全.自定义热键.括号匹配.代码片段. ...
- PyQt QFontDialog显示中文
QApplication 加载Translator app = QtWidgets.QApplication(sys.argv) translator = QtCore.QTranslator() i ...
- JavaScript笔记之第一天
JavaScript 1.JavaScript 显示数据 JavaScript 可以通过不同的方式来输出数据: 使用 window.alert() 弹出警告框. 使用 document.write() ...
- python基础教程(二)
继续第一篇的内容,讲解,python的一些基本的东西. 注释 为了让别人能够更容易理解程序,使用注释是非常有效的,即使是自己回头再看旧代码也是一样. >>> #获得用户名: > ...
- DateTable转化为泛型集合
public class ListUtil { public static List<T> ToList<T>(DataTable dt) { List<T> li ...
- java 利用jna调用c#的dll
一.需求阐述: 如果我们的项目利用c#开发,到了开发后期需要和java组进行合作,其中有一部分业务逻辑利用c#已经code completed,那么我们可能会考虑用java来调用现成的c#dll实现需 ...
- h5新增html标签语义
H5新增常用标签<body> <header>...</header> <nav>...</nav> <article> < ...
- 最新城市二级联动json(2017-09)
{ '安徽': [ '合肥', '芜湖', '蚌埠', '淮南', '马鞍山', '淮北', '铜陵', '安庆', '黄山', '阜阳', '宿州', '滁州', '六安', '宣城', '池州', ...
- JQuery实用技巧--学会你也是大神(1)——插件的制作技巧
前 言 JRedu 学习之前,首先我们需要知道什么是JQuery? JQuery是一个优秀的javascript框架. JQuery是继Prototype之后又一个优秀的Javascript框架 ...