UITextField-設(shè)置placeholder的正確姿勢

1.需求說明

項目中經(jīng)常會遇到各種各樣的需求,比如輸入框透明,邊框以及內(nèi)部的placeholder為白色,再或者placeholder要求居中。

樣例截圖

廣大的iOS開發(fā)人員可能會想到各種方法,本人只舉一些設(shè)置placeholder的顏色以及字體大小的常見用法。好,各位同行坐穩(wě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]
該屬性默認是空的。如果設(shè)置了的話,如果設(shè)置了該屬性,占位符( placeholder)會按照70%灰度顏色繪制,并按照富文本(attributed string)的樣式信息保持樣式(不包括文本顏色)。盡管沒有任何的樣式設(shè)置,但是給這個屬性分配一個新的值會使用一個新的字符串數(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添加方法,用于在段落樣式屬性中改變子屬性的值。更多了解,可以查看NSMutableParagraphStyleNSAttributedString的說明。

  _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è)置光標起始位置
    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];

未完待續(xù)....


  1. 文中譯文為筆者翻譯,可能有出入,請見諒. ? ?

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容