xcode iOS NSMutableAttributedString 多次添加属性

xcode iOS NSMutableAttributedString add attribute more than one time

我想显示英文日期样式。我编写了以下代码来获取字符 "redesign":

    - (NSMutableAttributedString *)attributedInfoString:(NSString *)string;
    {
        NSMutableAttributedString *attString = [[NSMutableAttributedString alloc] initWithString:string];
        UIFont *smallFont = [UIFont boldSystemFontOfSize:17.0f];
        UIFont *boldFont = [UIFont fontWithName:@"HelveticaNeue-Bold" size:24.0f];

        [attString beginEditing];

        [attString addAttribute:NSFontAttributeName value:(smallFont) range:[string rangeOfString:@"th"]];
        [attString addAttribute:NSFontAttributeName value:(smallFont) range:[string rangeOfString:@"th" options:NSBackwardsSearch]];
        [attString addAttribute:(NSString*)kCTSuperscriptAttributeName value:@"1" range:[string rangeOfString:@"th"]];
        [attString addAttribute:(NSString*)kCTSuperscriptAttributeName value:@"1" range:[string rangeOfString:@"th" options:NSBackwardsSearch]];

        [attString addAttribute:NSFontAttributeName value:(boldFont) range:[string rangeOfString:@"the"]];
        [attString addAttribute:NSFontAttributeName value:(boldFont) range:[string rangeOfString:@"the" options:NSBackwardsSearch]];
        [attString addAttribute:(NSString*)kCTSuperscriptAttributeName value:@"0" range:[string rangeOfString:@"the"]];
        [attString addAttribute:(NSString*)kCTSuperscriptAttributeName value:@"0" range:[string rangeOfString:@"the" options:NSBackwardsSearch]];

        [attString addAttribute:NSFontAttributeName value:(smallFont) range:[string rangeOfString:@"st"]];
        [attString addAttribute:NSFontAttributeName value:(smallFont) range:[string rangeOfString:@"st" options:NSBackwardsSearch]];
        [attString addAttribute:(NSString*)kCTSuperscriptAttributeName value:@"1" range:[string rangeOfString:@"st"]];
        [attString addAttribute:(NSString*)kCTSuperscriptAttributeName value:@"1" range:[string rangeOfString:@"st" options:NSBackwardsSearch]];

        [attString addAttribute:NSFontAttributeName value:(boldFont) range:[string rangeOfString:@"August"]];
        [attString addAttribute:(NSString*)kCTSuperscriptAttributeName value:@"0" range:[string rangeOfString:@"August"]];

...

        [attString endEditing];
        return attString;
    }

问题,我只从头或尾找到第一个"hit"。我怎样才能安排搜索整个字符串?特别是关于 "th" 因为在字符串中有时是单词 "the" 的 5 或 6 倍然后停止添加属性。

如果您找到 "th",请使用 -rangeOfString:options:range: 继续循环(您将范围参数设置为刚好超过您找到它的位置)。

我不确定你想要达到什么目的。但这是我通常用来为长字符串添加属性的方法。

- (NSMutableAttributedString *)attributedInfoString:(NSString *)string;
{
    __block NSMutableAttributedString *attriString = [[NSMutableAttributedString alloc] initWithString:string];

    [attriString addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:15.0f] range:NSMakeRange(0, string.length)];

    UIFont *bold = [UIFont boldSystemFontOfSize:17.0f];

    __block BOOL mustStop = NO;

    [string enumerateSubstringsInRange:NSMakeRange(0, [string length])
                               options:NSStringEnumerationByWords
                            usingBlock:^(NSString *substring, NSRange substringRange, NSRange enclosingRange, BOOL *stop)
     {
         // terminates adding of attribute to string if YES
         // as you can see `og` from the word dog is ignored
         if (!mustStop)
         {
             // since we are enumeratingByWord this is to check for specific string(word) in the `string`
             // in this example `the` is the trigger to terminate adding attributes to mutableString
             //
             if (![substring isEqual:@"the"])
             {
                 // add attribute if the substring(word) has `ox` or `og`
                 //
                 if ([substring rangeOfString:@"ox"].location != NSNotFound || [substring rangeOfString:@"og"].location != NSNotFound)
                 {
                     [attriString addAttribute:NSFontAttributeName value:bold range:substringRange];
                 }

                 else
                 {
                     // this is to add attribute to some specified `substring`
                     //
                     NSString *subsubString = @"uic";

                     NSRange subRange = [substring rangeOfString:subsubString];

                     if (subRange.location != NSNotFound)
                     {
                         substringRange.location += subRange.location;

                         substringRange.length = subsubString.length;

                         [attriString addAttribute:NSForegroundColorAttributeName value:[UIColor orangeColor] range:substringRange];

                         [attriString addAttribute:NSFontAttributeName value:bold range:substringRange];
                     }
                 }
             } else mustStop = YES;
         }
     }
     ];

    return attriString;
}

如果你这样使用:

title.attributedText = [self attributedInfoString:@"The quick brown fox jumped over the lazy dog"];

示例输出为:

希望这会有所帮助..干杯! :)

感谢 0yeoj,我找到了解决方案:

  1. 在字符串中删除开始标签并用唯一字符替换结束标签,没有单词会按此顺序 (öäüß)。

  2. 使用 0yeoj 中的代码查找字符串 "thöäüß"、stöäüß 等并根据需要添加属性。

  3. 这里发生了我的错误 - 我试图删除子循环中的字符“öäüß”,这总是导致崩溃。

  4. 在另一个 post 中找到解决方案并将其放在 return 表达式之前:

    while ([attriString.string containsString:@"öäüß"]) { NSRange rangeOfTag = [attriString.string rangeOfString:@"öäüß"]; [attriString replaceCharactersInRange:rangeOfTag withString:@""]; }

现在完美运行。感谢您的快速帮助。