RunTime運行時在iOS中的應用之UITextField預留位置placeholder

來源:互聯網
上載者:User

標籤:uicolor   拷貝   fork   尋找   運行   super   列印   value   第一次用   

RunTime運行時機制

runtime是一套比較底層的純C語言API, 屬於1個C語言庫, 包含了很多底層的C語言API。

在我們平時編寫的OC代碼中, 程式運行過程時, 其實最終都是轉成了runtime的C語言代碼, runtime算是OC的幕後工作者,下面介紹一下runtime的一個應用用於遍曆出UITextField的有那些隱藏屬性,查出後再通過KVC來進行修改這個屬性

 

//第一次用到這類的時候就會調用的只會調用一次方法 ,這個方法查的時候用一下 ,以後不用

+ (void)initialize{

    unsigned int count = 0;

    // 拷貝出所有的成員變數列表   ivars是指向這個數組的指標也是指向首個元素的指標

    Ivar *ivars = class_copyIvarList([UITextField class], &count);

    for (int i = 0; i < count; ++i) {

        //數組名其實就是指向數組首元素的指標  如果指標是指向數組的首元素,就可把指標當數組一樣用

        Ivar t = ivars[i];

        //列印成員變數名  尋找到   _placeholderLabel

        NSLog(@"%s",ivar_getName(t));

    }

    // 釋放指標變數 ivars 是拷貝過的所以用完要釋放

    free(ivars);

}

 

- (instancetype)initWithFrame:(CGRect)frame{

    if(self = [super initWithFrame:frame]){

        [self setupUI];

    }

    return self;

}

 

- (void)awakeFromNib{

    [super awakeFromNib];

    [self setupUI];

}

 

- (void)setupUI{

    self.textColor = [UIColor whiteColor];

    //tintColor 可設定游標的顏色等

    self.tintColor = self.textColor;

    

    //一載入這個TextField時讓裡面的佔位字元變灰色 就讓文本輸入框失去焦點z(因為在重寫失去焦點時設定了文字變灰色)

    [self resignFirstResponder];

}

 

// 當文本輸入框成為第一響應者就會調用

- (BOOL)becomeFirstResponder{

    [self setValue:self.textColor forKeyPath:@"_placeholderLabel.textColor"];

    return [super becomeFirstResponder];

}

 

// 當文本輸入框失去焦點的時個會調用的方法

- (BOOL)resignFirstResponder{

    //通過 KVC 訪問 _placeholderLabel.textColor 屬性 設定顏色

    [self setValue:[UIColor grayColor] forKeyPath:@"_placeholderLabel.textColor"];

    return [super resignFirstResponder];

}

 

RunTime運行時在iOS中的應用之UITextField預留位置placeholder

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.