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;
}
手伝ってくれてありがとう :)