CHRichTextEditor icon indicating copy to clipboard operation
CHRichTextEditor copied to clipboard

Content Height?

Open MattFoley opened this issue 13 years ago • 2 comments

Hi Cathandnya,

I've added a Font Selector to your editor, which I'd be glad to share with you, but I also have a question for you. Have you figured out how to find the exact size/height of the content inside your Editor?

Thanks!

MattFoley avatar Oct 22 '12 17:10 MattFoley

Hello,

I tried to examine content height of the editor (UIWebView) but I could not get it correctly. Thank you.

cathandnya avatar Oct 23 '12 23:10 cathandnya

I'm still working on cleaning this up, it's too much of a mess to give you a pull request, but here is the gist of the solution:

You'll need some way to hold the correct height around, in this example I'm just using a view. Use this as your done method, to get the correct height first:

NSString *innerHTML = [webView stringByEvaluatingJavaScriptFromString:@"document.getElementById('entryContents').innerHTML"];

CGFloat height = [[webView stringByEvaluatingJavaScriptFromString:@"document.getElementById(\"entryContents\").offsetHeight"] floatValue];

_viewToCreate = [self imageWithView:webView ofSize:CGSizeMake(webView.frame.size.width, height*1.25)];
[_viewToCreate setAutoresizesSubviews:NO];

self.html = innerHTML;

//Table gives accurate width
[webView loadHTMLString:[NSString stringWithFormat:@"<table id='foo' style=\"float=left;width=Auto;height=Auto\"><tr><td>%@</td></tr></table>",innerHTML] baseURL:nil];

Then to finish your dismissal, and get the correct width from the Table element use this in webViewDidFinishLoad:

        CGFloat width = [[webView stringByEvaluatingJavaScriptFromString:@"document.getElementById(\"foo\").offsetWidth"] floatValue];
        CGSize sizeOfText = CGSizeMake(width, _viewToCreate.frame.size.height);

Now, sizeOfText contains a somewhat accurate size for the text you entered. It isn't 100% full proof yet, but it'll get the job done.

MattFoley avatar Mar 11 '13 19:03 MattFoley