我的舊代碼使用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?
我想你可以非常容易地將它重新排列,看起來數字並沒有真正從它們的阿拉伯數字形式改變爲常用的本地化格式。 – CodaFi
呃 - 你說得對。在完成BGP之後,我阻止了正則表達式。 感謝您的現實檢查! – Mickey
您可以使用'rangeOfString:'在插入字符串後獲取插入字符串的範圍。 https://developer.apple.com/library/mac/documentation/Cocoa/Reference/Foundation/Classes/NSString_Class/Reference/NSString.html#//apple_ref/occ/instm/NSString/rangeOfString: – Greg