NSAttributedString帶屬性字符串常用屬性方法

使用NSAttributedString初始化,跟NSMutableString,NSString類似

使用方法:

為某一范圍內(nèi)文字設(shè)置多個屬性

- (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.? ? 常見的屬性及說明

NSFontAttributeName? 字體

NSParagraphStyleAttributeName? 段落格式

NSForegroundColorAttributeName? 字體顏色

NSBackgroundColorAttributeName? 背景顏色

NSStrikethroughStyleAttributeName 刪除線格式

NSUnderlineStyleAttributeName? ? ? 下劃線格式

NSStrokeColorAttributeName? ? ? ? 刪除線顏色

NSStrokeWidthAttributeName 刪除線寬度

NSShadowAttributeName? 陰影

1. // NSFontAttributeName? ? ? ? ? ? ? ? 設(shè)置字體屬性,默認值:字體:Helvetica(Neue) 字號:12

2. // NSForegroundColorAttributeNam? ? ? 設(shè)置字體顏色,取值為 UIColor對象,默認值為黑色

3. // NSBackgroundColorAttributeName? ? 設(shè)置字體所在區(qū)域背景顏色,取值為 UIColor對象,默認值為nil, 透明色

4. // NSLigatureAttributeName? ? ? ? ? ? 設(shè)置連體屬性,取值為NSNumber 對象(整數(shù)),0 表示沒有連體字符,1 表示使用默認的連體字符

5. // NSKernAttributeName? ? ? ? ? ? ? ? 設(shè)定字符間距,取值為 NSNumber 對象(整數(shù)),正值間距加寬,負值間距變窄

6. // NSStrikethroughStyleAttributeName? 設(shè)置刪除線,取值為 NSNumber 對象(整數(shù))

7. // NSStrikethroughColorAttributeName? 設(shè)置刪除線顏色,取值為 UIColor 對象,默認值為黑色

8. // NSUnderlineStyleAttributeName? ? ? 設(shè)置下劃線,取值為 NSNumber 對象(整數(shù)),枚舉常量 NSUnderlineStyle中的值,與刪除線類似

9. // NSUnderlineColorAttributeName? ? ? 設(shè)置下劃線顏色,取值為 UIColor 對象,默認值為黑色

10. // NSStrokeWidthAttributeName? ? ? ? 設(shè)置筆畫寬度,取值為 NSNumber 對象(整數(shù)),負值填充效果,正值中空效果

11. // NSStrokeColorAttributeName? ? ? ? 填充部分顏色,不是字體顏色,取值為 UIColor 對象

12. // NSShadowAttributeName? ? ? ? ? ? ? 設(shè)置陰影屬性,取值為 NSShadow 對象

13. // NSTextEffectAttributeName? ? ? ? ? 設(shè)置文本特殊效果,取值為 NSString 對象,目前只有圖版印刷效果可用:

14. // NSBaselineOffsetAttributeName? ? ? 設(shè)置基線偏移值,取值為 NSNumber (float),正值上偏,負值下偏

15. // NSObliquenessAttributeName? ? ? ? 設(shè)置字形傾斜度,取值為 NSNumber (float),正值右傾,負值左傾

16. // NSExpansionAttributeName? ? ? ? ? 設(shè)置文本橫向拉伸屬性,取值為 NSNumber (float),正值橫向拉伸文本,負值橫向壓縮文本

17. // NSWritingDirectionAttributeName? ? 設(shè)置文字書寫方向,從左向右書寫或者從右向左書寫

18. // NSVerticalGlyphFormAttributeName? 設(shè)置文字排版方向,取值為 NSNumber 對象(整數(shù)),0 表示橫排文本,1 表示豎排文本

19. // NSLinkAttributeName? ? ? ? ? ? ? ? 設(shè)置鏈接屬性,點擊后調(diào)用瀏覽器打開指定URL地址

20. // NSAttachmentAttributeName? ? ? ? ? 設(shè)置文本附件,取值為NSTextAttachment對象,常用于文字圖片混排

21. // NSParagraphStyleAttributeName? ? ? 設(shè)置文本段落排版格式,取值為 NSParagraphStyle 對象

3、 段落樣式

//? NSParagraphStyleAttributeName 段落的風(fēng)格(設(shè)置首行,行間距,對齊方式什么的)看自己需要什么屬性,寫什么

NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];

paragraphStyle.lineSpacing = 10;// 字體的行間距

paragraphStyle.firstLineHeadIndent = 20.0f;//首行縮進

paragraphStyle.alignment = NSTextAlignmentJustified;//(兩端對齊的)文本對齊方式:(左,中,右,兩端對齊,自然)

paragraphStyle.lineBreakMode = NSLineBreakByTruncatingTail;//結(jié)尾部分的內(nèi)容以……方式省略 ( "...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

圖文混排:

1、 直接使用NSTextAttachment插入圖片:

NSMutableAttributedString *attriStr=[[NSMutableAttributedString alloc] initWithString:content attributes:nil];

NSTextAttachment *attachment=[[NSTextAttachment alloc] initWithData:nil ofType:nil];

UIImage *img=[UIImage imageNamed:@"home_hui"];

attachment.image=img;// 插入圖

attachment.bounds=CGRectMake(0, -20, 40, 40);// 設(shè)置插入圖片的bounds

NSAttributedString *text=[NSAttributedString attributedStringWithAttachment:attachment];

[attriStr insertAttributedString:text atIndex:2];// 設(shè)置插入圖片的位置

label.attributedText=attriStr;

[self.view addSubview:label];

2、重載NSTextAttachment的方法,設(shè)置所插入圖片的顯示尺寸:

使用的時候 直接附圖片即可不需要考慮圖標(biāo)的占用尺寸!

- (CGRect)attachmentBoundsForTextContainer:(NSTextContainer *)textContainer proposedLineFragment:(CGRect)lineFrag glyphPosition:(CGPoint)position characterIndex:(NSUInteger)charIndex NS_AVAILABLE_IOS(7_0)

{

return CGRectMake( 0 , 0 , lineFrag.size.height , lineFrag.size.height );

}

4、 計算文本的尺寸:

計算NSAttributedString的尺寸:

CGSize titleSize = [attriStr boundingRectWithSize:CGSizeMake(SCREEN_W-38, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin context:nil].size;

計算普通NSString尺寸:

CGSize titleSize = [contentStr boundingRectWithSize:CGSizeMake(SCREEN_W-38, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:14.0]} context:nil].size;

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 轉(zhuǎn)載:http://blog.csdn.net/u010330109/article/details/518821...
    F麥子閱讀 4,321評論 0 3
  • 與NSString類似,在iOS中AttributedString也分為NSAttributedString和 N...
    錢十六閱讀 904評論 0 0
  • 悠游四海閱讀 339評論 8 7
  • 護照上莫名其妙多了很多中東國家的簽證,在土耳其遇到一個強奸犯但是逃走了,后來又遇到一個會說日語的藏族老婆婆其實是上...
    錦毛兔白玉瓜閱讀 118評論 0 0
  • 6歲的我們,哭著哭著笑了。。 16歲的我們,笑著笑著哭了。。 16歲,花季一般的年齡,可卻有說不出的傷與痛。沒有人...
    說好的一起閱讀 237評論 0 1

友情鏈接更多精彩內(nèi)容