美文网首页
TextField封装(占位文本位置可变)

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

作者: 丨十三叔丨 | 来源:发表于2017-09-18 11:26 被阅读0次

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

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

相关文章

网友评论

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

      本文链接:https://www.haomeiwen.com/subject/apgvsxtx.html