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,252評論 4 61
  • Android 自定義View的各種姿勢1 Activity的顯示之ViewRootImpl詳解 Activity...
    passiontim閱讀 173,523評論 25 708
  • 最近一段時間,社會上關于幼兒教育的話題比較多,對其中的一些問題存在比較大的爭議。但是,我們也不必太過擔憂,前天我在...
    桑榆老師閱讀 2,113評論 3 6
  • 穿衣搭配算法比賽終于要用UDF了,趕緊配置一個。發現官網上的介紹已經跟不上ODPS發展的步伐了,如果之前不是對Ma...
    yfwz100閱讀 8,000評論 2 2
  • 今天起來有點感想。Amy的課可以是一種方法也可以是一種理念。 首先方法我沒有用的很好,一開始方法用不好,反而還邯鄲...
    幸福滿屋Emily閱讀 758評論 1 1