今天写界面的时候,突然发现,我们UI小姐姐设计占位文字的大小和颜色与软件中占位文字的大小和颜色不一样!!!What ?(黑人问号)
为了避免被其他人发现,我决定先下手为强,在没有人发现的时候就将它扼杀在摇篮里。
修改占位文字的大小和颜色
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];
}