9

My old code uses NSLocalizedString to display the following where outputText was an NSMutableString that contained many such lines in a single output session:

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

I'm trying to change the color of the various ipAddress strings, but can't find a similar method when using NSMutableAttributedString.

The biggest problem I'm facing is that since this entire string will be localized, I can't reliably set the NSRange without breaking up each part of the formatted output.

Do I need to dissect each part of this string, convert it to NSAttributedString and append each piece to the outputText??

Mickey
  • 95
  • 1
  • 6
  • 1
    I imagine you could regex it out very easily, seeing as numbers don't really change from their arabic numeral forms in commonly localized formats. – CodaFi Nov 30 '12 at 13:57
  • Ugh - you're right. I'd blocked out regular expressions after I was done with BGP. Thanks for the reality check! – Mickey Nov 30 '12 at 14:28
  • You could use `rangeOfString:` to get the range of the inserted strings after inserting them. https://developer.apple.com/library/mac/documentation/Cocoa/Reference/Foundation/Classes/NSString_Class/Reference/NSString.html#//apple_ref/occ/instm/NSString/rangeOfString: – Greg Dec 30 '12 at 00:11

1 Answers1

3

The answer is: yes.

Yes, you need to localize sections with different attributes separately.

Cocoanetics
  • 8,171
  • 2
  • 30
  • 57
  • 1
    But that does take into account the use case where parameters order is reversed in the localized string, for instance `"(%1$@, %2$@)"`. That's why an `-(id)initWithFormat:...` method would be great in the `NSAttributedString` class, don't you think ? – dulaccc Sep 11 '13 at 02:04