1.需求說明
項目中經(jīng)常會遇到各種各樣的需求膀息,比如輸入框透明捧灰,邊框以及內(nèi)部的placeholder為白色喳钟,再或者placeholder要求居中律想。
廣大的iOS開發(fā)人員可能會想到各種方法,本人只舉一些設(shè)置placeholder的顏色以及字體大小的常見用法流济。好锐锣,各位同行坐穩(wěn)了,準(zhǔn)備開車。
2 代碼演示
2.1 iOS 6.0之前
//顏色設(shè)置
[self.textField setValue:color forKeyPath:@"_placeholderLabel.textColor"];
//字體大小
[self.textField setValue:[UIFont boldSystemFontOfSize:fontSize] forKeyPath:@"_placeholderLabel.font"];
設(shè)置placeholder的居中方式的時候绳瘟,通過該方法設(shè)置如下雕憔,會報錯,知道的大神可以幫忙晚上一下.
[textField setValue:[NSNumber numberWithInteger:NSTextAlignmentCenter] forKeyPath:@"_placeholderLabel.textAligment"];
2.2 iOS6之后 attributedPlaceholder屬性
蘋果官方文檔原文對該屬性的描述:
Discussion
This property is nil by default. If set, the placeholder string is drawn using a 70% gray color and the remaining style information (except the text color) of the attributed string. Assigning a new value to this property also replaces the value of the placeholder property with the same string data, albeit without any formatting information. Assigning a new value to this property does not affect any other style-related properties of the text field.
譯文[1]
該屬性默認(rèn)是空的糖声。如果設(shè)置了的話斤彼,如果設(shè)置了該屬性,占位符( placeholder)會按照70%灰度顏色繪制蘸泻,并按照富文本(attributed string)的樣式信息保持樣式(不包括文本顏色)琉苇。盡管沒有任何的樣式設(shè)置,但是給這個屬性分配一個新的值會使用一個新的字符串?dāng)?shù)據(jù)替代占位符中的值悦施。給該屬性分配一個新的值并不會對輸入框任何一個相關(guān)的樣式屬性造成影響并扇。
在設(shè)置placeholder的對齊方式的時候也使用了NSMutableParagraphStyle類。
Overview
The NSMutableParagraphStyle class adds methods to its superclass, NSParagraphStyle, for changing the values of the subattributes in a paragraph style attribute.See the NSParagraphStyle and NSAttributedString specifications for more information.
譯文[1]
這個NSMutableParagraphStyle類為它的父類NSParagraphStyle添加方法抡诞,用于在段落樣式屬性中改變子屬性的值穷蛹。更多了解土陪,可以查看NSMutableParagraphStyle和NSAttributedString的說明。
_textField = [[UITextField alloc]initWithFrame:CGRectMake(24*HorizontalScale, 20*HorizontalScale+64, ScreenWidth - 48*HorizontalScale, 80*HorizontalScale)];
_textField.layer.borderColor = [UIColor lightGrayColor].CGColor;
_textField.layer.borderWidth = 1;
_textField.layer.cornerRadius = 4;
//設(shè)置光標(biāo)起始位置
UIView *leftView = [[UIView alloc]initWithFrame:CGRectMake(0, 0,10,_textField.height )];
_textField.leftView = leftView;
_textField.leftViewMode = UITextFieldViewModeAlways;
CGRect frame = [@"輸入樓盤名" boundingRectWithSize:CGSizeMake(MAXFLOAT, _textField.height) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:14]} context:nil];
//設(shè)置textFiled的段落格式
NSMutableParagraphStyle *style = [_textField.defaultTextAttributes[NSParagraphStyleAttributeName] mutableCopy];
//設(shè)置垂直對齊
style.minimumLineHeight = _textField.font.lineHeight - (_textField.font.lineHeight - [UIFont systemFontOfSize:14.0].lineHeight) / 2.0;
//水平對齊
style.firstLineHeadIndent = (_textField.width - frame.size.width )*0.5 -DYNAMIC(20);
//設(shè)置placeholder的樣式
_textField.attributedPlaceholder = [[NSAttributedString alloc]initWithString:@"輸入樓盤名" attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:14],NSParagraphStyleAttributeName:style,NSForegroundColorAttributeName:[UIColor blueColor]
}];
[self.view addSubview:_textField];