今天寫界面的時(shí)候荔睹,突然發(fā)現(xiàn),我們UI小姐姐設(shè)計(jì)占位文字的大小和顏色與軟件中占位文字的大小和顏色不一樣=鸲取Sγ摹严沥!What 猜极?(黑人問(wèn)號(hào))
為了避免被其他人發(fā)現(xiàn),我決定先下手為強(qiáng)消玄,在沒(méi)有人發(fā)現(xiàn)的時(shí)候就將它扼殺在搖籃里跟伏。
修改占位文字的大小和顏色
修改占位文字的大小為16,顏色為紫色
1.富文本
NSMutableDictionary *attDic = [@{NSForegroundColorAttributeName:[UIColor purpleColor], NSFontAttributeName:[UIFont systemFontOfSize:16]} mutableCopy];
NSMutableAttributedString *attPlace = [[NSMutableAttributedString alloc] initWithString:@"富文本修改占位文字的大小和顏色" attributes:attDic];
_textField.attributedPlaceholder = attPlace;
2.KVC( Runtime
獲取私有的屬性名稱)
_textField.placeholder = @"KVC修改占位文字的大小和顏色";
[_textField setValue:[UIColor purpleColor] forKeyPath:@"_placeholderLabel.textColor"];
[_textField setValue:[UIFont boldSystemFontOfSize:16] forKeyPath:@"_placeholderLabel.font"];
3.UILabel的顯示與隱藏
#define SCREENWIDTH [UIScreen mainScreen].bounds.size.width
#define SCREENHEIGHT [UIScreen mainScreen].bounds.size.height
#define X(x) (SCREENWIDTH / 375.0 * x)
@property (nonatomic, strong) UITextField *textField;
@property (nonatomic, strong) UILabel *thLabel;
_textField = [[UITextField alloc] initWithFrame:CGRectMake(SCREENWIDTH / 2 - X(300) / 2, X(180), X(300), X(40))];
_textField.backgroundColor = [UIColor clearColor];
_textField.clearButtonMode = UITextFieldViewModeNever;
_textField.textAlignment = NSTextAlignmentLeft;
_textField.clearsOnBeginEditing = NO;
_textField.tintColor = [UIColor redColor];
_textField.textColor = [UIColor blackColor];
_textField.borderStyle = UITextBorderStyleRoundedRect;
[self.view addSubview:self.textField];
UILabel *thLabel = [[UILabel alloc] init];
thLabel.frame = CGRectMake(X(10), 0, X(280), X(40));
thLabel.textColor = [UIColor purpleColor];
thLabel.text = @"UILabel修改占位文字的大小和顏色";
thLabel.font = [UIFont systemFontOfSize:14];
[_textField addSubview:thLabel];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(textFiledEditChanged:) name:UITextFieldTextDidChangeNotification object:nil];
self.thLabel = thLabel;
- (void)textFiledEditChanged:(NSNotification *)obj {
if ([self.textField.text length] > 0) {
self.thLabel.hidden = YES;
} else {
self.thLabel.hidden = NO;
}
}
- (void)dealloc
{
[[NSNotificationCenter defaultCenter] removeObserver:self name:UITextFieldTextDidChangeNotification object:nil];
}