前言
相信大家都知道,uialertcontroller的標(biāo)題和內(nèi)容都是黑色的(對uialertcontroller不了解的朋友可以參考這篇文章),但是在很多場景下都需要修改他們的顏色,比如在輸入錯誤時把提示信息變?yōu)榧t色,或者自定義標(biāo)題的顏色,可是在公開的api接口中好像并沒有對應(yīng)的方法,那么我們應(yīng)該怎么做呢?下面話不多說了,來一起看看詳細(xì)的介紹:
第三方控件
第一種方法當(dāng)然就是使用第三方的alert控件了,現(xiàn)在github上有著眾多的alert控件(如sclalertview等),相信有很多都可以滿足大家的需求,只要使用cocoapods添加添加第三方庫就可以了。
kvc方法
但是也有一些人,不愿意去使用第三方庫,而是想要使用系統(tǒng)的uialertcontroller,這樣當(dāng)然也是可以的。蘋果公司并沒有完全的封死對uialertcontroller的定制,而是修改為了使用kvc的方法進(jìn)行定制。如果要自定義標(biāo)題和內(nèi)容,可以通過nsattributedstring把字體和顏色設(shè)置好,然后在通過kvc的方法進(jìn)行設(shè)置,就可以了。
下面是一個示例代碼和對應(yīng)的截圖:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
- ( void )testalert { uialertcontroller *alert = [uialertcontroller alertcontrollerwithtitle:nil message:nil preferredstyle:uialertcontrollerstylealert]; [alert addaction:[uialertaction actionwithtitle:@ "取消" style:uialertactionstylecancel handler:nil]]; nsdictionary *titleattr = @{ nsfontattributename:[uifont boldsystemfontofsize:20], nsforegroundcolorattributename:[uicolor greencolor] }; nsattributedstring *attributedtitle = [[nsattributedstring alloc] initwithstring:@ "測試有顏色標(biāo)題" attributes:titleattr]; [alert setvalue:attributedtitle forkey:@ "attributedtitle" ]; nsdictionary *messageattr = @{ nsfontattributename:[uifont systemfontofsize:12], nsforegroundcolorattributename:[uicolor redcolor] }; nsattributedstring *attributedmessage = [[nsattributedstring alloc] initwithstring:@ "測試有顏色文本" attributes:messageattr]; [alert setvalue:attributedmessage forkey:@ "attributedmessage" ]; [self presentviewcontroller:alert animated:yes completion:nil]; } |
屏幕截圖
?
關(guān)于自定義標(biāo)題和內(nèi)容就說這么些了,主要還是要看代碼才能明白。
總結(jié)
以上就是這篇文章的全部內(nèi)容了,本文還有許多不足,希望本文的內(nèi)容對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,如果有疑問大家可以留言交流,謝謝大家對服務(wù)器之家的支持。
原文鏈接:http://www.jianshu.com/p/0033d61968aa