ER: -[NSAttributedString initWithFormat:]

Originator:butler.grant.j
Number:rdar://36827972 Date Originated:24-Jan-2018 12:46 PM
Status:Duplicate of 12703541 (Open) Resolved:
Product:iOS + SDK (Foundation) Product Version:
Classification:Enhancement Request Reproducible:
 
This is a dupe of https://openradar.appspot.com/radar?id=5004456819163136. See the original copied below.

This definitely gets a +1 from me. In the past, I've gotten similar functionality by implementing a basic HTML parser that supports a limited subset of the HTML spec (mostly <b>/<strong>, <i>/<em>, <u>, <sup>, <sub>) and then included HTML in my localized strings, done the substitution, and then passed that string to the HTML parser, which produces an attributed string I could use in UI. It would be really great if Foundation supported this, so I wouldn't have to do those kinds of shenanigans.

=========================

Let’s say you have the string:

*John Doe* and *1 other* liked this

The stuff inside the asterisks should be bolded when shown in a label.

Ideally, when you’re putting this sort of thing in a strings file, you’d have something like:

“NAME_AND_OTHERS_LIKED” = “%@ and %@ liked this”;

Unfortunately, this sort of string doesn’t play nicely with formatting. I can substitute in the values  (“John Doe” and “1 other”), but there’s no way to know which sub ranges to bold on the attributed string without re-finding those strings, which isn’t totally reliable (because the substituted-in string may appear multiple times, and I can’t reliably infer that it should always be the first one, for example, because the rules of a particular language may enforce a different order).

What would be nice is if I could do (pseudocode):

NSAttributedString *name = @“John Doe” + bold style;
NSAttributedString *others = @“1 other” + bold style;
NSString *format = NSLocalizedString(“NAME_AND_OTHERS_LIKED”);
NSAttributedString *final = [[NSAttributedString alloc] initWithFormat:format, name, others];

I’d also like to be able to use another attributed string as the format, so I can have some pre-applied styles and then substitute in some otherly-styled text.

Comments


Please note: Reports posted here will not necessarily be seen by Apple. All problems should be submitted at bugreport.apple.com before they are posted here. Please only post information for Radars that you have filed yourself, and please do not include Apple confidential information in your posts. Thank you!