2015-05-13 54 views
-1

我創建了一個HTML屬性串像這樣:iOS版 - NSAttributedString標記化到localization.string元素

NSString * htmlString = @"<html><body> Some html string <sup>MD</sup></body></html>"; 
NSAttributedString * attrStr = [[NSAttributedString alloc] initWithData:[htmlString dataUsingEncoding:NSUnicodeStringEncoding] options:@{ NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType } documentAttributes:nil error:nil]; 

現在我有了這樣的令牌的本地化字符串:

"greeting_label" ="reeveBeets%s \n are awesome"; 

如何我是否使用我創建的屬性html文本替換%s,並將其放在我作爲名爲_label的iboutlet的標籤中?

+0

另外,IBOutlets不高德,正因爲如此,一個IBOutlet應該永遠不要預先加上_。您的標籤應該被命名爲標籤,而不是_label。 – TheCodingArt

+0

xcode爲我合成它。所以在我的.h文件中調用標籤,但是當我在代碼中使用它時,我使用_label。你怎麼看? – j2emanue

+0

是的,沒關係使用self.property,它不涉及實例變量,getter/setter副作用,init或dealloc。但沒關係。 – TheCodingArt

回答

0

本地化格式的字符串工作就像普通格式的字符串,你可以在下面的例子中看到:

[NSString stringWithFormat:NSLocalizedString(@"nearby_station", nil), self.endStation.name] 

"nearby_station" = "Nearby %@ Station"; 
+0

那真的不是我想要的。我想在你的令牌中插入一個NSAttributedString。即%@。所以我能做到這一點是問題:[NSString stringWithFormat:NSLocalizedString(@「nearby_station」,零),someAttributedString] – j2emanue

+0

嗯,我可以給的最好的是告訴你,你將不得不創建一個屬性的字符串手動將一個屬性字符串插入另一個。您可以通過追加和歸因字符串或將屬性字符串的屬性從一個屬性字符串添加到另一個屬性字符串來執行此操作。生成屬性字符串後,即可訪問所有屬性。我會重複一遍,上面的和stringWithFormat是完全一樣的,所以你可以用這個來做任何事情(你不能只是給它添加一個屬性字符串)。 – TheCodingArt

+0

你也應該改變你的問題,以更好地反映你想要的東西。 – TheCodingArt