YYLabel實現(xiàn)方式
//1.簡單顯示label
YYLabel *label = [YYLabel new];
label.frame = CGRectMake(100,50,100,25);
label.font = [UIFont systemFontOfSize:14.0f];
label.textColor = [UIColororangeColor];
label.textAlignment=NSTextAlignmentCenter;
label.lineBreakMode=0;
label.numberOfLines = NSLineBreakByWordWrapping;
label.text=@"YYTextDemo Test";
//??? [self.view addSubview:label];
//2.屬性字符串 簡單實用
NSMutableAttributedString*text= [[NSMutableAttributedStringalloc] initWithString:@"上海市第九城市信息技術有限公司"];
text.font = [UIFontboldSystemFontOfSize:13.0f];
text.color = [UIColorblueColor];
[textsetColor:[UIColor redColor]range:NSMakeRange(0,4)];
//??? 給你的label添加點擊事件
[textsetTextHighlightRange:NSMakeRange(0,4)
color:[UIColor orangeColor]
backgroundColor:[UIColorwhiteColor]
tapAction:^(UIView *containerView, NSAttributedString *text, NSRange range, CGRect rect){
NSLog(@"tap text range:...");
}];
YYLabel *attributedLabel = [YYLabel new];
attributedLabel.frame = CGRectMake(100,100,160,25);
attributedLabel.attributedText = text;
attributedLabel.userInteractionEnabled=YES;
attributedLabel.backgroundColor = [UIColor purpleColor];
//??? [self.view addSubview:attributedLabel];
//3.圖文混排模式
NSMutableAttributedString *textImage = [NSMutableAttributedString new];
UIFont *font= [UIFont systemFontOfSize:14.0f];
inti =2;
switch(i) {
case0:
{
//??? 方式一
NSString*title =@"2006年的諾貝爾文學獎頒給了土耳其作家奧爾罕.帕慕克枚荣。在很多人都很意外的時候滞磺,我心頭卻感到一陣開心桥温,因為早在兩年前角雷,我就知道了這個作家";
[textImageappendAttributedString:[[NSAttributedString alloc] initWithString:title attributes:nil]];
UIImage *image = [UIImage imageNamed:@"babilogo"];
image= [UIImage imageWithCGImage:image.CGImage scale:5orientation:UIImageOrientationUp];
NSMutableAttributedString*attachText= [NSMutableAttributedStringattachmentStringWithContent:image contentMode:UIViewContentModeCenter attachmentSize:image.size alignToFont:font alignment:YYTextVerticalAlignmentCenter];
[textImage appendAttributedString:attachText];
[textImageappendAttributedString:[[NSAttributedString alloc] initWithString:@"憑借《我的名字叫紅》獲得了都柏林文學..."attributes:nil]];
break;
}
case1:
{
NSString *title=@"text 與 UIView混排:";
[textImageappendAttributedString:[[NSAttributedStringalloc] initWithString:title attributes:nil]];
UISwitch *switcher = [UISwitch new];
switcher.frame= CGRectMake(0,0,50,50);
[switcher sizeToFit];
NSMutableAttributedString*attachText = [NSMutableAttributedString attachmentStringWithContent:switcher contentMode:UIViewContentModeCenter attachmentSize:switcher.frame.size alignToFont:font alignment:YYTextVerticalAlignmentCenter];
[textImageappendAttributedString:attachText];
[textImageappendAttributedString:[[NSAttributedString alloc] initWithString:@"\n"attributes:nil]];
break;
}
case2:
{
NSString*title =@"2006年的諾貝爾文學獎頒給了土耳其作家奧爾罕.帕慕克。在很多人都很意外的時候,我心頭卻感到一陣開心,因為早在兩年前,我就知道了這個作家";
[textImage appendAttributedString:[[NSAttributedString alloc] initWithString:title attributes:nil]];
UIImageView *imageView = [UIImageView new];
imageView.frame= CGRectMake(0,0,20,20);
[imageViewsetImage:[UIImage imageNamed:@"babilogo"]];
imageView.contentMode =UIViewContentModeScaleAspectFill;
imageView.userInteractionEnabled=YES;
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithActionBlock:^(id_Nonnullsender) {
NSLog(@"ImageAction");
}];
[imageViewaddGestureRecognizer:tap];
NSMutableAttributedString*attachText = [NSMutableAttributedStringattachmentStringWithContent:imageView contentMode:UIViewContentModeCenter attachmentSize:imageView.frame.size alignToFont:font alignment:YYTextVerticalAlignmentCenter];
[textImageappendAttributedString:attachText];
NSString*subTitle=@"憑借《我的名字叫紅》獲得了都柏林文學,2006年的諾貝爾文學獎頒給了土耳其作家奧爾罕.帕慕克枉氮。在很多人都很意外的時候,我心頭卻感到一陣開心暖庄,因為早在兩年前嘲恍,我就知道了這個作家";
[textImageappendAttributedString:[[NSAttributedString alloc] initWithString:subTitle attributes:nil]];
NSMutableParagraphStyle*paragraphStyle= [[NSMutableParagraphStylealloc] init];
[paragraphStylesetLineSpacing:16];//調整行間距
[textImage addAttribute:NSParagraphStyleAttributeName value:paragraphStyle range:NSMakeRange(0, [title length]+[subTitle length])];
[textImage setTextHighlightRange:NSMakeRange(0,8)
color:[UIColor orangeColor]
backgroundColor:[UIColor whiteColor]
tapAction:^(UIView *containerView, NSAttributedString *text, NSRange range, CGRect rect){
NSLog(@"tap text range:...");
}];
break;
}
default:
break;
}
textImage.font = font;
YYLabel *textImageLabel = [YYLabel new];
textImageLabel.userInteractionEnabled =YES;
textImageLabel.numberOfLines =0;
textImageLabel.frame = CGRectMake(0,0,320,400);
textImageLabel.attributedText = textImage;
//??? [self.view addSubview:textImageLabel];