【发布时间】:2015-07-23 02:07:26
【问题描述】:
我使用 NSTextAttachment 来显示占位符图像。单击此图像时,我想 显示另一张图片。我在 UItextView 委托方法中这样做:
-(BOOL)textView:(UITextView *)textView shouldInteractWithTextAttachment:(NSTextAttachment *)textAttachment inRange:(NSRange)characterRange
我遇到以下问题: 点击(单击)图像并已进入委托方法,但图像不会立即更改。 我尝试更改主队列中的图像并调用 setNeedsDisplay,但它仍然无法正常工作。 请帮忙:)
这是我的主要代码:
viewDidLoad:
NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:@"This is an example by @marcelofabri_"];
[attributedString addAttribute:NSLinkAttributeName
value:@"username://marcelofabri_"
range:[[attributedString string] rangeOfString:@"@marcelofabri_"]];
//first image
NSTextAttachment *textAttachment = [[NSTextAttachment alloc] init];
textAttachment.image = [UIImage imageNamed:@"sample_image.png"];
NSAttributedString *attrStringWithImage = [NSAttributedString attributedStringWithAttachment:textAttachment];
[attributedString appendAttributedString:attrStringWithImage];
self.textView.attributedText = attributedString;
self.textView.delegate = self;
self.textView.editable = NO;
UITextView 委托方法
-(BOOL)textView:(UITextView *)textView shouldInteractWithTextAttachment:(NSTextAttachment *)textAttachment inRange:(NSRange)characterRange{
UIImage *image = textAttachment.image;
NSLog(@"width:%f , height:%f",image.size.width,image.size.height);
//change second image
dispatch_async(dispatch_get_main_queue(), ^{
textAttachment.image = [UIImage imageNamed:@"sample_image_1"];
[_textView setNeedsDisplay];
});
return true;
}
感谢您的帮助:)
【问题讨论】:
-
在 setNeedsDisplay 之前写入 [_textView layoutIfNeeded]
-
即使我尝试了相同的代码及其工作
-
太奇怪了,我发现系统模态时长按占位符图像(保存到相机胶卷)ActionShet视图,第二张图像可以显示。
标签: ios objective-c textkit nstextattachment