Emmett
Emmett

Reputation: 14327

Single-tap for text selection doesn't work in UIWebView when content is small

I have a UIWebView with some lorem ipsum:

- (void)loadView
{
    UIWebView *webView = [[UIWebView alloc] initWithFrame:CGRectMake(0, 0, 300, 100)];

    [webView loadHTMLString:@"Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nulla quis urna risus, vitae sagittis justo. Aenean et justo elementum augue adipiscing tincidunt eget nec urna. Cras lorem nunc, commodo at ullamcorper ut, eleifend nec metus. Phasellus lectus elit, mattis non fermentum quis, elementum vel augue." baseURL:nil];

    UIView *container = [[UIView alloc] init];
    [container setBackgroundColor:[UIColor grayColor]];
    [container addSubview:webView];
    [self setView:container];
}

I can single-tap-and-hold to select the text, as expected:

enter image description here

But when I remove the last couple sentences of lorem ipsum (such that the content fits inside the webview's frame, and the vertical scrollbar is no longer induced), then I can no longer single-tap-and-hold to select text:

enter image description here

(I can still select the text, however, via double-tap-and-hold, though this has the slightly different behavior of selecting the entire paragraph by default.)

I'm struggling to (a) understand the current behavior and/or (b) fix it so single-tap-and-hold always selects the text.

Upvotes: 1

Views: 748

Answers (1)

Emmett
Emmett

Reputation: 14327

I seem to have stumbled upon the solution while randomly fiddling with variables:

The text is only selectable (via single-tap-and-hold) if either (a) the height of the webview is >= 100 or (b) the height of the webview's content is >= 100.

100 seems to be the magic number here: if I bump the height from 100 to 101, then text selection works as expected.

EDIT

In a separate example, the magic number was 104. So there's some height n, below which text section stops working normally, but I haven't discovered the pattern behind n.

Upvotes: 3

Related Questions