xcode iOS NSMutableAttributeString多次添加属性

xcode iOS NSMutableAttributeString多次添加属性,ios,xcode,nsmutableattributedstring,addattribute,Ios,Xcode,Nsmutableattributedstring,Addattribute,我想展示英文日期样式。我编写了以下代码来让角色“重新设计”: 问题是,我从开头或结尾只找到第一个“命中”。如何安排搜索整个字符串?特别是关于“th”,因为字符串中有时是单词“the”的5或6倍,然后停止添加属性。如果找到“th”,请使用-rangeOfString:options:range:(将range参数设置为刚好超过找到它的位置)继续循环。我不确定要实现什么。但这是我通常用于为长字符串添加属性的方法 - (NSMutableAttributedString *)attributedInf

我想展示英文日期样式。我编写了以下代码来让角色“重新设计”:


问题是,我从开头或结尾只找到第一个“命中”。如何安排搜索整个字符串?特别是关于“th”,因为字符串中有时是单词“the”的5或6倍,然后停止添加属性。

如果找到“th”,请使用
-rangeOfString:options:range:
(将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,我找到了解决方案:

  • 在字符串中,删除开始标记,并用唯一字符替换结束标记,这是任何单词都不会以这种顺序出现的(öäüß)

  • 使用0yeoj中的代码查找字符串“thöäüß”、stöäüß等,并根据需要添加属性

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

  • 在此处的另一篇文章中找到了解决方案,并将其放置在返回表达式之前:

    while([attriString.string包含字符串:@“öäüß”]){ NSRange rangeOfTag=[attriString.string rangeOfString:@“öäüß”]; [attriString ReplaceCharactersRange:rangeOfTag with String:@”“; }


  • 现在工作很完美。感谢您的快速帮助。

    谢谢,但解决方案的“口头”方式是众所周知的。对不起,我的问题不准确。我的障碍是从语言到代码的转换。我怎样才能做这个循环?谢谢-但它不是我需要的。我使用的xml文件有几个日期,如6月15日或3月3日。第一步是删除html标记,现在我想更改“th”、“st”、“nd”和“rd”的属性。现在,我试图保留标记以确定所需的范围,而不是在“the”或“nd”ind和“or”end中选择“th”。但是如何删除属性字符串中的标记?[attriString ReplaceCharactersRange:[string rangeOfString:@”“]和字符串:@”“;“会以崩溃而告终。”克里斯蒂安,我明白了。。请包括我使用的xml文件。。。对于这个问题,这有点误导。。。
    title.attributedText = [self attributedInfoString:@"The quick brown fox jumped over the lazy dog"];