iOS實用技能之同乙個String不同顏色的字

2021-07-15 15:31:41 字數 850 閱讀 1967

最近在開發中碰到個問題,需要乙個字串的數字為紅色,其餘為黑色。思考了半天,沒思路,問了下同事,才了解到有attributestring這個東西,看來我還是個新手啊,在這裡做個記錄,方便查閱吧。

要達到效果,比如 –> 這樣數字8 是紅色的需求就可以用nsmutableattributedstring來實現。

廢話不多說,直接上**

//這裡只是示例,你要求出需要設定顏色的字元的數量,這裡為colorcount

nsinteger colorstrcount = 1;

nsstring *tempstring = [nsstring stringwithformat:@"這裡有:5人"];

//用tempstring來初始化乙個nsmutableattributedstring

nsmutableattributedstring *colorstring = [[nsmutableattributedstring alloc]initwithstring:tempstring];

//把colorstring的4~colorstrcount這個範圍內的字元設定為紅色

[colorstring addattribute:nsforegroundcolorattributename value:[uicolor redcolor] range:nsmakerange(4, colorstrcount)];

//注意,這裡要用label的attributedtext來接收,不能用text

self

.activitytimeleftlabel

.attributedtext = colorstring;

這樣寫的效果就是 –> 這裡有:5人

iOS 同乙個workspace下建立多個專案程式設計

在ios開發中,相關聯的多個專案可能會放在同乙個workspace下進行開發,那習慣了乙個專案在乙個工作空間下的同學該怎麼快速開擼呢?只需要三步而已!第一步,先用xcode在目標目錄下建立乙個workspace檔案。見圖說話。第二步,用xcode開啟workspace檔案,然後在該workspace...

iOS 同乙個Label的不同樣式顯示

一行文字 要顯示成 一行文字 不需要用不同的兩個label進行拼接 今兒個就放個方法進來 uilabel o pricelabel uilabel alloc init o pricelabel.text 一行文字 o pricelabel.textalignment nstextalignment...

同乙個IP繫結2個網域名稱

technorati 標籤 ip,網域名稱 同乙個ip繫結2個網域名稱 假設已經在isp那裡申請了2個網域名稱www.test1.com www.test2.com,而且都是繫結到伺服器的公網ip是192.168.1.100.test1是apache在本機的乙個應用,訪問url為http 192.1...