很久沒有寫簡書了濒蒋,可能接下來會(huì)整理一下開發(fā)常用小Tips嗤练,看看蘋果文檔(沒錯(cuò)我就是懶!)矿咕,
今天就先說說文本開發(fā)中常用的一些知識(shí)點(diǎn)抢肛。
常用編輯文本屬性的類
NSAttributedString
- 設(shè)置字體大小
NSFontAttributeName : [UIFont systemFontOfSize:16]- 設(shè)置字間距
NSKernAttributeName : @2.0f- 設(shè)置字體顏色
NSForegroundColorAttributeName : [UIColor yellowColor]- 設(shè)置段落樣式(下文詳解)
NSParagraphStyleAttributeName- 設(shè)置背景顏色
NSBackgroundColorAttributeName : [UIColor yellowColor]- 設(shè)置空心(兩個(gè)屬性同時(shí)設(shè)置才有效果)
NSStrokeColorAttributeName : [UIColor yellowColor] //描邊顏色
NSStrokeWidthAttributeName : @3.0f //描邊寬度(默認(rèn)為 0,即不改變碳柱,大于0改變描邊寬度捡絮,小于0同時(shí)改變文字的描邊和填充寬度)- 設(shè)置刪除線和下劃線
NSStrikethroughStyleAttributeName : @(NSUnderlineStyleSingle)
NSUnderlineStyleAttributeName : @(NSUnderlineStyleSingle)- 設(shè)置陰影
NSShadowAttributeName : [[NSShadow alloc]init] (此處是強(qiáng)調(diào)傳入陰影對(duì)象,需要設(shè)置陰影樣式莲镣,且陰影需要配合以下三個(gè)屬性其中之一才能正常使用)- 設(shè)置豎排文本
NSVerticalGlyphFormAttributeName : @(0)- 設(shè)置字體傾斜
NSObliquenessAttributeName : @1- 設(shè)置文本扁平化
NSExpansionAttributeName : @1
使用示例:
///設(shè)置tabBar字體顏色
[[UITabBarItem appearance] setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:[UIColor whiteColor], NSForegroundColorAttributeName, nil] forState:UIControlStateNormal];`
NSMutableParagraphStyle
Important
A paragraph style object should not be mutated after adding it to an attributed string; doing so can cause your program to crash.
蘋果文檔里有一個(gè)重要提示:ParagraphStyle賦值到AttributedString之后就不能變動(dòng)了福稳,不然會(huì)導(dǎo)致崩潰!這是使用過程中需要注意的瑞侮!
使用示例:
NSMutableParagraphStyle *paraStyle = [[NSMutableParagraphStyle alloc] init];
paraStyle.alignment = NSTextAlignmentLeft; //傳說中的對(duì)齊方式 -> 左
paraStyle.firstLineHeadIndent = 0.0; //傳說中的首行頭部縮進(jìn)
paraStyle.headIndent = 0; //傳說中的除了首行的頭部縮進(jìn)
paraStyle.tailIndent = 0; //傳說中的尾部縮進(jìn)
paraStyle.lineBreakMode = NSLineBreakByCharWrapping; //傳說中的斷句模式
paraStyle.maximumLineHeight //傳說中的最大行高的圆,同理minimumLineHeight就是最小行高
paraStyle.lineSpacing = 8; //傳說中的行間距
paraStyle.paragraphSpacing = 0.0; //傳說中的段落間距
paraStyle.paragraphSpacingBefore = 0.0; //傳說中的段落頂部和文本內(nèi)容的間距
paraStyle.hyphenationFactor = 1.0; //傳說中的斷詞功能,取值0~1
paraStyle.baseWritingDirection //傳說中的書寫方向半火,左到右越妈、右到左、雙向
paraStyle.lineHeightMultiple //傳說中的行高乘數(shù)钮糖,改變行高不改變字體大小
//設(shè)置字間距 NSKernAttributeName:@1.5f
NSDictionary *dic = @{NSFontAttributeName : [UIFont systemFontOfSize:16]梅掠,NSParagraphStyleAttributeName:paraStyle, NSKernAttributeName:@1.5f
};
NSAttributedString *attributeStr = [[NSAttributedString alloc] initWithString:yourOwnString];
self.topicContentLabel.attributedText = attributeStr;
如果還滿足不了需求?
畢竟需求是無止境的,比如分段屬性瓤檐,就添加一個(gè)范圍
//創(chuàng)建可變的文本屬性
NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString: yourOwnString];
//給所有字符設(shè)置字體為Zapfino赂韵,字體高度為15像素
[attributedString addAttribute: NSFontAttributeName value: [UIFont fontWithName: @"Zapfino" size: 16] range: NSMakeRange(0, yourOwnString.length)];
//分段屬性:前1個(gè)字符顏色設(shè)置成藍(lán)色
[attributedString addAttribute: NSForegroundColorAttributeName value: [UIColor blueColor] range: NSMakeRange(0, 1)];
//分段屬性:第2個(gè)字符開始的3個(gè)字符設(shè)置為紅色
[attributedString addAttribute: NSForegroundColorAttributeName value: [UIColor redColor] range: NSMakeRange(1, 3)];
最后還沒辦法的話,就去進(jìn)修兩個(gè)框架TextKit和CoreText吧挠蛉,聽說很厲(難)害(學(xué))祭示。