ios7与uilabel中的nstextattachment进行自定义交互

时间:2013-11-03 08:58:21

标签: ios ios7 uilabel nstextattachment

我有一个NSTextAttachment,其中的图片位于UILabel,我希望在点击此附件时执行自定义行为。

UITextViewDelegate提供了一个方便的方法

textView:shouldInteractWithTextAttachment:inRange:

但只有在我使用UITextView时才能使用它。

如果我使用UILabel

,是否有任何解决方案?

谢谢!

1 个答案:

答案 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)];