ios的提供了很多的字体样式。有时候我们在开发应用的时候可能用到不同的字体,通过此Demo我们可以获取到所有的字体样式供我们选择。
首先获取字体字体族科名字,再通过族科的名字获取到字体的名字。
_fontArray = [[NSMutableArray alloc] initWithCapacity:242];
for (NSString * familyName in [UIFont familyNames]) {
NSLog(@"Font FamilyName = %@",familyName); //*输出字体族科名字
for (NSString * fontName in [UIFont fontNamesForFamilyName:familyName]) {
NSLog(@"\t%@",fontName);
[_fontArray addObject:fontName];
}
然后再使用如下代码就可以修改字符串的字体。
NSString * testStr = @"It has 15 words";
// NSString * testStr = @"我随手一打就是漂亮的十五个字了";
NSMutableAttributedString *str = [[NSMutableAttributedString alloc] initWithString:testStr];
[str addAttribute:NSForegroundColorAttributeName value:[UIColor blueColor] range:NSMakeRange(0,3)];
[str addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:NSMakeRange(3,4)];
[str addAttribute:NSForegroundColorAttributeName value:[UIColor greenColor] range:NSMakeRange(7,4)];
[str addAttribute:NSFontAttributeName value:[UIFont fontWithName:_fontArray[indexPath.row] size:30] range:NSMakeRange(0, 15)];
显示中文和英文的效果不是很一致,英文的效果明显一些,如下是部分效果截图。具体在Demo里面有。
demo下载地址:http://download.csdn.net/detail/u011496891/8663909
转载请注明出处:http://blog.csdn.net/zhouzhoujianquan/article/details/45503357