2 つのフォントと色の間をスワイプして NSAttributedString を作成する必要があります。
それらを検出できる場合は、名前を既知のマークアップ (例: @aName) で囲んで置き換える必要があります。次に、文字列を解析して NSAttributedString を作成します。
このコードを使用できます (テストされていないため、おそらく調整する必要があります)。
// String to parse
NSString *markup = @"MT <color>@OraTV</color>: SNEAK PEEK: <color>@tomgreenlive</color>...";
// Names font and color
UIFont *boldFont = [UIFont boldSystemFontOfSize:15.0f];
UIColor *boldColor = [UIColor blueColor];
// Other text font and color
UIFont *stdFont = [UIFont systemFontOfSize:15.0f];
UIColor *stdColor = [UIColor blackColor];
// Current font and color
UIFont *currentFont = stdFont;
UIColor *currentColor = stdColor;
// Parse HTML string
NSMutableAttributedString *aString = [[NSMutableAttributedString alloc] initWithString:@""];
NSRegularExpression *regex = [[NSRegularExpression alloc] initWithPattern:@"(.*?)(<[^>]+>|\\Z)"
options:NSRegularExpressionCaseInsensitive|NSRegularExpressionDotMatchesLineSeparators
error:nil];
NSArray *chunks = [regex matchesInString:markup options:0 range:NSMakeRange(0, [markup length])];
for (NSTextCheckingResult* b in chunks)
{
NSArray *parts = [[markup substringWithRange:b.range] componentsSeparatedByString:@"<"];
NSDictionary *attrs = [NSDictionary dictionaryWithObjectsAndKeys:currentFont,NSFontAttributeName,currentColor,NSForegroundColorAttributeName,nil];
[aString appendAttributedString:[[NSAttributedString alloc] initWithString:[parts objectAtIndex:0] attributes:attrs]];
if([parts count] > 1)
{
NSString *tag = (NSString *)[parts objectAtIndex:1];
if([tag hasPrefix:@"color"])
{
currentFont = boldFont;
currentColor = boldColor;
}
else if([tag hasPrefix:@"/color"])
{
currentFont = stdFont;
currentColor = stdColor;
}
}
}
それが役立つことを願っています。
シリル