UITextField-修改占位文字和光標(biāo)的顏色,大小

版權(quán)聲明:本文為博主原創(chuàng)文章,轉(zhuǎn)載請(qǐng)附上原文出處鏈接和本聲明。
本文鏈接:https://yotrolz.com/posts/552ed0af/


一.設(shè)置占位文字的顏色

方法一:利用富文本


/** 手機(jī)號(hào)輸入框 */
@property (weak, nonatomic) IBOutlet UITextField *phoneTextField;

- (void)viewDidLoad {
    [super viewDidLoad];
    // 創(chuàng)建一個(gè)富文本對(duì)象
    NSMutableDictionary *attributes = [NSMutableDictionary dictionary];
    // 設(shè)置富文本對(duì)象的顏色
    attributes[NSForegroundColorAttributeName] = [UIColor whiteColor];
    // 設(shè)置UITextField的占位文字
    self.phoneTextField.attributedPlaceholder = [[NSAttributedString alloc] initWithString:@"手機(jī)號(hào)" attributes:attributes];
    
}

方法二:利用Runtime獲取私有的屬性名稱,利用KVC設(shè)置屬性

// 設(shè)置占位文字的顏色為紅色(注意下面的'self'代表你要修改占位文字的UITextField控件)
[self setValue:[UIColor redColor] forKeyPath:@"_placeholderLabel.textColor"];
  • 注意:_placeholderLabel.textColor是不可亂寫的哦,我們是怎么獲取到這個(gè)屬性的呢?請(qǐng)看下文:
// 只調(diào)用一次(自定義UITextField)
+ (void)initialize {

    [self getIvars];
    
}

// 獲取私有變量名稱
+ (void)getIvars {
    
    unsigned int count = 0;
    
    Ivar *ivars = class_copyIvarList([UITextField class], &count);
    
    for (int i = 0; i < count; i++) {
        Ivar ivar = ivars[i];
        
        NSLog(@"%s----%s", ivar_getName(ivar), ivar_getTypeEncoding(ivar));
    }
}

查看打印,找出可能的屬性名稱,試試便知;

  • 完整代碼:自定義的UITextField,獲取到焦點(diǎn)(編輯狀態(tài))的時(shí)候是白色,失去焦點(diǎn)(非編輯狀態(tài))的時(shí)候是灰色:
#import "YCTextField.h"
#import <objc/runtime.h>

#define YCplaceholderTextColor @"_placeholderLabel.textColor"

@implementation YCTextField

+ (void)initialize {

    [self getIvars];
    
}

// 獲取私有變量名稱
+ (void)getIvars {
    
    unsigned int count = 0;
    
    Ivar *ivars = class_copyIvarList([UITextField class], &count);
    
    for (int i = 0; i < count; i++) {
        Ivar ivar = ivars[i];
        
        NSLog(@"%s----%s", ivar_getName(ivar), ivar_getTypeEncoding(ivar));
    }
}

- (void)awakeFromNib {

    // 設(shè)置光標(biāo)的顏色
    self.tintColor = self.textColor;
}

// 獲取到焦點(diǎn)
- (BOOL)becomeFirstResponder {

    // 利用運(yùn)行時(shí)獲取key,設(shè)置占位文字的顏色
    [self setValue:self.textColor forKeyPath:YCplaceholderTextColor];
    
    return [super becomeFirstResponder];
}

// 失去焦點(diǎn)
- (BOOL)resignFirstResponder {

    // 利用運(yùn)行時(shí)獲取key,設(shè)置占位文字的顏色
    [self setValue:[UIColor grayColor] forKeyPath:YCplaceholderTextColor];
    
    return [super resignFirstResponder];
}

@end

方法三.將占位文字上去(重寫- (void)drawPlaceholderInRect:(CGRect)rect;)

- (void)drawPlaceholderInRect:(CGRect)rect
{

    [[UIColor orangeColor] set];
    
    [self.placeholder drawInRect:rect withFont:[UIFont systemFontOfSize:20]];
}

二.設(shè)置光標(biāo)顏色

// 設(shè)置光標(biāo)的顏色
self.tintColor = [UIColor redColor];

三.設(shè)置占位文字的偏移

  • 重寫-(CGRect)placeholderRectForBounds:(CGRect)bounds;方法
  • 可以用來(lái)設(shè)置光標(biāo)與占位的間距
//控制placeHolder的位置,左右縮20
-(CGRect)placeholderRectForBounds:(CGRect)bounds
{
    
    //return CGRectInset(bounds, 20, 0);
    CGRect inset = CGRectMake(bounds.origin.x+50, bounds.origin.y, bounds.size.width -10, bounds.size.height);//更好理解些
    return inset;
}
  • 擴(kuò)充:系統(tǒng)還提供了很多類似的方法
    • – textRectForBounds:  //重寫來(lái)重置文字區(qū)域
    • – drawTextInRect:    //改變繪文字屬性.重寫時(shí)調(diào)用super可以按默認(rèn)圖形屬性繪制,若自己完全重寫繪制函數(shù),就不用調(diào)用super了.
    • – placeholderRectForBounds:  //重寫來(lái)重置占位符區(qū)域
    • – drawPlaceholderInRect:  //重寫改變繪制占位符屬性.重寫時(shí)調(diào)用super可以按默認(rèn)圖形屬性繪制,若自己完全重寫繪制函數(shù),就不用調(diào)用super了
    • – borderRectForBounds:  //重寫來(lái)重置邊緣區(qū)域
    • – editingRectForBounds:  //重寫來(lái)重置編輯區(qū)域
    • – clearButtonRectForBounds:  //重寫來(lái)重置clearButton位置,改變size可能導(dǎo)致button的圖片失真
    • – leftViewRectForBounds:
    • – rightViewRectForBounds:
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容