Search code examples
iosnsattributedstringlocalizable.strings

iOS - NSAttributedString tokenized to localization.string element


I have created a html attributed string like this:

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];

Now i have a localized string that has a token like this:

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

how do i replace the %s with my attributed html text i created and put it in a label i have as a iboutlet called _label ?


Solution

  • Localized formatted strings work just like regular formatted strings as you can see in the example below:

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

    Where

    "nearby_station" = "Nearby %@ Station";