以下內容對于靈活修改textField中文本以及占位文本屬性進行了完整的封裝,加入項目中可以節約開發時間。
LDTextField.h 文件中
#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;
}
以上封裝內容如有補充,請觀看的童鞋們發消息告知?。?!