iOS-textField 的 placeholder 和 textField 的 fontSize 不同大小的實(shí)現(xiàn)小坑

需求背景: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;
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

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

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