UITextView textContainer exclusion path fails if full width and positioned at top of textContainer

后端 未结 5 1356
余生分开走
余生分开走 2021-01-01 22:46

In iOS 8, I\'m trying to add a UIImageView as a subview of a UITextView, similar to what\'s shown here - but with the text below the image.

5条回答
  •  孤独总比滥情好
    2021-01-01 23:08

    A quick workaround:

    CGRect exclusionRect = [textView convertRect:imageView.bounds fromView:imageView];
    if (exclusionRect.origin.x <= 0 && exclusionRect.origin.y <= 0 && exclusionRect.size.width >= textView.bounds.size.width) {
      exclusionRect.origin.x = 1;
      exclusionRect.origin.y = 1;
      exclusionRect.size.width -= 2;
    }
    

    Your image will still draw the same and unless you're using a font with glyphs that are 1px wide (I'm not even sure that's possible given kerning, etc), your exclusionRect will be guaranteed to be smaller than the full width.

    I would be interested to know what kind of results you see if you allow your rect to be moved around in real-time. Attach a UIPanGestureRecognizer and update your exclusionRect as you pan around the screen. At what point does the text jump into the image?

    Edit: If you're seeing problems until it is able to fit at least one character, maybe try adjusting your text frame.

    if (exclusionRect.origin.x <= 0 && exclusionRect.origin.y <= 0 && exclusionRect.size.width >= textView.bounds.size.width) {
      frame.origin.y += CGRectGetMaxY(exclusionRect);
      frame.size.height -= CGRectGetMaxY(exclusionRect);
      [textView setFrame:frame];
    }
    

提交回复
热议问题