iOS自己總結(jié)的超級詳細分解富文本大全(AttributedString),圖文混排很輕松

iOS自己總結(jié)的超級詳細分解富文本大全(AttributedString)岖食,圖文混排很輕松

轉(zhuǎn)載于?http://blog.csdn/deft_mkjing/article/details/52141936


NSFontAttributeName???????????????設(shè)置字體大小和字體的類型?默認12Helvetica(Neue)

NSForegroundColorAttributeName????設(shè)置字體顏色,默認黑色?UIColor對象

NSBackgroundColorAttributeName????設(shè)置字體所在區(qū)域的背景顏色,默認為nil浙垫,透明色

NSLigatureAttributeName???????????設(shè)置連體屬性,NSNumber對象?默認0沒有連體

NSKernAttributeName???????????????設(shè)置字符間距郑诺,?NSNumber浮點型屬性?正數(shù)間距加大夹姥,負數(shù)間距縮小

NSStrikethroughStyleAttributeName?設(shè)置刪除線,NSNumber對象

NSStrikethroughColorAttributeName?設(shè)置刪除線顏色辙诞,UIColor對象辙售,默認是黑色

NSUnderlineStyleAttributeName?????設(shè)置下劃線,NSNumber對象?NSUnderlineStyle枚舉值

NSUnderlineColorAttributeName?????設(shè)置下劃線顏色飞涂,UIColor對象旦部,默認是黑色

NSStrokeWidthAttributeName????????設(shè)置筆畫寬度,NSNumber對象?正數(shù)中空?負數(shù)填充

NSStrokeColorAttributeName????????設(shè)置填充部分顏色较店,不是指字體顏色志鹃,UIColor對象

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

NSTextEffectAttributeName?????????設(shè)置文本特殊效果?NSString對象?只有圖版印刷效果可用

NSBaselineOffsetAttributeName?????設(shè)置基線偏移量泽西,NSNumberfloat對象?正數(shù)向上偏移曹铃,負數(shù)向下偏移

NSObliquenessAttributeName????????設(shè)置字體傾斜度,NSNumberfloat對象捧杉,正數(shù)右傾斜陕见,負數(shù)左傾斜

NSExpansionAttributeName??????????設(shè)置文本橫向拉伸屬性秘血,NSNumberfloat對象,正數(shù)橫向拉伸文本评甜,負數(shù)壓縮

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

NSVerticalGlyphFormAttributeName??設(shè)置文本排版方向,NSNumber對象忍坷。0橫向排版炭玫,1豎向排版

NSLinkAttributeName???????????????設(shè)置文本超鏈接或详,點擊可以打開指定URL地址

NSAttachmentAttributeName?????????設(shè)置文本附件眉睹,取值為NSTextAttachment對象舞终,一般為圖文混排

NSParagraphStyleAttributeName?????設(shè)置文本段落排版,為NSParagraphStyle對象

道來旬薯,每個屬性一行晰骑,分分鐘教大家簡單學(xué)習(xí)富文本

1.NSFontAttributeName

說明:該屬性用于改變一段文本的字體。如果不指定該屬性绊序,則默認為12-point Helvetica(Neue)

2.NSForegroundColorAttributeName

說明:該屬性用于指定一段文本的字體顏色硕舆。如果不指定該屬性,則默認為黑色

3.NSBackgroundColorAttributeName

說明:設(shè)置文字背景顏色

NSString*string?=@"落霞與孤鶩齊飛,秋水共長天一色";

NSMutableAttributedString*mutableAttriteStr?=?[[NSMutableAttributedStringalloc]init];

NSAttributedString*attributeStr1=?[[NSAttributedStringalloc]initWithString:[stringsubstringWithRange:NSMakeRange(0,3)]attributes:@{NSFontAttributeName?:[UIFontfontWithName:@"futura"size:12],NSForegroundColorAttributeName?:?[UIColorredColor],NSBackgroundColorAttributeName?:?[UIColoryellowColor]}];

NSAttributedString*attributeStr4=?[[NSAttributedStringalloc]initWithString:[stringsubstringWithRange:NSMakeRange(3,4)]attributes:@{NSFontAttributeName?:[UIFontfontWithName:@"futura"size:22],NSForegroundColorAttributeName?:?[UIColorredColor],NSBackgroundColorAttributeName?:?[UIColoryellowColor]}];

NSAttributedString*attributeStr2=?[[NSAttributedStringalloc]initWithString:[stringsubstringWithRange:NSMakeRange(8,4)]attributes:@{NSFontAttributeName?:?[UIFontboldSystemFontOfSize:22],NSForegroundColorAttributeName?:?[UIColorblackColor],NSBackgroundColorAttributeName?:?[UIColorlightGrayColor]}];

NSAttributedString*attributeStr5=?[[NSAttributedStringalloc]initWithString:[stringsubstringWithRange:NSMakeRange(12,3)]attributes:@{NSFontAttributeName?:?[UIFontboldSystemFontOfSize:12],NSForegroundColorAttributeName?:?[UIColorblackColor],NSBackgroundColorAttributeName?:?[UIColorlightGrayColor]}];

NSAttributedString*attriteStr3=?[[NSAttributedStringalloc]initWithString:[stringsubstringWithRange:NSMakeRange(7,1)]attributes:@{NSBackgroundColorAttributeName?:?[UIColorgreenColor]}];

[mutableAttriteStrappendAttributedString:attributeStr1];

[mutableAttriteStrappendAttributedString:attributeStr4];

[mutableAttriteStrappendAttributedString:attriteStr3];

[mutableAttriteStrappendAttributedString:attributeStr2];

[mutableAttriteStrappendAttributedString:attributeStr5];

self.label1.attributedText=?mutableAttriteStr;

4.NSLigatureAttributeName

連體字符是指某些連在一起的字符骤公,它們采用單個的圖元符號抚官。0 表示沒有連體字符。1

表示使用默認的連體字符阶捆。2表示使用所有連體符號凌节。默認值為 1(注意,iOS不支持值

為 2)

//?連體藝術(shù)字趁猴,不是每個都能連起的刊咳,f和l??f和i就可以彪见,其他各位可以自己去試試

self.label2.attributedText=?[[NSAttributedStringalloc]initWithString:@"flush?and?fily"attributes:@{NSLigatureAttributeName?:?[NSNumbernumberWithInt:1],NSFontAttributeName?:?[UIFontfontWithName:@"futura"size:30]}];

NSMutableAttributedString*mutableAttributeStr2=?[[NSMutableAttributedStringalloc]init];

NSAttributedString*string6=?[[NSAttributedStringalloc]initWithString:@"ABCDE?"attributes:@{NSKernAttributeName?:?[NSNumbernumberWithInt:-3],NSForegroundColorAttributeName?:?[UIColorredColor]}];

NSAttributedString*string7=?[[NSAttributedStringalloc]initWithString:@"FGHIJ?"attributes:@{NSKernAttributeName?:?[NSNumbernumberWithInt:0],NSForegroundColorAttributeName?:?[UIColoryellowColor]}];

NSAttributedString*string8=?[[NSAttributedStringalloc]initWithString:@"KLMNO?"attributes:@{NSKernAttributeName?:?@(15),NSForegroundColorAttributeName?:?[UIColorblueColor]}];

[mutableAttributeStr2appendAttributedString:string6];

[mutableAttributeStr2appendAttributedString:string7];

[mutableAttributeStr2appendAttributedString:string8];

self.label3.attributedText=?mutableAttributeStr2;

5.NSKernAttributeName

字符間距正值間距加寬儡司,負值間距變窄

[objc]view plaincopy

NSMutableAttributedString*mutableAttributeStr2=?[[NSMutableAttributedStringalloc]init];

NSAttributedString*string6=?[[NSAttributedStringalloc]initWithString:@"ABCDE?"attributes:@{NSKernAttributeName?:?[NSNumbernumberWithInt:-3],NSForegroundColorAttributeName?:?[UIColorredColor]}];

NSAttributedString*string7=?[[NSAttributedStringalloc]initWithString:@"FGHIJ?"attributes:@{NSKernAttributeName?:?[NSNumbernumberWithInt:0],NSForegroundColorAttributeName?:?[UIColoryellowColor]}];

NSAttributedString*string8=?[[NSAttributedStringalloc]initWithString:@"KLMNO?"attributes:@{NSKernAttributeName?:?@(15),NSForegroundColorAttributeName?:?[UIColorblueColor]}];

[mutableAttributeStr2appendAttributedString:string6];

[mutableAttributeStr2appendAttributedString:string7];

[mutableAttributeStr2appendAttributedString:string8];

self.label3.attributedText=?mutableAttributeStr2;

6.NSStrikethroughStyleAttributeName ?和 NSStrikethroughColorAttributeName

NSStrikethroughStyleAttributeName 設(shè)置刪除線,取值為 NSNumber 對象(整數(shù))余指,

NSStrikethroughColorAttributeName 設(shè)置刪除線顏色

枚舉常量 NSUnderlineStyle中的值:

NSUnderlineStyleNone? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? =0x00, ?不設(shè)置

NSUnderlineStyleSingle? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? =0x01, ? 設(shè)置單細刪除線

NSUnderlineStyleThickNS_ENUM_AVAILABLE(10_0,7_0) ? =0x02, 設(shè)置粗單刪除線

NSUnderlineStyleDoubleNS_ENUM_AVAILABLE(10_0,7_0) ? ? =0x09,雙細刪除線

[objc]view plaincopy

NSMutableAttributedString*mutableAttributeStr3=?[[NSMutableAttributedStringalloc]init];

NSAttributedString*string9=?[[NSAttributedStringalloc]initWithString:@"只要998??"attributes:@{NSStrikethroughStyleAttributeName?:?@(NSUnderlineStyleSingle),NSStrikethroughColorAttributeName?:?[UIColorredColor]}];

NSAttributedString*string10=?[[NSAttributedStringalloc]initWithString:@"真的998??"attributes:@{NSStrikethroughStyleAttributeName?:?@(NSUnderlineStyleThick),NSStrikethroughColorAttributeName?:?[UIColorblueColor]}];

NSAttributedString*string11=?[[NSAttributedStringalloc]initWithString:@"好吧9塊拿走"attributes:@{NSStrikethroughStyleAttributeName?:?@(NSUnderlineStyleDouble),NSStrikethroughColorAttributeName?:?[UIColoryellowColor]}];

[mutableAttributeStr3appendAttributedString:string9];

[mutableAttributeStr3appendAttributedString:string10];

[mutableAttributeStr3appendAttributedString:string11];

self.label4.attributedText=?mutableAttributeStr3;

7.NSUnderlineStyleAttributeName 和

NSUnderlineColorAttributeName

給文字加下劃線和更換下劃線顏色捕犬,屬性和上面的刪除線都是一樣用的

[objc]view plaincopy

NSMutableAttributedString*mutableAttributeStr4=?[[NSMutableAttributedStringalloc]init];

NSAttributedString*string12=?[[NSAttributedStringalloc]initWithString:@"只要888??"attributes:@{NSUnderlineStyleAttributeName?:?@(NSUnderlineStyleSingle),NSUnderlineColorAttributeName?:?[UIColorredColor]}];

NSAttributedString*string13=?[[NSAttributedStringalloc]initWithString:@"真的88??"attributes:@{NSUnderlineStyleAttributeName?:?@(NSUnderlineStyleThick),NSUnderlineColorAttributeName?:?[UIColorpurpleColor]}];

NSAttributedString*string14=?[[NSAttributedStringalloc]initWithString:@"好吧8塊拿走"attributes:@{NSUnderlineStyleAttributeName?:?@(NSUnderlineStyleDouble),NSUnderlineColorAttributeName?:?[UIColoryellowColor]}];

[mutableAttributeStr4appendAttributedString:string12];

[mutableAttributeStr4appendAttributedString:string13];

[mutableAttributeStr4appendAttributedString:string14];

self.label5.attributedText=?mutableAttributeStr4;

8.NSStrokeWidthAttributeName 和NSStrokeColorAttributeName

設(shè)置文字描邊顏色,需要和NSStrokeWidthAttributeName設(shè)置描邊寬度酵镜,這樣就能使文字空心.

NSStrokeWidthAttributeName 這個屬性所對應(yīng)的值是一個 NSNumber 對象(小數(shù))碉碉。該值改變筆畫寬度(相對于字體 size 的百分比),負值填充效果淮韭,正值中空效果垢粮,默認為 0,即不改變靠粪。正數(shù)只改變描邊寬度蜡吧。負數(shù)同時改變文字的描邊和填充寬度毫蚓。例如,對于常見的空心字昔善,這個值通常為 3.0元潘。

同時設(shè)置了空心的兩個屬性,并且 NSStrokeWidthAttributeName 屬性設(shè)置為整數(shù)君仆,文字前景色就無效果了

[objc]view plaincopy

NSMutableAttributedString*mutableAttributeStr5=?[[NSMutableAttributedStringalloc]init];

//?如果這個stroke的width正數(shù)的時候翩概,字體就中空了,那么前景色就設(shè)置無效了

NSAttributedString*string15=?[[NSAttributedStringalloc]initWithString:@"HELLO?"attributes:@{NSStrokeWidthAttributeName?:?@(5),NSStrokeColorAttributeName?:?[UIColoryellowColor],NSFontAttributeName?:?[UIFontboldSystemFontOfSize:30],NSForegroundColorAttributeName?:?[UIColorredColor]}];

NSAttributedString*string16=?[[NSAttributedStringalloc]initWithString:@"YUKD??"attributes:@{NSStrokeWidthAttributeName?:?@(0),NSStrokeColorAttributeName?:?[UIColorredColor],NSFontAttributeName?:?[UIFontboldSystemFontOfSize:30]}];

NSAttributedString*string17=?[[NSAttributedStringalloc]initWithString:@"GOOGLE"attributes:@{NSStrokeWidthAttributeName?:?@(-5),NSStrokeColorAttributeName?:?[UIColorgreenColor],NSFontAttributeName?:?[UIFontboldSystemFontOfSize:30],NSForegroundColorAttributeName?:?[UIColorlightGrayColor]}];

[mutableAttributeStr5appendAttributedString:string15];

[mutableAttributeStr5appendAttributedString:string16];

[mutableAttributeStr5appendAttributedString:string17];

self.label6.attributedText=?mutableAttributeStr5;

9.NSShadowAttributeName

設(shè)置文字陰影返咱,取值為NSShadow對象

[objc]view plaincopy

//?設(shè)置陰影

NSShadow*shadow?=?[[NSShadowalloc]init];

shadow.shadowBlurRadius=5.0f;//?模糊度

shadow.shadowColor=?[UIColorblueColor];

shadow.shadowOffset=?CGSizeMake(1,5);

NSAttributedString*string20=?[[NSAttributedStringalloc]initWithString:@"HELLO_HALY_璟"attributes:@{NSFontAttributeName?:?[UIFontboldSystemFontOfSize:30],NSForegroundColorAttributeName?:?[UIColorredColor],NSShadowAttributeName?:?shadow}];

self.label7.attributedText=?string20;

10.NSTextEffectAttributeNam

NSTextEffectAttributeName //設(shè)置文本特殊效果钥庇,取值為NSString類型,目前只有一個可用效果 NSTextEffectLetterpressStyle(凸版印刷效果)

[objc]view plaincopy

//?文本印刷洛姑,我也不知道是什么鬼

//?設(shè)置陰影

NSAttributedString*string21=?[[NSAttributedStringalloc]initWithString:@"HELLO_HALY_璟"attributes:@{NSFontAttributeName?:?[UIFontboldSystemFontOfSize:30],NSForegroundColorAttributeName?:?[UIColorredColor],NSShadowAttributeName?:?shadow,NSTextEffectAttributeName?:?NSTextEffectLetterpressStyle}];

self.label8.attributedText=?string21;

11.NSBaselineOffsetAttributeName

文字基線偏移,想要達到以下圖片的效果上沐,就要設(shè)置需要的文字偏移,正數(shù)上移楞艾,負數(shù)下

[objc]view plaincopy

//?設(shè)置文本的基線?負數(shù)向下??正數(shù)向上??0不變

UIFont*bigFont?=?[UIFontboldSystemFontOfSize:36];

UIFont*smallFont?=?[UIFontboldSystemFontOfSize:bigFont.pointSize/2];

CGFloat?capHeight?=?bigFont.pointSize-?smallFont.pointSize;

NSMutableAttributedString*attributeString6=?[[NSMutableAttributedStringalloc]init];

NSAttributedString*string22=?[[NSAttributedStringalloc]initWithString:@"¥"attributes:@{NSFontAttributeName?:?smallFont,NSForegroundColorAttributeName?:?[UIColorblackColor],NSBaselineOffsetAttributeName?:?@(0)}];

NSAttributedString*string23=?[[NSAttributedStringalloc]initWithString:@"9"attributes:@{NSFontAttributeName?:?bigFont,NSForegroundColorAttributeName?:?[UIColorblueColor],NSBaselineOffsetAttributeName?:?@(0)}];

NSAttributedString*string24=?[[NSAttributedStringalloc]initWithString:@".99"attributes:@{NSFontAttributeName?:?smallFont,NSForegroundColorAttributeName?:?[UIColorblackColor],NSBaselineOffsetAttributeName?:?@(0)}];

NSAttributedString*string28=?[[NSAttributedStringalloc]initWithString:@"????七夕大促銷????"attributes:@{NSFontAttributeName?:?[UIFontboldSystemFontOfSize:24],NSForegroundColorAttributeName?:?[UIColorblackColor],NSBaselineOffsetAttributeName?:?@(0),NSShadowAttributeName?:?shadow}];

NSAttributedString*string25=?[[NSAttributedStringalloc]initWithString:@"¥"attributes:@{NSFontAttributeName?:?smallFont,NSForegroundColorAttributeName?:?[UIColorblackColor],NSBaselineOffsetAttributeName?:?@(capHeight?-5)}];

NSAttributedString*string26=?[[NSAttributedStringalloc]initWithString:@"0"attributes:@{NSFontAttributeName?:?bigFont,NSForegroundColorAttributeName?:?[UIColorredColor],NSBaselineOffsetAttributeName?:?@(0)}];

NSAttributedString*string27=?[[NSAttributedStringalloc]initWithString:@".09"attributes:@{NSFontAttributeName?:?smallFont,NSForegroundColorAttributeName?:?[UIColorblackColor],NSBaselineOffsetAttributeName?:?@(capHeight?-5)}];

[attributeString6appendAttributedString:string22];

[attributeString6appendAttributedString:string23];

[attributeString6appendAttributedString:string24];

[attributeString6appendAttributedString:string28];

[attributeString6appendAttributedString:string25];

[attributeString6appendAttributedString:string26];

[attributeString6appendAttributedString:string27];

self.label9.attributedText=?attributeString6;

NSMutableAttributedString*attributeString7=?[[NSMutableAttributedStringalloc]init];

NSAttributedString*string29=?[[NSAttributedStringalloc]initWithString:@"Hello??"attributes:@{NSFontAttributeName?:?[UIFontboldSystemFontOfSize:40],NSForegroundColorAttributeName?:?[UIColorblackColor],NSBaselineOffsetAttributeName?:?@(0)}];

NSAttributedString*string30=?[[NSAttributedStringalloc]initWithString:@"??World????"attributes:@{NSFontAttributeName?:?[UIFontboldSystemFontOfSize:20],NSForegroundColorAttributeName?:?[UIColorblackColor],NSBaselineOffsetAttributeName?:?@(0)}];

NSAttributedString*string31=?[[NSAttributedStringalloc]initWithString:@"Hello??"attributes:@{NSFontAttributeName?:?[UIFontboldSystemFontOfSize:40],NSForegroundColorAttributeName?:?[UIColorblackColor],NSBaselineOffsetAttributeName?:?@(0)}];

NSAttributedString*string32=?[[NSAttributedStringalloc]initWithString:@"??World"attributes:@{NSFontAttributeName?:?[UIFontboldSystemFontOfSize:20],NSForegroundColorAttributeName?:?[UIColorblackColor],NSBaselineOffsetAttributeName?:?@(6)}];

[attributeString7appendAttributedString:string29];

[attributeString7appendAttributedString:string30];

[attributeString7appendAttributedString:string31];

[attributeString7appendAttributedString:string32];

self.label10.attributedText=?attributeString7;

12.NSObliquenessAttributeName

NSObliquenessAttributeName 設(shè)置字體傾斜度参咙,取值為 NSNumber(float),正值右傾硫眯,負值左傾

13.NSExpansionAttributeName

NSExpansionAttributeName 設(shè)置字體的橫向拉伸蕴侧,取值為NSNumber (float),正值拉伸 两入,負值壓縮

14.NSVerticalGlyphFormAttributeName

NSVerticalGlyphFormAttributeName 設(shè)置文字排版方向净宵,取值為NSNumber對象(整數(shù)),0表示橫排文本裹纳,1表示豎排文本 在ios中只支持0

[objc]view plaincopy

//????說明:NSVerticalGlyphFormAttributeName?設(shè)置文字排版方向择葡,取值為NSNumber對象(整數(shù)),0表示橫排文本剃氧,1表示豎排文本?在iOS中只支持0

//說明:NSObliquenessAttributeName?設(shè)置字體傾斜度敏储,取值為?NSNumber(float),正值右傾朋鞍,負值左傾

//????NSExpansionAttributeName

//????說明:NSExpansionAttributeName?設(shè)置字體的橫向拉伸已添,取值為NSNumber?(float),正值拉伸?滥酥,負值壓縮

NSMutableAttributedString*mutableAttriteStr?=?[[NSMutableAttributedStringalloc]init];

NSAttributedString*attributeStr1=?[[NSAttributedStringalloc]initWithString:@"HeHe_XiXi_mm"attributes:@{NSFontAttributeName?:[UIFontboldSystemFontOfSize:30],NSForegroundColorAttributeName?:?[UIColorredColor],NSShadowAttributeName?:?shadow,NSObliquenessAttributeName?:?@(1),NSExpansionAttributeName?:?@(1),NSVerticalGlyphFormAttributeName?:?@(1)}];

[mutableAttriteStrappendAttributedString:attributeStr1];

self.label1.attributedText=?mutableAttriteStr;

15.NSWritingDirectionAttributeName

文字書寫方向取值為以下組合為例 ?他是一個數(shù)組包含NSNumber 一般書寫就是L ---R

那么我們多個需求也就是從R --- L

The values of theNSNumberobjects should be0,1,2, or3, forLRE,RLE,LRO, orRLOrespectively, and combinations ofleftToRightandrightToLeftwithNSTextWritingDirectionEmbeddingorNSTextWritingDirectionOverride

[objc]view plaincopy

//?文字書寫方向

//????The?values?of?the?NSNumber?objects?should?be?0,?1,?2,?or?3,?for?LRE,?RLE,?LRO,?or?RLO?respectively,?and?combinations?of?leftToRight?and?rightToLeft?with?NSTextWritingDirectionEmbedding?or?NSTextWritingDirectionOverride,?as?shown?in?Table?1.

//????NSWritingDirectionLeftToRight?|?NSTextWritingDirectionEmbedding

//????NSWritingDirectionRightToLeft?|?NSTextWritingDirectionEmbedding

//????NSWritingDirectionLeftToRight?|?NSTextWritingDirectionOverride

//????NSWritingDirectionRightToLeft?|?NSTextWritingDirectionOverride

NSMutableAttributedString*attributuStr2=?[[NSMutableAttributedStringalloc]init];

NSAttributedString*string1=?[[NSAttributedStringalloc]initWithString:@"一切皆有可能"attributes:@{NSFontAttributeName?:?[UIFontboldSystemFontOfSize:24],NSForegroundColorAttributeName?:?[UIColorredColor],NSWritingDirectionAttributeName?:?@[@(3)]}];

[attributuStr2appendAttributedString:string1];

self.label.attributedText=?attributuStr2;

16.NSLinkAttributeName

這貨有點奇葩更舞,所以很多人用第三方例如YYLabel來做,這東西不能在UILabel和UITextField使用坎吻,只能用UITextView來進行缆蝉,實現(xiàn)他的代理,在代理方法里面進行URL跳轉(zhuǎn)

- (BOOL)textView:(UITextView*)textView shouldInteractWithURL:(NSURL*)URL inRange:(NSRange)characterRange

該方法返回YES就能打開URL,NO不做任何事情

注:

1.一定要實現(xiàn)UITextView的代理才能進行URL跳轉(zhuǎn)

2.textView的editable屬性修改為NO刊头,在編輯時不可點擊

[objc]view plaincopy

//????把?NSLinkAttributeName?屬性單獨列出來贝搁,是因為在?UILabel?和?UITextField?中是無法使用該屬性的。更準確點說是在UILabel?和?UITextField?中無法實現(xiàn)點擊鏈接啟動瀏覽器打開一個URL地址芽偏,因為在此過程中用到了一個代理函數(shù)雷逆。只能用在?UITextView?中

self.textView.delegate=self;

self.textView.scrollEnabled=NO;

self.textView.editable=NO;

self.textView.textContainer.lineFragmentPadding=0;

self.textView.textContainerInset=?UIEdgeInsetsMake(0,0,0,0);

NSString*str?=@"??跳轉(zhuǎn)到宓珂璟的博客";

NSMutableAttributedString*attrStr?=?[[NSMutableAttributedStringalloc]initWithString:str];

[attrStraddAttribute:NSLinkAttributeNamevalue:[NSURLURLWithString:@"http://blog.csdn.net/deft_mkjing"]range:[strrangeOfString:@"宓珂璟的博客"]];

[attrStraddAttribute:NSFontAttributeNamevalue:[UIFontboldSystemFontOfSize:30]range:NSMakeRange(0,?str.length)];

self.textView.attributedText=?attrStr;

17.NSTextAttachment

設(shè)置文本附件,取值為NSTextAttachment對象 常用于圖文混排

[objc]view plaincopy

NSString*words?=@"天才";

NSMutableAttributedString*strAtt?=?[[NSMutableAttributedStringalloc]initWithString:wordsattributes:@{NSFontAttributeName?:?[UIFontboldSystemFontOfSize:25]}];

NSTextAttachment*attatch?=?[[NSTextAttachmentalloc]initWithData:nilofType:nil];

attatch.bounds=?CGRectMake(0,0,40,30);

attatch.image=?[UIImageimageNamed:@"$DCZ2WLN9RWI6JF(Q`P_(NH.jpg"];

NSTextAttachment*attatch1=?[[NSTextAttachmentalloc]initWithData:nilofType:nil];

attatch1.bounds=?CGRectMake(0,0,50,30);

attatch1.image=?[UIImageimageNamed:@"%5T@J(4WKWSOX2~~PY0{4M0.jpg"];

NSAttributedString*string8=?[NSAttributedStringattributedStringWithAttachment:attatch];

NSAttributedString*string9=?[NSAttributedStringattributedStringWithAttachment:attatch1];

[strAttinsertAttributedString:string8atIndex:1];

[strAttinsertAttributedString:string9atIndex:0];

self.label3.attributedText=?strAtt;

基本的就介紹到這里了污尉,上面那些已經(jīng)足夠用了膀哲,而且可以任意組合出想要的圖文混排,

如果遇到特殊的需求或者不能滿足的被碗,那么就需要各位去試試往上的第三方富文本了

給個好用的大家可以試試點擊打開鏈接

本次Demo地址:Demo傳送門

Demo里面貌似沒有注釋掉多余的屬性列表某宪,大家要看的自行注釋一下

謝謝大家閱讀,記錄的同時希望能幫到大家H衿印P宋埂!


?ios知識點總結(jié)——富文本實現(xiàn)圖文混排

?[簡單] poi word2007簡單圖文混排

?圖文混排

?基于NGUI的表情圖文混排解決方案

?實現(xiàn)圖文混排ListView展示 ---- Android版

?iOS 富文本焚志、圖文混排

?用CSS實現(xiàn)HTML網(wǎng)頁圖文混排效果

?iOS圖文混排(需要在文本或者字符串中某些特定位置添加文本框)

?jQuery圖文混排tab選項卡

?小胖說事13--------NSTextAttachment富文本控件實現(xiàn)圖文混排

猜你在找

深度學(xué)習(xí)基礎(chǔ)與TensorFlow實踐

【在線峰會】前端開發(fā)重點難點技術(shù)剖析與創(chuàng)新實踐

【在線峰會】一天掌握物聯(lián)網(wǎng)全棧開發(fā)之道

【在線峰會】如何高質(zhì)高效的進行Android技術(shù)開發(fā)

IT(6)

ios(216)

人工智能(1)

服務(wù)器(7)

android 安全(9)

android 測試工具(12)

android崩潰(4)

c語言(1)

產(chǎn)品(15)

外貿(mào)(1)

IOS異常(2)

PS(1)

基礎(chǔ)算法+基礎(chǔ)知識總結(jié)(13)

設(shè)計模式(1)

html+js+jquery+css(72)

Job(19)

文章存檔

2017年06月(2)

2017年05月(4)

2017年04月(9)

2017年03月(16)

2017年02月(11)

展開

閱讀排行

RecyclerView setHasFixedSize(true); 的作用(14385)

Android自定義View進階 - 貝塞爾曲線(4479)

把Android手機變成遠程監(jiān)控攝像頭(3394)

GLIDE 的 一些用法(2971)

51個賺錢最好的方法(史上最全攻略)(2919)

TabLayout:另一種Tab的實現(xiàn)方式(2890)

eclipse轉(zhuǎn)android studio常遇到的問題(2011)

安卓7.1 新特性Shortcut(1795)

ViewCompat.animate 動畫實現(xiàn)方式(1780)

通過代碼 新增 和 “修改”NSLayoutConstraint(1625)

評論排行

GitHub上史上最全的iOS開源項目分類匯總(2)

Android 基于google Zxing實現(xiàn)二維碼衣迷、條形碼掃描,仿微信二維碼掃描效果(1)

HashMap 和 SparseArray比較(1)

Android各大網(wǎng)絡(luò)請求庫的比較及實戰(zhàn)(1)

Android性能優(yōu)化 view.setLayerType(View.LAYER_TYPE_NONE, null); 來優(yōu)化Property動畫(1)

drawLine DashPathEffect繪制虛線變成了實線(1)

瘋狂ios講義之自定義UI控件(1)

Android自定義View進階 - 貝塞爾曲線(1)

Android 向右滑動銷毀(finish)Activity, 隨著手勢的滑動而滑動的效果(1)

23個Facebook Paper中的設(shè)計細節(jié)(1)


* 如何選擇優(yōu)化器 optimizer

* 性能測試場景設(shè)計雜談

* 線程進階:多任務(wù)處理——Java 中的鎖

最新評論

Android NDK——使用Android Studio引用so庫酱酬,jar包及module并使用JNI的正確姿勢

qq_38354031: 我來頂一個

Android各大網(wǎng)絡(luò)請求庫的比較及實戰(zhàn)

孫華強: CSDN博友你好壶谒,我是孫華強,現(xiàn)將此篇博文收錄進“Android知識庫”膳沽。CSDN現(xiàn)在在做CSDN博...

Android性能優(yōu)化 view.setLayerType(View.LAYER_TYPE_NONE, null); 來優(yōu)化Property動畫

Boiling_Cola: 可以轉(zhuǎn)嗎汗菜?

GitHub上史上最全的iOS開源項目分類匯總

renzhengjigou: 仔細看了一遍,收獲很大挑社。

GitHub上史上最全的iOS開源項目分類匯總

renzhengjigou: 仔細看了一遍陨界,收獲很大。

HashMap 和 SparseArray比較

twoOrEleven: 但是SparseArray里有沒有能替代hashMap中的containsKey()的Api呢?

把Android手機變成遠程監(jiān)控攝像頭

qq_29964245: PC服務(wù)器端痛阻,改怎么弄博主

HTML 如何讓元素可見性 設(shè)置為 不可見

王虹蕓: CSDN的博客可見性可以設(shè)置嗎菌瘪?

Android自定義View進階 - 貝塞爾曲線

Burgessb: demo在哪

drawLine DashPathEffect繪制虛線變成了實線

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市录平,隨后出現(xiàn)的幾起案子麻车,更是在濱河造成了極大的恐慌缀皱,老刑警劉巖斗这,帶你破解...
    沈念sama閱讀 218,755評論 6 507
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場離奇詭異啤斗,居然都是意外死亡表箭,警方通過查閱死者的電腦和手機,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,305評論 3 395
  • 文/潘曉璐 我一進店門钮莲,熙熙樓的掌柜王于貴愁眉苦臉地迎上來免钻,“玉大人彼水,你說我怎么就攤上這事〖颍” “怎么了凤覆?”我有些...
    開封第一講書人閱讀 165,138評論 0 355
  • 文/不壞的土叔 我叫張陵,是天一觀的道長拆魏。 經(jīng)常有香客問我盯桦,道長,這世上最難降的妖魔是什么渤刃? 我笑而不...
    開封第一講書人閱讀 58,791評論 1 295
  • 正文 為了忘掉前任拥峦,我火速辦了婚禮,結(jié)果婚禮上卖子,老公的妹妹穿的比我還像新娘略号。我一直安慰自己,他們只是感情好洋闽,可當(dāng)我...
    茶點故事閱讀 67,794評論 6 392
  • 文/花漫 我一把揭開白布玄柠。 她就那樣靜靜地躺著,像睡著了一般诫舅。 火紅的嫁衣襯著肌膚如雪随闪。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 51,631評論 1 305
  • 那天骚勘,我揣著相機與錄音铐伴,去河邊找鬼。 笑死俏讹,一個胖子當(dāng)著我的面吹牛当宴,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播泽疆,決...
    沈念sama閱讀 40,362評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼户矢,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了殉疼?” 一聲冷哼從身側(cè)響起梯浪,我...
    開封第一講書人閱讀 39,264評論 0 276
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎瓢娜,沒想到半個月后挂洛,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 45,724評論 1 315
  • 正文 獨居荒郊野嶺守林人離奇死亡眠砾,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 37,900評論 3 336
  • 正文 我和宋清朗相戀三年虏劲,在試婚紗的時候發(fā)現(xiàn)自己被綠了。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點故事閱讀 40,040評論 1 350
  • 序言:一個原本活蹦亂跳的男人離奇死亡柒巫,死狀恐怖励堡,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情堡掏,我是刑警寧澤应结,帶...
    沈念sama閱讀 35,742評論 5 346
  • 正文 年R本政府宣布,位于F島的核電站泉唁,受9級特大地震影響摊趾,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜游两,卻給世界環(huán)境...
    茶點故事閱讀 41,364評論 3 330
  • 文/蒙蒙 一砾层、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧贱案,春花似錦肛炮、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,944評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至瘩燥,卻和暖如春秕重,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背厉膀。 一陣腳步聲響...
    開封第一講書人閱讀 33,060評論 1 270
  • 我被黑心中介騙來泰國打工溶耘, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留,地道東北人服鹅。 一個月前我還...
    沈念sama閱讀 48,247評論 3 371
  • 正文 我出身青樓凳兵,卻偏偏與公主長得像,于是被迫代替她去往敵國和親企软。 傳聞我的和親對象是個殘疾皇子庐扫,可洞房花燭夜當(dāng)晚...
    茶點故事閱讀 44,979評論 2 355

推薦閱讀更多精彩內(nèi)容