2012-11-30 79 views
9

我的舊代碼使用NSLocalizedString顯示以下內容,其中的outputText是包含在一個單一的輸出會很多這樣的線的的NSMutableString:使用nsattributedstring和nslocalizedstring

[outputText appendFormat: NSLocalizedString(@"\n\n%@ and %@ are identical. No comparison required.", @"\n\n%@ and %@ are identical. No comparison required."), self.ipAddress, secAddress.ipAddress]; 

我試圖改變各種顏色ipAddress字符串,但使用NSMutableAttributedString時找不到類似的方法。

我面臨的最大問題是,由於整個字符串將被本地化,因此我無法可靠地設置NSRange而不破壞格式化輸出的每個部分。

我是否需要剖析此字符串的每個部分,將其轉換爲NSAttributedString並將每個部分追加到outputText?

+1

我想你可以非常容易地將它重新排列,看起來數字並沒有真正從它們的阿拉伯數字形式改變爲常用的本地化格式。 – CodaFi

+0

呃 - 你說得對。在完成BGP之後,我阻止了正則表達式。 感謝您的現實檢查! – Mickey

+0

您可以使用'rangeOfString:'在插入字符串後獲取插入字符串的範圍。 https://developer.apple.com/library/mac/documentation/Cocoa/Reference/Foundation/Classes/NSString_Class/Reference/NSString.html#//apple_ref/occ/instm/NSString/rangeOfString: – Greg

回答

3

答案是肯定的。

是的,您需要分別定位具有不同屬性的部分。

+1

但是,說明在本地化字符串中反轉參數順序的用例,例如'「(%1 $ @,%2 $ @)」'。這就是爲什麼' - (id)initWithFormat:...'方法在'NSAttributedString'類中很棒,你不覺得嗎? – dulaccc