需求背景:textField 的 placeholder 要比 textField 輸入狀態(tài)下的文字要小
實(shí)現(xiàn)
要自定義 textField 的 placeholder,我們使用 attributedPlaceholder 來設(shè)置:
NSString *placeholder = @"請(qǐng)輸入文字";
NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:placeholder];
[attributedString addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:14] range:NSMakeRange(0, placeholder.length)];
textFiled.attributedPlaceholder = attributedString;
textFiled.font = [UIFont systemFontOfSize:28];
結(jié)果發(fā)現(xiàn) attributedPlaceholder 字體大小并不是14,而是28,后面設(shè)置的 font 的大小覆蓋了 attributedPlaceholder 的字體大小,由于我的編碼習(xí)慣是先設(shè)置 placeholder,后設(shè)置font,所以出現(xiàn)了這個(gè)小坑。
解決
先設(shè)置font,再設(shè)置 placeholder
textFiled.font = [UIFont systemFontOfSize:28];
textFiled.attributedPlaceholder = attributedString;