#向參考的博客原文作者致敬
#參考鏈接:
http://www.reibang.com/p/8f49c9c99b21
http://www.cnblogs.com/wangbinios/p/6123946.html
http://blog.csdn.net/liujinlongxa/article/details/44840753
#待學習鏈接
http://www.itnose.net/detail/6177538.html
http://blog.csdn.net/ys410900345/article/details/25976179
http://www.cocoachina.com/ios/20160512/16223.html
本篇博客主要是NSMutableAttributedString的簡介篇莉炉,詳細篇后續(xù)有時間跟新啤呼,歡迎大家批評改正。
一呢袱、創(chuàng)建方式
簡述:
NSMutableAttributedString適用的控件非常多,例如:UITextView
翅敌、UILabel
羞福、UIButton
....等,這里總結(jié)就以《UILabel》為例子蚯涮。
《1》治专、方式一:
/* 創(chuàng)建一個label */
UILabel *testLabel = [[UILabel alloc] initWithFrame:CGRectMake(30, 60, 200, 200)];
/* 自動換行 */
testLabel.numberOfLines = 0;
/* 初始化的字符串 */
NSString *testStr = @"年輕人就要多去挑戰(zhàn),要讓不可能成為可能";
#/* 初始化的方式創(chuàng)建 */
NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:testStr];
#//也可以使用此方法初始化創(chuàng)建- (instancetype)initWithString:(NSString *)str;
/****** 最基本的添加屬性的方式,使用哪一個屬性就添加哪一個 ,這里只是隨便使用了幾個屬性后續(xù)有時間還會一一介紹遭顶。******/
/*****
*@Attribute 參數(shù)含義:添加的屬性名稱
*@value:屬性所需要賦的值
*@range: 添加的屬性適應的范圍
************/
[attributedString addAttribute:NSFontAttributeName value:[UIFont fontWithName: @"Zapfino" size: 15] range:NSMakeRange(0, testStr.length)];
[attributedString addAttribute: NSForegroundColorAttributeName value: [UIColor blueColor] range: NSMakeRange(0, 4)];
[attributedString addAttribute: NSForegroundColorAttributeName value: [UIColor redColor] range: NSMakeRange(4, 3)];
#/* 這里需要注意UILabel之前是使用Label的`text`屬性進行賦值的张峰,但是使用NSMutableAttributedString設置完文本后就要改成控件的`attributedText`屬性進行賦值否則會有警告并且賦值不成功。*/
testLabel.attributedText = attributedString;
[self.view addSubview:testLabel]
《2》棒旗、方式二:
/* 創(chuàng)建一個label */
UILabel *testLabel = [[UILabel alloc] initWithFrame:CGRectMake(30, 60, 200, 200)];
/* 自動換行 */
testLabel.numberOfLines = 0;
/* 初始化的字符串 */
NSString *testStr = @"年輕人就要多去挑戰(zhàn),要讓不可能成為可能";
#/*使用字典創(chuàng)建喘批,然后直接將要設置的屬性添加到字典中 */
NSDictionary *attrDict = @{ NSFontAttributeName: [UIFont fontWithName: @"Zapfino" size: 15], NSForegroundColorAttributeName: [UIColor blueColor] };
#/* 這里使用NSMutableAttributedString中自帶的一個個實例方法將剛才設置的全部屬性添加進去*/
testLabel.attributedText = [[NSAttributedString alloc] initWithString: testStr attributes: attrDict];
[self.view addSubview:testLabel];
二、方法簡介
《1》铣揉、常用方法簡介
/* 初始化方法 */
- (instancetype)initWithString:(NSString *)str;
- (instancetype)initWithString:(NSString *)str attributes:(nullable NSDictionary<NSString *, id> *)attrs;
- (instancetype)initWithAttributedString:(NSAttributedString *)attrStr;
/* 為某一范圍內(nèi)文字設置多個屬性 */
- (void)setAttributes:(NSDictionary *)attrs range:(NSRange)range;
/* 為某一范圍內(nèi)文字添加某個屬性 */
- (void)addAttribute:(NSString *)name value:(id)value range:(NSRange)range;
/* 為某一范圍內(nèi)文字添加多個屬性 */
- (void)addAttributes:(NSDictionary *)attrs range:(NSRange)range;
/*移除某范圍內(nèi)的某個屬性*/
- (void)removeAttribute:(NSString *)name range:(NSRange)range;
《2》饶深、詳細介紹一些方法
[1]、獲取指定位置上的屬性信息逛拱,并返回與指定位置屬性相同并且連續(xù)的字符串的范圍信息敌厘。
- (NSDictionary<NSString *, id> *)attributesAtIndex:(NSUInteger)location effectiveRange:(nullable NSRangePointer)range;
#例如:
NSRange range = NSMakeRange(0, 2);
NSDictionary*dic = [attributedString attributesAtIndex:2 effectiveRange:&range];
#參數(shù)介紹:
@location :為位置信息 對應例子中attributesAtIndex:2
@effectiveRange 有效范圍 對應例子中&range
[2]、獲取指定范圍內(nèi)的子字符串的信息
#參數(shù)介紹:
@NSAttributedString 返回一個NSAttributedString是咧對象
@range 從哪一個范圍獲取
- (NSAttributedString *)attributedSubstringFromRange:(NSRange)range;
[3]朽合、** 在Range中查找string從attributesAtIndex:開始找俱两,找到的第一個屬性,即attributeDict曹步,把屬性的位置傳給longestEffectiveRange:屬性 **
#參數(shù)介紹:
@inRange 在字符串中的范圍查找
@attributesAtIndex 從哪一個索引開始查找宪彩,找到第一個屬性
@longestEffectiveRange 將第一個屬性的位置傳過來
#/* 方法 */
- (NSDictionary<NSString *, id> *)attributesAtIndex:(NSUInteger)location longestEffectiveRange:(nullable NSRangePointer)range inRange:(NSRange)rangeLimit;
能成為可能";
#范例:
/* 創(chuàng)建一個label */ UILabel *testLabel = [[UILabel alloc] initWithFrame:CGRectMake(30, 60, 200, 200)];
[self.view addSubview:testLabel];
NSString *testStr = @"年輕人就要多去挑戰(zhàn),要讓不可能成為可能";
NSDictionary *attrDict = @{ NSFontAttributeName: [UIFont fontWithName: @"Zapfino" size: 15], NSForegroundColorAttributeName: [UIColor blueColor] };
NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:testStr attributes:attrDict];
[attributedString addAttribute:NSForegroundColorAttributeNamevalue: [UIColor redColor] range:NSMakeRange(7, 5)];
NSDictionary *attributeDict;
NSRange effectiveRange = { 0, 10 };
do {
NSRange range = NSMakeRange (NSMaxRange(effectiveRange), [attributedString length] - NSMaxRange(effectiveRange));
attributeDict = [attributedString attributesAtIndex: range.location longestEffectiveRange: &effectiveRange inRange: range];
NSLog (@"Range: %@ Attributes: %@",
NSStringFromRange(effectiveRange), attributeDict);
testLabel.attributedText = attributedString;
} while (NSMaxRange(effectiveRange) < [attributedString length]);
??????[4]、判斷兩個NSAttributedString是否相等
- (BOOL)isEqualToAttributedString:(NSAttributedString *)other;
三箭窜、屬性簡介
1毯焕、NSFontAttributeName(字體)
** 該屬性所對應的值是一個 UIFont 對象。該屬性用于改變一段文本的字體磺樱。如果不指定該屬性纳猫,則默認為12-point Helvetica(Neue)。**
2竹捉、NSParagraphStyleAttributeName(段落)
** 該屬性所對應的值是一個 NSParagraphStyle 對象芜辕。該屬性在一段文本上應用多個屬性。如果不指定該屬性块差,則默認為 NSParagraphStyle 的defaultParagraphStyle 方法返回的默認段落屬性侵续。**
3倔丈、NSForegroundColorAttributeName(字體顏色)
** 該屬性所對應的值是一個 UIColor 對象。該屬性用于指定一段文本的字體顏色状蜗。如果不指定該屬性需五,則默認為黑色。**
4轧坎、NSBackgroundColorAttributeName(字體背景色)
** 該屬性所對應的值是一個 UIColor 對象宏邮。該屬性用于指定一段文本的背景顏色。如果不指定該屬性缸血,則默認無背景色蜜氨。**
5、NSLigatureAttributeName(連字符)
該屬性所對應的值是一個 NSNumber 對象(整數(shù))捎泻。連體字符是指某些連在一起的字符飒炎,它們采用單個的圖元符號。
- 0 表示沒有連體字符笆豁。
- 1 表示使用默認的連體字符郎汪。
- 2表示使用所有連體符號。
- 默認值為 1(注意闯狱,iOS 不支持值為 2)
6怒竿、NSKernAttributeName(字間距)
** NSKernAttributeName 設定字符間距,取值為 NSNumber 對象(整數(shù))扩氢,正值間距加寬耕驰,負值間距變窄**
7、NSStrikethroughStyleAttributeName(刪除線)
NSStrikethroughStyleAttributeName 設置刪除線录豺,取值為 NSNumber 對象(整數(shù))朦肘,枚舉常量 NSUnderlineStyle中的值:
NSUnderlineStyleNone。/* 默認值 */
NSUnderlineStyleNone /* 不設置刪除線 */
NSUnderlineStyleSingle/* 設置刪除線為細單實線 */
NSUnderlineStyleThick/* 設置刪除線為粗單實線 */
NSUnderlineStyleDouble /* 設置刪除線為細雙實線 */
8双饥、NSStrikethroughColorAttributeName (刪除線顏色)
** NSStrikethroughColorAttributeName 設置刪除線顏色媒抠,取值為 UIColor 對象,默認值為黑色**
9咏花、NSUnderlineStyleAttributeName(下劃線)
該屬性所對應的值是一個 NSNumber 對象(整數(shù))趴生。
該值指定是否在文字上加上下劃線,該值參考“Underline Style Attributes”昏翰。
默認值是NSUnderlineStyleNone苍匆。
#下劃線除了線條位置和刪除線不同外,其他的都可以完全參照刪除線設置棚菊。
10浸踩、NSUnderlineColorAttributeName(下劃線顏色)
** NSUnderlineColorAttributeName 設置下劃線顏色,取值為 UIColor 對象统求,默認值為黑色 **
11检碗、NSStrokeColorAttributeName(邊線顏色)
12据块、NSStrokeWidthAttributeName(邊線寬度)
13、NSShadowAttributeName(陰影)
該屬性所對應的值是一個 NSShadow 對象折剃。默認為 nil另假。單獨設置不好使,和這三個任一個都好使怕犁,NSVerticalGlyphFormAttributeName,
NSObliquenessAttributeName,
NSExpansionAttributeName
14浪谴、NSVerticalGlyphFormAttributeName(橫豎排版)
** 該屬性所對應的值是一個 NSNumber 對象(整數(shù))。0 表示橫排文本因苹。1 表示豎排文本。在 iOS 中篇恒,總是使用橫排文本扶檐,0 以外的值都未定義。**