有時(shí)候因?yàn)槔习宓囊蠡蛘遀I的設(shè)計(jì)想法毫胜,我們會(huì)遇到需要把字體弄成各種各樣的類型涕侈,這樣就需要富文本文字來解決胁孙,所以在此把一些大神已經(jīng)總結(jié)好的各種可能遇到的字體復(fù)制了一下陪拘,方便以后使用?是臁A迓!
1襟雷、初始化
NSMutableAttributedString *attrString = [[NSMutableAttributedString alloc] initWithString:string];
2杨帽、設(shè)置字體類型方法
//方法
- (void)addAttribute:(NSString *)name value:(id)value range:(NSRange)range;
//demo
[attrString addAttribute:NSFontAttributeName value:baseFont range:NSMakeRange(0, length)];
//name : 修改的富文本類型的名字,例如(NSFontAttributeName:字號(hào)嗤军、字體名注盈;NSForegroundColorAttributeName:字體顏色)
3、各種字體舉例
//系統(tǒng)默認(rèn)字體類型
UIFont *baseFont = [UIFont systemFontOfSize:fontSize];
[attrString addAttribute:NSFontAttributeName value:baseFont range:NSMakeRange(0, length)];
//粗體
UIFont *boldFont = [UIFont boldSystemFontOfSize:fontSize];
[attrString addAttribute:NSFontAttributeName value:boldFont range:[string rangeOfString:@"Text"]];
//設(shè)置傾斜叙赚,需要導(dǎo)入coreText
UIFont *italicFont = GetVariationOfFontWithTrait(baseFont,kCTFontTraitItalic);
[attrString addAttribute:NSFontAttributeName value:italicFont
range:[string rangeOfString:@"Label"]];
// 設(shè)置顏色
[attrString addAttribute:NSForegroundColorAttributeName value:[UIColor blueColor] range:[string rangeOfString:@"ent"]];
//設(shè)置背景色
[attrString addAttribute:NSBackgroundColorAttributeName value:[UIColor blueColor] range:[string rangeOfString:@"ent"]];
//字體名稱有以下:
[attrString addAttribute:NSFontAttributeName value:[UIFont fontWithName:@"Verdana-BoldItalic" size:18] range:[string rangeOfString:@"Label"]];
//設(shè)置段落格式
NSMutableParagraphStyle *style = [[NSParagraphStyle defaultParagraphStyle] mutableCopy];
style.lineSpacing = 10;//增加行高
style.headIndent = 10;//頭部縮進(jìn)老客,相當(dāng)于左padding
style.tailIndent = -10;//相當(dāng)于右padding
style.lineHeightMultiple = 1.5;//行間距是多少倍
style.alignment = NSTextAlignmentLeft;//對(duì)齊方式
style.firstLineHeadIndent = 20;//首行頭縮進(jìn)
style.paragraphSpacing = 10;//段落后面的間距
style.paragraphSpacingBefore = 20;//段落之前的間距
[attrString addAttribute:NSParagraphStyleAttributeName value:style range:NSMakeRange(0, length)];
//字符間距 2pt
[attrString addAttribute:NSKernAttributeName value:@2 range:NSMakeRange(0, length)];
//設(shè)置文字描邊顏色,需要和NSStrokeWidthAttributeName設(shè)置描邊寬度震叮,這樣就能使文字空心
[attrString addAttribute:NSStrokeColorAttributeName value:[UIColor blueColor] range:[string rangeOfString:@"is"]];
//空心字胧砰,文字邊框描述
[attrString addAttribute:NSStrokeWidthAttributeName value:@2 range:[string rangeOfString:@"is"]];
//下劃線
[attrString addAttribute:NSUnderlineStyleAttributeName value:@(NSUnderlineStyleSingle) range:[string rangeOfString:@"text"]];
//厚的下劃線
[attrString addAttribute:NSUnderlineStyleAttributeName value:@(NSUnderlineStyleThick) range:[string rangeOfString:@"label"]];
//刪除線
[attrString addAttribute:NSStrikethroughStyleAttributeName value:@(NSUnderlinePatternSolid | NSUnderlineStyleSingle) range:[string rangeOfString:@"things"]];
//刪除線藍(lán)色
[attrString addAttribute:NSStrikethroughColorAttributeName value:[UIColor blueColor] range:[string rangeOfString:@"things"]];