TextField封装(占位文本位置可变)

以下内容对于灵活修改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;
}

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

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 发现 关注 消息 iOS 第三方库、插件、知名博客总结 作者大灰狼的小绵羊哥哥关注 2017.06.26 09:4...
    肇东周阅读 12,232评论 4 61
  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 173,284评论 25 708
  • 最近一段时间,社会上关于幼儿教育的话题比较多,对其中的一些问题存在比较大的争议。但是,我们也不必太过担忧,前天我在...
    桑榆老师阅读 2,112评论 3 6
  • 穿衣搭配算法比赛终于要用UDF了,赶紧配置一个。发现官网上的介绍已经跟不上ODPS发展的步伐了,如果之前不是对Ma...
    yfwz100阅读 7,981评论 2 2
  • 今天起来有点感想。Amy的课可以是一种方法也可以是一种理念。 首先方法我没有用的很好,一开始方法用不好,反而还邯郸...
    幸福满屋Emily阅读 757评论 1 1