IOS開發得到所有的字型樣式
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(@%@,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裡面有。