我有一个NSTextAttachment
,其中的图片位于UILabel
,我希望在点击此附件时执行自定义行为。
UITextViewDelegate
提供了一个方便的方法
textView:shouldInteractWithTextAttachment:inRange:
但只有在我使用UITextView
时才能使用它。
如果我使用UILabel
?
谢谢!
答案 0 :(得分:1)
文本附件成为模型的一部分,并被视为字符,因此请尝试添加指向附件范围的链接。 像这样:
NSTextAttachment *attachment = [[NSTextAttachment alloc] initWithData:nil ofType:nil];
attachment.image = [UIImage imageNamed:@"myImage"];
NSAttributedString *imageString = [NSAttributedString attributedStringWithAttachment:attachment];
NSMutableAttributedString *mutableImageString = [imageString mutableCopy];
[mutableImageString addAttribute:NSLinkAttributeName value:@"http://stackoverflow.com" range:NSMakeRange(0, mutableImageString.length)];