iOS UIlabel添加照片 UITextView 文字/照片點(diǎn)擊事件
根據(jù)產(chǎn)品需求需要文本的行末添加一個(gè)圖標(biāo)或者圖片赚导,并可實(shí)現(xiàn)部分文字的點(diǎn)擊和照片的點(diǎn)擊事件
效果圖
方案1:添加的圖片是iconfont圖標(biāo)裳瘪。iconfont,從字面上就能理解它就是字體筑凫,讓開發(fā)者像使用字體一樣使用圖標(biāo)。
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(50, 160, 280, 40)];
[self.view addSubview:label];
label.font = [UIFont fontWithName:@"iconfont" size:15];//設(shè)置label的字體
label.text = @"這是用label顯示的iconfont \U0000e60c";
如果不顯示侣监,請檢查一下iconfont的資源文件是否在工程目錄下。
檢查資源文件
plist文件配置
方案2:通過 NSMutableAttributedString 在文本末尾添加一個(gè)照片
UILabel *titleLabel = [[UILabel alloc]init];
titleLabel.frame = CGRectMake(10, 300, 100, 200);
titleLabel.backgroundColor = [UIColor redColor];
[self.view addSubview:titleLabel];
NSTextAttachment *attachment = [[NSTextAttachment alloc]init];
attachment.image = [UIImage imageNamed:@"eys"];
NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc]initWithString:@"我的后面跟著一個(gè)照片"];
[attributedString addAttribute:NSForegroundColorAttributeName value:[UIColor blueColor] range:NSMakeRange(0, 10)];
[attributedString addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:20] range:NSMakeRange(0, 10)];
NSAttributedString *attachmentString = [NSAttributedString attributedStringWithAttachment:attachment];
[attributedString insertAttributedString:attachmentString atIndex:10];
titleLabel.attributedText = attributedString;
titleLabel.numberOfLines = 0;
[titleLabel sizeToFit];
UITextView 文字/照片點(diǎn)擊事件
NSMutableAttributedString *attributeString2 = [[NSMutableAttributedString alloc]initWithString:@"哈哈哈哈哈哈哈哈,你還沒有中獎(jiǎng)浇雹,趕快去參加活動(dòng)"];
[attributeString2 addAttribute:NSLinkAttributeName value:@"click://" range:[[attributeString2 string]rangeOfString:@"參加活動(dòng)"]];
NSTextAttachment *attachment2 = [[NSTextAttachment alloc]init];
attachment2.image = [UIImage imageNamed:@"eys"];
attachment2.bounds = CGRectMake(0, 0, 15, 15);
NSAttributedString *attachmentString2 = [NSAttributedString attributedStringWithAttachment:attachment2];
NSMutableAttributedString *imgAttribute = [[NSMutableAttributedString alloc]initWithAttributedString:attachmentString2];
[imgAttribute addAttribute:NSLinkAttributeName value:@"IMGURL://" range:NSMakeRange(0, imgAttribute.length)];
[attributeString2 insertAttributedString:imgAttribute atIndex:[attributeString2 string].length];
UITextView *textView = [[UITextView alloc]initWithFrame:CGRectMake(20,400 , 200, 220)];
[self.view addSubview:textView];
textView.attributedText = attributeString2;
textView.linkTextAttributes = @{NSForegroundColorAttributeName:[UIColor blueColor],NSUnderlineColorAttributeName:[UIColor grayColor]};
textView.font = [UIFont systemFontOfSize:18];
textView.textAlignment = NSTextAlignmentLeft;
textView.backgroundColor = [UIColor brownColor];
textView.delegate = self;
textView.editable = NO;
textView.scrollEnabled = NO;
[self.view addSubview:textView];
self.curentView = textView;
UItextView的代理方法
-(BOOL)textView:(UITextView *)textView shouldInteractWithURL:(NSURL *)URL inRange:(NSRange)characterRange interaction:(UITextItemInteraction)interaction {
NSLog(@"打印獲取到的url的字符串為----%@",[URL scheme]);
if ([[URL scheme] isEqualToString:@"click"]) {
NSLog(@"執(zhí)行了文字的點(diǎn)擊事件");
UIAlertView *alertView = [[UIAlertView alloc]initWithTitle:@"提示" message:@"文字被點(diǎn)擊了" delegate:nil cancelButtonTitle:nil otherButtonTitles:@"我知道了", nil];
[alertView show];
return NO;
}else if ([[URL scheme] isEqualToString:@"IMGURL"]) {
UIAlertView *alertView = [[UIAlertView alloc]initWithTitle:@"提示" message:@"照片被點(diǎn)擊了" delegate:nil cancelButtonTitle:nil otherButtonTitles:@"我知道了", nil];
[alertView show];
NSLog(@"執(zhí)行了點(diǎn)擊末尾的照片");
}
return YES;
}