This is my NSLocalizedString:

 "YOUR_INFO" = "%i is the first number,  %i the second and %i the third";

Later on, i'm using this string like this:

 NSString *detailString = [NSString stringWithFormat:NSLocalizedString(@"YOUR_INFO", nil), firstVal, secondVal, thirdVal];

I'm trying to find out the indexes of the variables in order to use NSMutableAttributedString to highlight them in bold.

I was going to use rangeOfString, but then I realised there could be an issue if some if two values are the same.... Is there any way to get the position in the string of those variables in any other smart way?

有帮助吗?

解决方案

If you just want to bold each number in a string, you could look at using enumerateSubstringsInRange:options:usingBlock: to enumerate each word in the string and check if it's numeric (longLongValue). When you find numeric values the enumeration block provides you with the range so you know what to modify in your attributed string.

其他提示

Use positional specifiers (... %1$i is the first number, %2$i is the second...). You need to do this anyway, with a localized string, since the order of replacement may change.

The other option is to surround the to-be-bolded values with some sentinel pattern like $>$...$<$ (being sure, of course, to post-process the string to remove the sentinels, even if no bolding is performed).

Nice question!

I think, somehow, you would end-up writing your own stringWithFormat version, in other words, parsing your localized string.

You could search backwards for all %i matches in your string and apply the bold attribute. And then use replaceCharactersInRange to replace the value manually with the string representation of each number. Searching backwards could be important, depending on how you do it, because replacing a string would shift the next values.

  • (void)replaceCharactersInRange:(NSRange)aRange withString:(NSString *)aString

The new characters inherit the attributes of the first replaced character from aRange. Where the length of aRange is 0, the new characters inherit the attributes of the character preceding aRange if it has any, otherwise of the character following aRange.

Try below code to get location of particular text/number in a string.. I dont know it will work for you or not..But use as per your requirement.

    NSString *notiStr;
    int location;
    int length;

    if ([detailString rangeOfString:@"1"].location == NSNotFound)
    {

    }
    else
    {
        location = [notificationString rangeOfString:@"1"].location;
        notiStr = @"1";
        length = 1;
    }
    UIFont *boldFont = [UIFont fontWithName:@"Helvitica-Bold" size:16];
    UIFont *regularFont = [UIFont fontWithName:@"Helvitica-Regular" size:16];
    NSDictionary *attrs = [NSDictionary dictionaryWithObjectsAndKeys:
                                   regularFont, NSFontAttributeName,nil];


    NSDictionary *subAttrs = [NSDictionary dictionaryWithObjectsAndKeys:
                                      boldFont, NSFontAttributeName, nil];

    const NSRange range = NSMakeRange(location,length);

    NSMutableAttributedString *attributedText;

    attributedText = [[NSMutableAttributedString alloc] initWithString:[NSString stringWithFormat:@"%@",detailedString] attributes:attrs];

    [attributedText setAttributes:subAttrs range:range];

Hope it helps you..to get an idea.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top