您的位置:首页 > 其它

中英文多字体的富文本

2015-09-12 13:58 274 查看
/**
*  传入字符串 中文字体 英文字体 返回富文本
*/
- (NSMutableAttributedString *)attrStrWithStr:(NSString *)str enFont:(UIFont *)enFont zhFont:(UIFont *)zhFont{
NSMutableAttributedString *attrStr = [[NSMutableAttributedString alloc] init];
for(int i = 0; i< str.length; i++){
NSRange range = NSMakeRange(i, 1);
NSString *newStr = [str substringWithRange:range];
NSMutableAttributedString *tempAttrStr = [[NSMutableAttributedString alloc] initWithString:newStr];
int a = [str characterAtIndex:i];
if( a > 0x4e00 && a < 0x9fff){
[tempAttrStr addAttribute:NSFontAttributeName value:zhFont range:NSMakeRange(0, 1)];
} else {
[tempAttrStr addAttribute:NSFontAttributeName value:enFont range:NSMakeRange(0, 1)];
}
[attrStr appendAttributedString:tempAttrStr];
}
return attrStr;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: