0

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;
}

手伝ってくれてありがとう :)

4

2 に答える 2

1

掘り下げた後、レイアウトマネージャーを無効にすることでtextviewが画像を再描画できることがわかりました

[textView.layoutManager invalidateDisplayForCharacterRange:characterRange];
于 2015-07-23T16:11:48.343 に答える
-1
[textView.layoutManager invalidateDisplayForCharacterRange:characterRange];//refresh display
[textView.layoutManager invalidateLayoutForCharacterRange:characterRange actualCharacterRange:NULL];//refresh layout
于 2015-11-08T08:14:54.440 に答える