【问题标题】:Get the index of a parameter in a NSLocalizedString获取 NSLocalizedString 中参数的索引
【发布时间】:2014-03-10 19:40:01
【问题描述】:

这是我的 NSLocalizedString:

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

稍后,我会像这样使用这个字符串:

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

我正在尝试找出变量的索引,以便使用NSMutableAttributedString 以粗体突出显示它们。

我本来打算使用rangeOfString,但后来我意识到如果两个值相同的话可能会出现问题...... 有没有办法以任何其他智能方式获取这些变量在字符串中的位置?

【问题讨论】:

  • 这个具体的字符串还是一般的?
  • 在替换控制字符串之前,将项目加粗。
  • @Wain 通常,由于字符串已本地化,因此索引可以更改
  • @MidhunMP mmmmmmmhhh,你到底是什么意思?
  • 我想重要的是,你总是将字符串中的所有数字加粗吗?或任意内容。如果是任意的,变量值是否会存在于字符串中的其他位置?

标签: ios objective-c nsstring nslocalizedstring nsrange


【解决方案1】:

如果您只想将字符串中的每个数字加粗,您可以查看使用enumerateSubstringsInRange:options:usingBlock: 枚举字符串中的每个单词并检查它是否为数字(longLongValue)。当您找到数值时,枚举块会为您提供范围,以便您知道要在属性字符串中修改什么。

【讨论】:

  • 这听起来是个好主意,只是对我的技能有点矫枉过正:(我现在正在查看@KenThomases 的答案:stackoverflow.com/questions/11171051/…,因为它正在实施我认为你建议我的东西
  • 我仍在努力 :) 通过使用您建议的枚举器块成功地将 int 替换为占位符字符串,但是如何用 NSMutableAttributedString 替换 int?问题是 replaceCharactersInRange:withString 方法接受 NSString,而不是 NSMutableAttributedString
  • 好的,需要拆分。从完整字符串创建可变属性字符串。然后枚举完整字符串并使用范围设置属性字符串 (addAttributes:range:) 中的属性。您不会将属性字符串添加到非属性字符串。
  • 是的,你完全正确,我同时做到了:)
  • 非常感谢您的支持!如果您有兴趣看一下,我想为您提供应用程序的兑换代码:)
【解决方案2】:

使用位置说明符 (... %1$i is the first number, %2$i is the second...)。无论如何,您都需要使用本地化字符串执行此操作,因为替换顺序可能会改变。

另一种选择是用一些标记模式(如$>$...$<$)包围要加粗的值(当然,即使不执行粗体,也要对字符串进行后处理以删除标记)。

【讨论】:

  • thx,但这将问题转移到如何访问位置说明符的位置......
  • @Sr.Richie - 但是您可以合理地假设模式%n$ 表示一个说明符,并且您可以从n 确定参数编号,即使顺序发生更改。
【解决方案3】:

好问题!

我认为,不知何故,您最终会编写自己的 stringWithFormat 版本,换句话说,解析您的本地化字符串。

您可以向后搜索字符串中的所有%i 匹配项并应用粗体属性。然后使用replaceCharactersInRange 将值手动替换为每个数字的字符串表示形式。向后搜索可能很重要,具体取决于您的操作方式,因为替换字符串会移动下一个值。

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

新字符继承第一个替换的属性 aRange 中的字符。如果 aRange 的长度为 0,则新的 字符继承 aRange 前面字符的属性,如果 它有任何,否则是 aRange 后面的字符。

【讨论】:

    【解决方案4】:

    尝试下面的代码来获取特定文本/数字在字符串中的位置.. 我不知道它是否适合你..但根据你的要求使用。

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

    希望它可以帮助你..得到一个想法。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-10-15
      • 1970-01-01
      • 2015-11-26
      • 1970-01-01
      • 1970-01-01
      • 2013-10-31
      相关资源
      最近更新 更多