當前位置:股票大全官網 - 財經新聞 - YYLabel富文本顯示

YYLabel富文本顯示

YYLabel實現方法

//1.只需顯示標簽。

YY label * label =【YY label new】;

label . frame = CGRectMake(100,50,100,25);

label . font =【ui font system font of size:14.0 f】;

label . text color =【uicoloorangecolor】;

label . text alignment = NSTextAlignmentCenter;

label . line break mode = 0;

label . number of lines = nslinebreakbywordwarping;

label . text = @“YYTextDemo Test“;

//?【self . view addSubview:label】;

//2.屬性字符串簡單而實用。

Nmutable attributed string * text =【【Nmutable attributed string galloc】initwithString:@“上海第九城市信息技術有限公司“】;

text . font =【uifont bold systemfont of size:13.0 f】;

text . color =【UIColorblueColor】;

【textset color:【ui color red color】range:NSMakeRange(0,4)】;

//?將點擊事件添加到標簽中。

【textsettexthighlight range:NSMakeRange(0,4)

顏色:【UIColor橙色】

背景色:【UIColorwhiteColor】

tapaction:^(uiview * container view,NSAttributedString *text,NSRange range,cgrect rect ){

NSLog (@“輕按文本範圍:。。。“);

}];

YY label * attributed label =【YY label new】;

attributed label . frame = CGRectMake(100,100,160,25);

attributed label . attributed text = text;

attributed label . user interaction enabled = YES;

attributed label . background color =【ui color purple color】;

//?【self . view addSubview:attributed label】;

//3.圖文混合布局模式

NSMutableAttributedString * text image =【NSMutableAttributedString new】;

ui font * font =【ui font system font of size:14.0 f】;

inti = 2;

開關(I ){

案例0:

{

//?模式1

2006年諾貝爾文學獎授予了土耳其作家奧爾罕·帕慕克。當許多人感到驚訝時,我高興了壹陣子,因為我早在兩年前就知道這個作家了。

【textImageappendAttributedString:【【NSAttributedString alloc】init withstring:title attributes:nil】】;

ui image * image =【ui image image named:@“babilogo“】;

image =【ui image imageWithCGImage:image。CG image scale:5 orientation:UIImageOrientationUp】;

NSMutableAttributedString * attachText =【NSMutableAttributedStringattachmentStringWithContent:image content mode:UIViewContentModeCenter attachmentSize:image . size alignToFont:font alignment:yytextverticalaligmentcenter】;

【text image appendAttributedString:attach text】;

【textimageappendattributestring:【【nsattributestring alloc】init withstring:@“我得到了都柏林文學,我的名字是紅色。。。“屬性:nil】】;

打破;

}

案例1:

{

NSString *title=@“文本和UIView混合:“;

【textImageappendAttributedString:【【nsattributedstring alloc】init withstring:title attributes:nil】】;

ui switch * switcher =【ui switch new】;

switcher . frame = CGRectMake(0,0,50,50);

【switcher sizeToFit】;

NSMutableAttributedString * attachText =【NSMutableAttributedString attachmentStringWithContent:switcher content mode:UIViewContentModeCenter attachmentSize:switcher . frame . size Ali gnto font:font alignment:YYTextVerticalAlignmentCenter】;

【textImageappendAttributedString:attach text】;

【textImageappendAttributedString:【【NSAttributedString alloc】init withstring:@“\ n“attributes:nil】】;

打破;

}

案例二:

{

2006年諾貝爾文學獎授予了土耳其作家奧爾罕·帕慕克。當許多人感到驚訝時,我高興了壹陣子,因為我早在兩年前就知道這個作家了。

【text image appendAttributedString:【【NSAttributedString alloc】init withstring:title attributes:nil】】;

ui imageView * imageView =【ui imageView new】;

imageview . frame = CGRectMake(0,0,20,20);

【imageViewsetImage:【ui image image named:@“babilogo“】】;

imageview . content mode = uiviewcontentmodescalaspectfill;

imageview . user interaction enabled = YES;

uitapgesturerecognizer * tap =【【uitapgesturerecognizer alloc]initwithactionblock:^(id_nonnullsender】{

NSLog(@“image action“);

}];

【imageViewaddGestureRecognizer:tap】;

NSMutableAttributedString * attachText =【NSMutableAttributedStringattachmentStringWithContent:imageView content mode:UIViewContentModeCenter attachmentSize:imageView . frame . size al ignto font:font alignment:YYTextVerticalAlignmentCenter】;

【textImageappendAttributedString:attach text】;

NSString*subTitle=@“憑借《我的名字是紅色》獲得都柏林文學獎,2006年諾貝爾文學獎被授予土耳其作家奧爾罕·帕慕克。當許多人感到驚訝時,我高興了壹陣子,因為我早在兩年前就知道這個作家了。

【textImageappendAttributedString:【【NSAttributedString alloc】init withstring:subTitle attributes:nil】】;

NSMutableParagraphStyle * paragraphStyle =【【NSMutableParagraphStylealloc】init】;

【paragraphStylesetLineSpacing:16】;//調整行距

【text image add attribute:nsparagraphstyle attributename value:paragraphStyle range:NSMakeRange(0,【標題長度】+【字幕長度】)】;

【text image settexthighlight range:NSMakeRange(0,8)

顏色:【UIColor橙色】

背景顏色:【UIColor白色顏色】

tapaction:^(uiview * container view,NSAttributedString *text,NSRange range,cgrect rect ){

NSLog (@“輕按文本範圍:。。。“);

}];

打破;

}

默認值:

打破;

}

textImage.font = font

YY label * text image label =【YY label new】;

text image label . user interaction enabled = YES;

textimagelabel . number of lines = 0;

textimagelabel . frame = CGRectMake(0,0,320,400);

text image label . attributed text = text image;

//?【self . view add subview:text image label】;