//通過runtime,我們發(fā)現(xiàn)UITextField有一個叫做_placeholderLabel的私有變量且改,下面來看下:
unsignedintcount =0;
Ivar*ivars =class_copyIvarList([UITextFieldclass], &count);
NSLog(@"count%d",count);
for(inti =0; i < count; i++) {
Ivarivar = ivars[i];
constchar*name =ivar_getName(ivar);
NSString*objcName = [NSStringstringWithUTF8String:name];
NSLog(@"%d : %@",i,objcName);
}
通過上面的代碼已經(jīng)證實確實有_placeholderLabel验烧,這個私有變量,現(xiàn)在我們可以通過KVC來訪問是有變量钾虐,并設(shè)置placeholder的顏色噪窘、字體等屬性
UITextField*textField = [[UITextFieldalloc]initWithFrame:CGRectMake(0,100, [UIScreenmainScreen].bounds.size.width,30)];
[textFieldsetBackgroundColor:[UIColorgrayColor]];
[self.viewaddSubview:textField];
//_placeholderLabel
UILabel*placeHolderLabel = [[UILabelalloc]init];
placeHolderLabel.text=@"請輸入文字";
placeHolderLabel.numberOfLines=0;
placeHolderLabel.textColor= [UIColorredColor];
[placeHolderLabelsizeToFit];
[textFieldaddSubview:placeHolderLabel];
// ?字體保持一致
textField.font= [UIFontsystemFontOfSize:13.f];
placeHolderLabel.font= [UIFontsystemFontOfSize:13.f];
[textFieldsetValue:placeHolderLabelforKey:@"_placeholderLabel"];
}