NSAttributedString 的21種屬性 詳解


原文連接:http://www.jianshu.com/p/09f54730feaa

先看看全部的Key

NSFontAttributeName; //字體,value是UIFont對象 NSParagraphStyleAttributeName;//繪圖的風格(居中,換行模式,間距等諸多風格),value是NSParagraphStyle對象 NSForegroundColorAttributeName;// 文字顏色,value是UIFont對象 NSBackgroundColorAttributeName;// 背景色,value是UIFont NSLigatureAttributeName; // 字符連體,value是NSNumber NSKernAttributeName; // 字符間隔 NSStrikethroughStyleAttributeName;//刪除線,value是NSNumber NSUnderlineStyleAttributeName;//下劃線,value是NSNumber NSStrokeColorAttributeName; //描繪邊顏色,value是UIColor NSStrokeWidthAttributeName; //描邊寬度,value是NSNumber NSShadowAttributeName; //陰影,value是NSShadow對象 NSTextEffectAttributeName; //文字效果,value是NSString NSAttachmentAttributeName;//附屬,value是NSTextAttachment 對象 NSLinkAttributeName;//連接,value是NSURL or NSString NSBaselineOffsetAttributeName;//基礎偏移量,value是NSNumber對象 NSUnderlineColorAttributeName;//下劃線顏色,value是UIColor對象 NSStrikethroughColorAttributeName;//刪除線顏色,value是UIColor NSObliquenessAttributeName; //字體傾斜 NSExpansionAttributeName; //字體扁平化 NSVerticalGlyphFormAttributeName;//垂直或者水平,value是 NSNumber,0表示水平,1垂直

NSAttributedString 能夠很是方便的實現文字排版和圖文混排功能. 共有21種效果(API), 本文將較詳細的介紹21種屬性的使用css

核心API: NSAttributedString. NSMutableAttributedString.編程

NSString *string = @"An NSAttributedString object manages character strings and associated sets of attributes (for example, font and kerning) that apply to individual characters or ranges of characters in the string. An association of characters and their attributes is called an attributed string. "; /* 這句話就是對這個類的一個最簡明扼要的歸納。NSAttributedString管理一個字符串,以及與該字符串中的單個字符或某些範圍的字符串相關的屬性。它有一個子類NSMutableAttributedString * 具體實現時,NSAttributedString維護了一個NSString,用來保存最原始的字符串,另有一個NSDictionary用來保存各個子串/字符的屬性。 */
#pragma mark - NSMutableAttributedString 建立 /* 三種初始化方法,NSMutableAttributedString沒有初始化方法,使用父類初始化方法, 使用initWithString:, initWithString:attributes:, 或者 initWithAttributedString: */ NSAttributedString *attStr = [[NSAttributedString alloc] initWithString:string attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:30]}]; NSMutableAttributedString *mAttStr = [[NSMutableAttributedString alloc] initWithString:string];

1.NSFontAttributeName ->設置字體屬性,默認值:字體:Helvetica(Neue) 字號:12數組

/* 設置字體大小及字體類型 */ NSRange font_range = [string rangeOfString:@"An"]; [mAttStr addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:20] range:font_range]; [mAttStr addAttribute:NSFontAttributeName value:[UIFont fontWithName:@"Courier-BoldOblique" size:17.0] range:NSMakeRange(10, 10)];

2.NSParagraphStyleAttributeName ->設置文本段落排版格式,取值爲 NSParagraphStyle 對象(詳情見以下代碼)瀏覽器

  • NSParagraphStyle與NSMutableParagraphStyle包括如下屬性ruby

    • alignment ->對齊方式
    • firstLineHeadIndent ->首行縮進
    • headIndent ->縮進
    • tailIndent ->尾部縮進
    • lineBreakMode ->斷行方式
    • maximumLineHeight ->最大行高
    • minimumLineHeight ->最低行高
    • lineSpacing ->行距
    • paragraphSpacing ->段距
    • paragraphSpacingBefore ->段首空間
    • baseWritingDirection ->句子方向
    • lineHeightMultiple ->可變行高,乘因數。
    • hyphenationFactor ->連字符屬性
NSMutableParagraphStyle *style = [[NSMutableParagraphStyle alloc] init]; style.firstLineHeadIndent = 10; style.lineSpacing = 10; [mAttStr addAttribute:NSParagraphStyleAttributeName value:style range:NSMakeRange(10, 10)];

3.NSForegroundColorAttributeName ->設置字體顏色,取值爲 UIColor對象,默認值爲黑色app

[mAttStr addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:NSMakeRange(10, 10)];

4.NSBackgroundColorAttributeName ->設置字體所在區域背景顏色,取值爲 UIColor對象,默認值爲nil, 透明色ide

[mAttStr addAttribute:NSBackgroundColorAttributeName value:[UIColor greenColor] range:NSMakeRange(10, 10)];

5.NSLigatureAttributeName ->設置連體屬性,取值爲NSNumber 對象(整數),0 表示沒有連體字符,1 表示使用默認的連體字符字體

[mAttStr addAttribute:NSLigatureAttributeName value:@1 range:NSMakeRange(10, 10)];

6.NSKernAttributeName ->設置字符間距,取值爲 NSNumber 對象(整數),正值間距加寬,負值間距變窄ui

[mAttStr addAttribute:NSKernAttributeName value:@2 range:NSMakeRange(10, 10)];

7.NSStrikethroughStyleAttributeName ->設置刪除線,取值爲 NSNumber 對象(整數)url

/* 值爲整型NSNumber,可取值爲 NSUnderlineStyle enum { NSUnderlineStyleNone = 0x00, NSUnderlineStyleSingle = 0x01, NSUnderlineStyleThick NS_ENUM_AVAILABLE(10_0, 7_0) = 0x02, NSUnderlineStyleDouble NS_ENUM_AVAILABLE(10_0, 7_0) = 0x09, NSUnderlinePatternSolid NS_ENUM_AVAILABLE(10_0, 7_0) = 0x0000, NSUnderlinePatternDot NS_ENUM_AVAILABLE(10_0, 7_0) = 0x0100, NSUnderlinePatternDash NS_ENUM_AVAILABLE(10_0, 7_0) = 0x0200, NSUnderlinePatternDashDot NS_ENUM_AVAILABLE(10_0, 7_0) = 0x0300, NSUnderlinePatternDashDotDot NS_ENUM_AVAILABLE(10_0, 7_0) = 0x0400, NSUnderlineByWord NS_ENUM_AVAILABLE(10_0, 7_0) = 0x8000 }; 設置刪除線。 */ [mAttStr addAttribute:NSStrikethroughStyleAttributeName value:@3 range:NSMakeRange(10, 10)];

8.NSStrikethroughColorAttributeName ->設置刪除線顏色,取值爲 UIColor 對象,默認值爲黑色

[mAttStr addAttribute:NSStrikethroughColorAttributeName value:[UIColor blueColor] range:NSMakeRange(10, 10)];

9.NSUnderlineStyleAttributeName ->設置下劃線,取值爲 NSNumber 對象(整數),枚舉常量 NSUnderlineStyle中的值,與刪除線相似

[mAttStr addAttribute:NSUnderlineStyleAttributeName value:@3 range:NSMakeRange(10, 10)];

10.NSUnderlineColorAttributeName ->設置下劃線顏色,取值爲 UIColor 對象,默認值爲nil

[mAttStr addAttribute:NSUnderlineColorAttributeName value:[UIColor blackColor] range:NSMakeRange(10, 10)];

11.NSStrokeWidthAttributeName ->設置筆畫寬度(粗細),取值爲 NSNumber 對象(整數),負值填充效果,正值中空效果

[mAttStr addAttribute:NSStrokeWidthAttributeName value:@10 range:NSMakeRange(10, 10)];

12.NSStrokeColorAttributeName ->填充部分顏色,不是字體顏色,取值爲 UIColor 對象 默認值爲nil,設置的屬性同ForegroundColor

[mAttStr addAttribute:NSStrokeColorAttributeName value:[UIColor radColor] range:NSMakeRange(10, 10)];

13.NSShadowAttributeName ->設置陰影屬性,取值爲 NSShadow 對象 默認值爲nil

NSShadow *shadow = [[NSShadow alloc]init]; shadow.shadowOffset = CGSizeMake(10, 10); shadow.shadowColor = [UIColor redColor]; [mAttStr addAttribute:NSShadowAttributeName value:shadow range:NSMakeRange(10, 10)];

14.NSTextEffectAttributeName ->設置文本特殊效果,取值爲 NSString 對象,目前只有圖版印刷效果可用, 使用此屬性指定的文字效果,如NSTextEffectLetterpressStyle。此屬性的默認值爲nil,表示沒有文本效應

[mAttStr addAttribute:NSTextEffectAttributeName value:NSTextEffectLetterpressStyle range:NSMakeRange(10, 10)];

15.NSBaselineOffsetAttributeName ->設置基線偏移值,取值爲 NSNumber (float),正值上偏,負值下偏, 默認值是0

[mAttStr addAttribute:NSBaselineOffsetAttributeName value:@1 range:NSMakeRange(10, 10)];

16.NSObliquenessAttributeName ->設置字形傾斜度,取值爲 NSNumber (float),正值右傾,負值左傾, 默認值是0(表示沒有傾斜)

[mAttStr addAttribute:NSObliquenessAttributeName value:@0.5 range:NSMakeRange(10, 10)];

17.NSExpansionAttributeName ->設置文本橫向拉伸屬性,取值爲 NSNumber (float),正值橫向拉伸文本,負值橫向壓縮文本

[mAttStr addAttribute:NSExpansionAttributeName value:@1.0 range:NSMakeRange(10, 10)];

18.NSWritingDirectionAttributeName ->設置文字書寫方向,從左向右書寫或者從右向左書寫

//取值爲包含NSNumber對象的數組. 從左向右書寫或者從右向左書寫. // NSArray of NSNumbers representing the nested levels of writing direction overrides as defined by Unicode LRE, RLE, LRO, and RLO characters. The control characters can be obtained by masking NSWritingDirection and NSTextWritingDirection values. LRE: NSWritingDirectionLeftToRight|NSWritingDirectionEmbedding, RLE: NSWritingDirectionRightToLeft|NSWritingDirectionEmbedding, LRO: NSWritingDirectionLeftToRight|NSWritingDirectionOverride, RLO: NSWritingDirectionRightToLeft|NSWritingDirectionOverride, [mAttStr addAttribute:NSWritingDirectionAttributeName value:@[@2] range:NSMakeRange(10, 10)];

19.NSVerticalGlyphFormAttributeName ->設置文字排版方向,取值爲 NSNumber 對象(整數),0 表示橫排文本,1 表示豎排文本 在iOS中, 老是以橫向排版

[mAttStr addAttribute:NSVerticalGlyphFormAttributeName value:@0 range:NSMakeRange(10, 10)];

20.NSLinkAttributeName ->設置連接屬性,點擊後調用瀏覽器打開指定URL地址

/** * 此屬性的值是NSURL對象(首選)或一個NSString對象。此屬性的默認值爲nil,表示沒有連接。 * UILabel沒法使用該屬性, 可使用UITextView 控件. */ UITextView *textView = [[UITextView alloc] initWithFrame:CGRectMake(50, 50, 50, 50)]; [self.view addSubview:textView]; textView.backgroundColor = [UIColor redColor]; NSString *strLink = @"百度連接"; NSAttributedString *attStrUrl = [[NSAttributedString alloc] initWithString:strLink attributes:@{NSLinkAttributeName: [NSURL URLWithString:@"http://www.baidu.com"]}]; textView.editable = NO; /* 簽定協議, 指定代理人以後. 但點擊連接時, 會回調協議方法 (- textView:shouldInteractWithURL:inRange:) */ textView.delegate = self; textView.attributedText = attStr;
- (BOOL)textView:(UITextView *)textView shouldInteractWithURL:(NSURL *)URL inRange:(NSRange)characterRange { NSLog(@"%s", __FUNCTION__); NSLog(@"url: %@", URL); return YES; }

注意:

@interface ViewController () <UITextViewDelegate>

21.NSAttachmentAttributeName ->設置文本附件,取值爲NSTextAttachment對象,經常使用於文字圖片混排

/* 這個屬性的值是一個NSTextAttachment對象。此屬性的默認值爲nil,表示無附件。*/ /** * 關於NSTextAttachment類的簡單說明 * * NSTextAttachment 類有一個指定的初始化方法(- initWithData:ofType:), 須要指定附件文檔的數據和附件文件的類型. 若是附件文檔數據指定nil, 那麼系統將會默認指定爲image對象做爲值. 所以, 也能夠經過這個特性實現圖文混排. * 下面就以附件爲image對象來講明NSAttachmentAttributeName的使用. * */ UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(50, 50, 50, 50)]; label.backgroundColor = [UIColor redColor]; [self.view addSubview:label]; /* 下面實如今百度兩個漢字之間插入一個照片 */ NSString *stiAtt = @"百度"; NSTextAttachment *attach = [[NSTextAttachment alloc] initWithData:nil ofType:nil]; attach.bounds = CGRectMake(0, 0, 50, 50); attach.image = [UIImage imageNamed:@"taobao.jpg"]; NSAttributedString *strAtt = [NSAttributedString attributedStringWithAttachment:attach]; NSMutableAttributedString *strMatt = [[NSMutableAttributedString alloc] initWithString:stiAtt]; [strMatt insertAttributedString:strAtt atIndex:1]; label.attributedText = strMatt; self.titleLabel.attributedText = mAttStr; [self.titleLabel sizeToFit];

在此要感謝個人啓蒙老師, 原哥, 是他帶我走上了iOS編程這條"不歸"之路!

相關文章
相關標籤/搜索