ER: -[NSAttributedString initWithFormat:]

Originator:davedelong
Number:rdar://36827007 Date Originated:1/24/2019
Status:duplicate Resolved:
Product:Foundation Product Version:
Classification:Enhancement Request Reproducible:
 
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

Duplicate of #12703541

By davedelong at Jan. 31, 2018, 10:48 p.m. (reply...)

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!