我想显示英文日期风格。我制作了以下代码,以便让角色重新设计":
- (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;
}
问题是,我发现只有第一个"命中"从开始到结束。如何安排搜索整个字符串?特别是关于""因为在字符串中有时是单词的5或6倍""然后停止添加属性。
答案 0 :(得分:1)
如果找到“th”,请使用-rangeOfString:options:range:
继续循环(将范围参数设置为刚刚找到它的位置)。
答案 1 :(得分:1)
我不确定你想要达到的目标。但这是我通常用于为长字符串添加属性的内容。
- (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"];
示例输出将是:
希望这会有所帮助..干杯! :)
答案 2 :(得分:0)
感谢0yeoj我找到了解决方案:
在字符串中删除开始标记,并用唯一字符替换结束标记,按此顺序不会有任何单词(öäüß)。
使用0yeoj中的代码查找字符串“thöäüß”,stöäüß等,并根据需要添加属性。
我的错误发生了 - 我试图删除子循环中的字符“öäüß”,这会导致崩溃。
在此处的另一篇文章中找到解决方案并将其放在return-expression之前:
while([attriString.string containsString:@“öäüß”]){ NSRange rangeOfTag = [attriString.string rangeOfString:@“öäüß”]; [attriString replaceCharactersInRange:rangeOfTag withString:@“”]; }
现在工作完美。感谢您的快速帮助。