YYKit框架學(xué)習(xí)之YYLabel

一、YYLabel

強(qiáng)大的富文本顯示功能,可根據(jù)文字的range隨意添加點(diǎn)擊事件

1 自動(dòng)換行

YYLabel? *yyLabel = [YYLabel new];

yyLabel.numberOfLines = 0;

創(chuàng)建容器

YYTextContainer *titleContarer = [YYTextContainer new];

限制寬度

titleContarer.size = CGSizeMake([UIScreen mainScreen].bounds.size.width-40,CGFLOAT_MAX);

設(shè)置富文本

NSMutableAttributedString? *resultAttr = [self getAttr:title];

根據(jù)容器和文本創(chuàng)建布局對(duì)象

YYTextLayout *titleLayout = [YYTextLayout layoutWithContainer:titleContarer text:resultAttr];

得到文本高度

CGFloat titleLabelHeight = titleLayout.textBoundingSize.height;

設(shè)置frame

yyLabel.frame = CGRectMake(20,84,[UIScreen mainScreen].bounds.size.width-40,titleLabelHeight);

2 各種格式設(shè)置

對(duì)齊方式 這里是 兩邊對(duì)齊

resultAttr.alignment = NSTextAlignmentCenter;

設(shè)置行間距

resultAttr.lineSpacing = 3;

resultAttr.font = [UIFont systemFontOfSize:20];

{

NSRange range =? [attributedString rangeOfString:@"it was the worst of times"];

[resultAttr setFont:[UIFont boldSystemFontOfSize:30] range:range];

}

描邊

{

NSRange range =[attributedString rangeOfString:@"it was the age of wisdom"];

//文字描邊(空心字)默認(rèn)黑色,必須設(shè)置width

[resultAttr setStrokeColor:[UIColor orangeColor] range:range];

[resultAttr setStrokeWidth:@(2) range:range];

}

劃線(xiàn)

{

NSRange range = [attributedString rangeOfString:@"it was the age of foolishness, it was the season of light" options:NSCaseInsensitiveSearch];

YYTextDecoration *decoration = [YYTextDecoration decorationWithStyle:YYTextLineStyleSingle

width:@(1)

color:[UIColor blueColor]];

//刪除樣式

[resultAttr setTextStrikethrough:decoration range:range];

//下劃線(xiàn)

[resultAttr setTextUnderline:decoration range:range];

}

設(shè)置邊框

{

NSRange range = [attributedString rangeOfString:@"這是最好的時(shí)代,這是最壞的時(shí)代" options:NSCaseInsensitiveSearch];

//邊框

YYTextBorder *border = [YYTextBorder new];

border.strokeColor = [UIColor redColor];

border.strokeWidth = 4;

border.lineStyle = YYTextLineStylePatternDashDotDot;

border.cornerRadius = 1;

border.insets = UIEdgeInsetsMake(0, -2, 0, -2);

[resultAttr setTextBorder:border range:range];

}

設(shè)置陰影

{

NSRange range = [attributedString rangeOfString:@"這是智慧的時(shí)代,這是愚蠢的時(shí)代" options:NSCaseInsensitiveSearch];

//陰影

NSShadow *shadow = [[NSShadow alloc] init];

[shadow setShadowColor:[UIColor redColor]];

[shadow setShadowBlurRadius:1.0];

[shadow setShadowOffset:CGSizeMake(2, 2)];

[resultAttr setShadow:shadow range:range];

}

3 高亮顯示文本 點(diǎn)擊交互事件

{

NSRange range = [attributedString rangeOfString:@"這是希望之春,這是失望之冬" options:NSCaseInsensitiveSearch];

YYTextBorder *border = [YYTextBorder new];

border.cornerRadius = 50;

border.insets = UIEdgeInsetsMake(0, -10, 0, -10);

border.strokeWidth = 0.5;

border.strokeColor = [UIColor yellowColor];

border.lineStyle = YYTextLineStyleSingle;

[resultAttr setTextBorder:border range:range];

[resultAttr setTextBackgroundBorder:border range:range];

[resultAttr setColor:[UIColor greenColor] range:range];

YYTextBorder *highlightBorder = border.copy;

highlightBorder.strokeWidth = 0;

highlightBorder.strokeColor =? [UIColor purpleColor];

highlightBorder.fillColor =? [UIColor purpleColor];

YYTextHighlight *highlight = [YYTextHighlight new];

[highlight setColor:[UIColor whiteColor]];

[highlight setBackgroundBorder:highlightBorder ];

highlight.tapAction = ^(UIView *containerView, NSAttributedString *text, NSRange range, CGRect rect) {

[self alertShow:[NSString stringWithFormat:@"Tap: %@",[text.string substringWithRange:range]]];

};

[resultAttr setTextHighlight:highlight range:range];

// 點(diǎn)擊復(fù)制

[resultAttr setTextHighlightRange:[attributedString rangeOfString:@"450351763"]

color:[UIColor greenColor]

backgroundColor:[UIColor whiteColor]

tapAction:^(UIView *containerView, NSAttributedString *text, NSRange range, CGRect rect){

UIPasteboard *pboard = [UIPasteboard generalPasteboard];

pboard.string = @"450351763";

[self alertShow:@"復(fù)制成功"];

}];

}

4 圖文混排 支持各種格式包括gif

{

for (int i = 1; i<5; i++) {

NSString *path;

if(i == 4){

path = [[NSBundle mainBundle] pathForScaledResource:[NSString stringWithFormat:@"%d",i] ofType:@"gif"];

}else{

path = [[NSBundle mainBundle] pathForScaledResource:[NSString stringWithFormat:@"%d",i] ofType:@"jpg"];

}

NSData *data = [NSData dataWithContentsOfFile:path];

//修改表情大小

YYImage *image = [YYImage imageWithData:data scale:3];

image.preloadAllAnimatedImageFrames = YES;

YYAnimatedImageView *imageView = [[YYAnimatedImageView alloc] initWithImage:image];

NSMutableAttributedString *attachText = [NSMutableAttributedString attachmentStringWithContent:imageView contentMode:UIViewContentModeCenter attachmentSize:imageView.size alignToFont:[UIFont systemFontOfSize:18] alignment:YYTextVerticalAlignmentCenter];

[resultAttr appendAttributedString:attachText];

}

}

YYkit推薦pod安裝方式(我使用的是 ?pod 'YYKit', '~> 1.0.9' 最新版本)

以下為全部代碼貼到項(xiàng)目中可以直接運(yùn)行查看效果

@interfaceViewController2()

@end

@implementationViewController2

-?(void)viewDidLoad?{

[superviewDidLoad];

self.view.backgroundColor=?[UIColorcolorWithRed:0.3green:0.5blue:0.8alpha:1];

[self showYYLabel];

}

-(void)showYYLabel{

NSString*title?=@"It?was?the?best?of?times,?it?was?the?worst?of?times,?it?was?the?age?of?wisdom,?it?was?the?age?of?foolishness,?it?was?the?season?of?light,?it?was?the?season?of?darkness,?it?was?the?spring?of?hope,?it?was?the?winter?of?despair,?we?had?everything?before?us,?we?had?nothing?before?us.?We?were?all?going?direct?to?heaven,?we?were?all?going?direct?the?other?way.\n這是最好的時(shí)代,這是最壞的時(shí)代;這是智慧的時(shí)代,這是愚蠢的時(shí)代;這是信仰的時(shí)期,這是懷疑的時(shí)期;這是光明的季節(jié),這是黑暗的季節(jié);這是希望之春,這是失望之冬;人們面前有著各樣事物,人們面前一無(wú)所有;人們正在直登天堂,人們正在直下地獄。\n點(diǎn)擊復(fù)制QQ450351763進(jìn)行在線(xiàn)咨詢(xún)";

YYLabel??*yyLabel?=?[YYLabelnew];

yyLabel.backgroundColor=?[UIColorcolorWithRed:0.3green:0.4blue:0.1alpha:1];

//異步顯示

yyLabel.displaysAsynchronously=YES;

yyLabel.numberOfLines=0;

//創(chuàng)建容器

YYTextContainer*titleContarer?=?[YYTextContainernew];

//限制寬度

titleContarer.size=?CGSizeMake([UIScreenmainScreen].bounds.size.width-40,CGFLOAT_MAX);

//設(shè)置富文本

NSMutableAttributedString??*resultAttr?=?[selfgetAttr:title];

//根據(jù)容器和文本創(chuàng)建布局對(duì)象

YYTextLayout*titleLayout?=?[YYTextLayoutlayoutWithContainer:titleContarertext:resultAttr];

//得到文本高度

CGFloat?titleLabelHeight?=?titleLayout.textBoundingSize.height;

//設(shè)置frame

yyLabel.frame=?CGRectMake(20,84,[UIScreenmainScreen].bounds.size.width-40,titleLabelHeight);

yyLabel.attributedText=?titleAttr;

[self.viewaddSubview:yyLabel];

}

-?(NSMutableAttributedString*)getAttr:(NSString*)attributedString?{

NSMutableAttributedString*resultAttr?=?[[NSMutableAttributedStringalloc]initWithString:attributedString];

//????一、?格式設(shè)置

//對(duì)齊方式?這里是?兩邊對(duì)齊

resultAttr.alignment=?NSTextAlignmentCenter;

//設(shè)置行間距

resultAttr.lineSpacing=3;

resultAttr.font=?[UIFontsystemFontOfSize:20];

{

NSRange?range?=??[attributedStringrangeOfString:@"it?was?the?worst?of?times"];

[resultAttrsetFont:[UIFontboldSystemFontOfSize:30]range:range];

}

//描邊

{

NSRange?range?=[attributedStringrangeOfString:@"it?was?the?age?of?wisdom"];

//文字描邊(空心字)默認(rèn)黑色,必須設(shè)置width

[resultAttrsetStrokeColor:[UIColororangeColor]range:range];

[resultAttrsetStrokeWidth:@(2)range:range];

}

//劃線(xiàn)

{

NSRange?range?=?[attributedStringrangeOfString:@"it?was?the?age?of?foolishness,?it?was?the?season?of?light"options:NSCaseInsensitiveSearch];

YYTextDecoration*decoration?=?[YYTextDecorationdecorationWithStyle:YYTextLineStyleSingle

width:@(1)

color:[UIColorblueColor]];

//刪除樣式

[resultAttrsetTextStrikethrough:decorationrange:range];

//下劃線(xiàn)

[resultAttrsetTextUnderline:decorationrange:range];

}

//設(shè)置邊框

{

NSRange?range?=?[attributedStringrangeOfString:@"這是最好的時(shí)代,這是最壞的時(shí)代"options:NSCaseInsensitiveSearch];

//邊框

YYTextBorder*border?=?[YYTextBordernew];

border.strokeColor=?[UIColorredColor];

border.strokeWidth=4;

border.lineStyle=?YYTextLineStylePatternDashDotDot;

border.cornerRadius=1;

border.insets=?UIEdgeInsetsMake(0,?-2,0,?-2);

[resultAttrsetTextBorder:borderrange:range];

}

//設(shè)置陰影

{

NSRange?range?=?[attributedStringrangeOfString:@"這是智慧的時(shí)代,這是愚蠢的時(shí)代"options:NSCaseInsensitiveSearch];

//陰影

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

[shadowsetShadowColor:[UIColorredColor]];

[shadowsetShadowBlurRadius:1.0];

[shadowsetShadowOffset:CGSizeMake(2,2)];

[resultAttrsetShadow:shadowrange:range];

}

//高亮顯示文本

{

NSRange?range?=?[attributedStringrangeOfString:@"這是希望之春,這是失望之冬"options:NSCaseInsensitiveSearch];

YYTextBorder*border?=?[YYTextBordernew];

border.cornerRadius=50;

border.insets=?UIEdgeInsetsMake(0,?-10,0,?-10);

border.strokeWidth=0.5;

border.strokeColor=?[UIColoryellowColor];

border.lineStyle=?YYTextLineStyleSingle;

[resultAttrsetTextBorder:borderrange:range];

[resultAttrsetTextBackgroundBorder:borderrange:range];

[resultAttrsetColor:[UIColorgreenColor]range:range];

YYTextBorder*highlightBorder?=?border.copy;

highlightBorder.strokeWidth=0;

highlightBorder.strokeColor=??[UIColorpurpleColor];

highlightBorder.fillColor=??[UIColorpurpleColor];

YYTextHighlight*highlight?=?[YYTextHighlightnew];

[highlightsetColor:[UIColorwhiteColor]];

[highlightsetBackgroundBorder:highlightBorder?];

highlight.tapAction=?^(UIView*containerView,NSAttributedString*text,?NSRange?range,?CGRect?rect)?{

[selfalertShow:[NSStringstringWithFormat:@"Tap:?%@",[text.stringsubstringWithRange:range]]];

};

[resultAttrsetTextHighlight:highlightrange:range];

//?點(diǎn)擊復(fù)制

[resultAttrsetTextHighlightRange:[attributedStringrangeOfString:@"450351763"]

color:[UIColorgreenColor]

backgroundColor:[UIColorwhiteColor]

tapAction:^(UIView*containerView,NSAttributedString*text,?NSRange?range,?CGRect?rect){

UIPasteboard*pboard?=?[UIPasteboardgeneralPasteboard];

pboard.string=@"450351763";

[selfalertShow:@"復(fù)制成功"];

}];

}

//????圖文混排

{

for(inti?=1;?i<5;?i++)?{

NSString*path;

if(i?==4){

path?=?[[NSBundlemainBundle]pathForScaledResource:[NSStringstringWithFormat:@"%d",i]ofType:@"gif"];

}else{

path?=?[[NSBundlemainBundle]pathForScaledResource:[NSStringstringWithFormat:@"%d",i]ofType:@"jpg"];

}

NSData*data?=?[NSDatadataWithContentsOfFile:path];

//修改表情大小

YYImage*image?=?[YYImageimageWithData:datascale:3];

image.preloadAllAnimatedImageFrames=YES;

YYAnimatedImageView*imageView?=?[[YYAnimatedImageViewalloc]initWithImage:image];

NSMutableAttributedString*attachText?=?[NSMutableAttributedStringattachmentStringWithContent:imageViewcontentMode:UIViewContentModeCenterattachmentSize:imageView.sizealignToFont:[UIFontsystemFontOfSize:18]alignment:YYTextVerticalAlignmentCenter];

[resultAttrappendAttributedString:attachText];

}

}

returnresultAttr;

}

-(void)alertShow:(NSString*)str{

UIAlertController*vc?=??[UIAlertControlleralertControllerWithTitle:nilmessage:strpreferredStyle:UIAlertControllerStyleActionSheet];

[self.navigationControllerpresentViewController:vcanimated:YEScompletion:^{

dispatch_after(dispatch_time(DISPATCH_TIME_NOW,?(int64_t)(0.5*?NSEC_PER_SEC)),?dispatch_get_main_queue(),?^{

[vcdismissViewControllerAnimated:YEScompletion:^{

}];

});

}];

}

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

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

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