###1、Attributes字典鍵值對照表bash
鍵 | 值類型 | 備註 |
---|---|---|
NSKernAttributeName | NSNumber | 文字間距,支持浮點型 |
NSFontAttributeName | UIFont | 字體 |
NSForegroundColorAttributeName | UIColor | 顏色 |
NSParagraphStyleAttributeName | NSMutableParagraphStyle | 段落樣式,詳細見【1】 |
NSBackgroundColorAttributeName | UIColor | 字體的所佔區域的背景顏色 |
NSStrokeColorAttributeName | UIColor | 字體邊框的顏色 |
NSStrokeWidthAttribute | NSNumber | 邊框的寬度 |
NSStrikethroughStyleAttributeName | NSNumber | 刪除線 |
NSUnderlineStyleAttributeName | NSNumber | 下劃線 |
NSVerticalGlyphFormAttributeName | NSNumber | 文字排版方向0 表示橫排文本.1 表示豎排文本,iOS只有橫排 |
NSObliquenessAttributeName | NSNumber | 文字傾斜程度 |
NSExpansionAttributeName | NSNumber | 文字的扁平化程度 |
NSShadowAttributeName | NSShadow | 文字陰影,見【2】 |
【1】NSMutableParagraphStyle類使用範例app
NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
paragraphStyle.lineSpacing = 10;// 字體的行間距
paragraphStyle.firstLineHeadIndent = 20.0f;//首行縮進
paragraphStyle.alignment = NSTextAlignmentJustified;//(兩端對齊的)文本對齊方式:(左,中,右,兩端對齊,天然)
paragraphStyle.lineBreakMode = NSLineBreakByTruncatingTail;//結尾部分的內容以……方式省略 ( "...wxyz" ,"abcd..." ,"ab...yz")
paragraphStyle.headIndent = 20;//總體縮進(首行除外)
paragraphStyle.tailIndent = 20;//尾部縮進
paragraphStyle.minimumLineHeight = 10;//最低行高
paragraphStyle.maximumLineHeight = 20;//最大行高
paragraphStyle.paragraphSpacing = 15;//段與段之間的間距
paragraphStyle.paragraphSpacingBefore = 22.0f;//段首行空白空間/* Distance between the bottom of the previous paragraph (or the end of its paragraphSpacing, if any) and the top of this paragraph. */paragraphStyle.baseWritingDirection = NSWritingDirectionLeftToRight;//從左到右的書寫方向(一共➡️⬇️⬅️三種)
paragraphStyle.lineHeightMultiple = 15;/* Natural line height is multiplied by this factor (if positive) before being constrained by minimum and maximum line height. */
paragraphStyle.hyphenationFactor = 1;//連字屬性 在iOS,惟一支持的值分別爲0和1
複製代碼
【2】NSShadowAttributeName對應的是NSShadow對象,單一使用不會有任何效果,須要配合着NSVerticalGlyphFormAttributeName(文字排版方向)、NSObliquenessAttributeName(文字傾斜)、NSExpansionAttributeName(文字扁平化)配合使用,NSShadow相關屬性設置以下所示.框架
NSShadow *shadow = [[NSShadow alloc]init];
shadow.shadowBlurRadius = 5;//模糊度
shadow.shadowColor = [UIColor whiteColor];//陰影顏色
shadow.shadowOffset = CGSizeMake(1, 5);//陰影的大小
複製代碼
1.NSAttributedString 屬性字體
//均爲只讀
@property (readonly, copy) NSString *string;
@property (readonly) NSUInteger length;
複製代碼
建立方法ui
- (instancetype)initWithString:(NSString *)str;
- (instancetype)initWithAttributedString:(NSAttributedString *)attrStr;
- (instancetype)initWithString:(NSString *)str attributes:(nullable NSDictionary<NSString *, id> *)attrs;
複製代碼
其餘方法this
//富文本字符串比較
- (BOOL)isEqualToAttributedString:(NSAttributedString *)other;
複製代碼
2.NSMutableAttributedString 做爲NSAttributedString的子類增長了如下動態添加富文本樣式的方法,見文思意,也沒必要過多解釋。spa
- (void)addAttribute:(NSString *)name value:(id)value range:(NSRange)range;
- (void)addAttributes:(NSDictionary<NSString *, id> *)attrs range:(NSRange)range;
- (void)removeAttribute:(NSString *)name range:(NSRange)range;
- (void)replaceCharactersInRange:(NSRange)range withAttributedString:(NSAttributedString *)attrString;
- (void)insertAttributedString:(NSAttributedString *)attrString atIndex:(NSUInteger)loc;
- (void)appendAttributedString:(NSAttributedString *)attrString;
//徹底替換掉attributeString的內容
- (void)setAttributedString:(NSAttributedString *)attrString;
複製代碼
我是分割區~~~大神,女友喊你回家吃飯,你能夠先走了。3d
###3、demo 若是以上看完了,還不懂怎麼使用的朋友。那不妨看看在下拙劣的demo吧。(注:Label的背景顏色都是在xib(nib)中設置的)code
NSAttributedString *string = [[NSAttributedString alloc] initWithString:@"改變文字間距,間距爲6"attributes:@{NSKernAttributeName:@6}];
_label1.attributedText = string;
複製代碼
NSAttributedString *string = [[NSAttributedString alloc] initWithString:@"改變文字字體,20號粗體"attributes:@{NSFontAttributeName:[UIFont boldSystemFontOfSize:20]}];
_label2.attributedText = string;
複製代碼
NSAttributedString *string = [[NSAttributedString alloc] initWithString:@"改變文字顏色,綠色"attributes:@{NSForegroundColorAttributeName:[UIColor greenColor]}];
_label3.attributedText = string;
複製代碼
NSMutableAttributedString *mutableAttributedstring = [[NSMutableAttributedString alloc] initWithString:@"改變文字邊框顏色寬度,這二者協同使用纔有效果,寬度正數爲空心(前景色失效),負數描邊"];
//空心
[mutableAttributedstring addAttribute:NSStrokeWidthAttributeName value:@3.2 range:NSMakeRange(0, 37)];
[mutableAttributedstring addAttribute:NSStrokeColorAttributeName value:[UIColor greenColor] range:NSMakeRange(0, 37)];
//描邊
[mutableAttributedstring addAttribute:NSStrokeWidthAttributeName value:@-5 range:NSMakeRange(38, mutableAttributedstring.length - 38)];
[mutableAttributedstring addAttribute:NSStrokeColorAttributeName value:[UIColor redColor] range:NSMakeRange(38, mutableAttributedstring.length - 38)];
_label4.numberOfLines = 3;
_label4.attributedText = mutableAttributedstring;
複製代碼
NSMutableAttributedString *mutableAttributedstring = [[NSMutableAttributedString alloc] initWithString:@"一條刪除線,兩條刪除線,加粗刪除線"];
[mutableAttributedstring addAttribute:NSStrikethroughStyleAttributeName value:[NSNumber numberWithInteger:NSUnderlineStyleSingle] range:NSMakeRange(0,5)];
[mutableAttributedstring addAttribute:NSStrikethroughStyleAttributeName value:[NSNumber numberWithInteger:NSUnderlineStyleDouble] range:NSMakeRange(6,5)];
[mutableAttributedstring addAttribute:NSStrikethroughStyleAttributeName value:[NSNumber numberWithInteger:NSUnderlineStyleThick] range:NSMakeRange(12,5)];
_label5.attributedText = mutableAttributedstring;
複製代碼
NSMutableAttributedString *mutableAttributedstring = [[NSMutableAttributedString alloc] initWithString:@"一條下劃線,兩條下劃線,加粗下劃線"];
[mutableAttributedstring addAttribute:NSUnderlineStyleAttributeName value:[NSNumber numberWithInteger:NSUnderlineStyleSingle] range:NSMakeRange(0,5)];
[mutableAttributedstring addAttribute:NSUnderlineStyleAttributeName value:[NSNumber numberWithInteger:NSUnderlineStyleDouble] range:NSMakeRange(6,5)];
[mutableAttributedstring addAttribute:NSUnderlineStyleAttributeName value:[NSNumber numberWithInteger:NSUnderlineStyleThick] range:NSMakeRange(12,5)];
_label6.attributedText = mutableAttributedstring;
複製代碼
NSMutableAttributedString *mutableAttributedstring = [[NSMutableAttributedString alloc] initWithString:@"文字傾斜,文字陰影,壓扁文字"];
[mutableAttributedstring addAttribute:NSObliquenessAttributeName value:@1 range:NSMakeRange(0,4)];
//文字陰影
NSShadow *shadow = [[NSShadow alloc] init];
//模糊度
shadow.shadowBlurRadius = 5;
//陰影顏色
shadow.shadowColor = [UIColor yellowColor];
//陰影大小
shadow.shadowOffset = CGSizeMake(2, 5);
[mutableAttributedstring addAttribute:NSShadowAttributeName value:shadow range:NSMakeRange(5,4)];
//壓扁文字
[mutableAttributedstring addAttribute:NSExpansionAttributeName value:@0.8 range:NSMakeRange(10,4)];
_label7.numberOfLines = 2;
_label7.attributedText = mutableAttributedstring;
複製代碼
NSMutableAttributedString *mutableAttributedstring = [[NSMutableAttributedString alloc] initWithString:@"據外媒報道,美國太空探索技術公司SpaceX原定於北京時間2月18日23時01分,在佛羅里達州發射「龍」無人貨運飛船,運送補給物資前往國際太空站,並嘗試回收搭載飛船的「獵鷹9」號火箭。但二級火箭在倒計時13秒時因引擎故障,宣佈取消發射。\n在發射暫停後約10分鐘後,SpaceX首席執行官埃隆·馬斯克(Elon Musk)在社交媒體上發佈消息稱:「一切運行正常,除了二級火箭發動機轉向液壓活塞的運動軌跡稍顯奇怪,須要進行調查。」"];
NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
// 字體的行間距
paragraphStyle.lineSpacing = 10;
//首行縮進
paragraphStyle.firstLineHeadIndent = 20.0f;
//(兩端對齊的)文本對齊方式:(左,中,右,兩端對齊,天然)
// paragraphStyle.alignment = NSTextAlignmentLeft;
//結尾部分的內容以……方式省略 ( "...wxyz" ,"abcd..." ,"ab...yz")
// paragraphStyle.lineBreakMode = NSLineBreakByTruncatingTail;
paragraphStyle.headIndent = 20;//總體縮進(首行除外)
//設置尾部縮進後就出問題
// paragraphStyle.tailIndent = 2;//尾部縮進
paragraphStyle.minimumLineHeight = 10;//最低行高
paragraphStyle.maximumLineHeight = 20;//最大行高
// paragraphStyle.paragraphSpacing = 15;//段與段之間的間距
paragraphStyle.paragraphSpacingBefore = 22.0f;//段首行空白空間/* Distance between the bottom of the previous paragraph (or the end of its paragraphSpacing, if any) and the top of this paragraph. */
paragraphStyle.baseWritingDirection = NSWritingDirectionLeftToRight;//從左到右的書寫方向(一共➡️⬇️⬅️三種)
paragraphStyle.lineHeightMultiple = 15;/* Natural line height is multiplied by this factor (if positive) before being constrained by minimum and maximum line height. */
paragraphStyle.hyphenationFactor = 1;//連字屬性 在iOS,惟一支持的值分別爲0和1
[mutableAttributedstring addAttribute:NSParagraphStyleAttributeName value:paragraphStyle range:NSMakeRange(0, mutableAttributedstring.length)];
[mutableAttributedstring addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:10] range:NSMakeRange(0, mutableAttributedstring.length)];
_label8.numberOfLines = MAXFLOAT;
_label8.attributedText = mutableAttributedstring;
複製代碼
經常使用的也就這些了吧,更深刻仍是用富文本框架吧。orm