字符串没有粗体等属性。字符串仅包含字符,包括换行符。如果你想用属性丰富你的字符串,看看 NSAttributedString。
更新:对于我们这些看不到的人来说,为什么属性字符串是解决方案,一段简单的代码:
- (NSAttributedString*)attributedStringByReplaceHtmlTag:(NSString*)tagName withAttributes:(NSDictionary*)attributes
{
NSString *openTag = [NSString stringWithFormat:@"<%@>", tagName];
NSString *closeTag = [NSString stringWithFormat:@"</%@>", tagName];
NSMutableAttributedString *resultingText = [self mutableCopy];
while ( YES ) {
NSString *plainString = [resultingText string];
NSRange openTagRange = [plainString rangeOfString:openTag];
if (openTagRange.length==0) {
break;
}
NSRange searchRange;
searchRange.location = openTagRange.location+openTagRange.length;
searchRange.length = [plainString length]-searchRange.location;
NSRange closeTagRange = [plainString rangeOfString:closeTag options:0 range:searchRange];
NSRange effectedRange;
effectedRange.location = openTagRange.location+openTagRange.length;
effectedRange.length = closeTagRange.location - effectedRange.location;
[resultingText setAttributes:attributes range:effectedRange];
[resultingText deleteCharactersInRange:closeTagRange];
[resultingText deleteCharactersInRange:openTagRange];
}
return resultingText;
}
但我没有很好地测试它,因为我必须在编程时准备意大利烩饭。;-)