@@ -33,6 +33,8 @@ final class IssueCommentHtmlCell: IssueCommentBaseCell, ListBindable, UIWebViewD
3333
3434 private static let ImgScheme = " freetime-img "
3535 private static let HeightScheme = " freetime-hgt "
36+ private static let JavaScriptHeight = " offsetHeight "
37+
3638 private static let htmlHead = """
3739 <!DOCTYPE html><html><head><style>
3840 * {margin: 0;padding: 0;}
@@ -80,9 +82,9 @@ final class IssueCommentHtmlCell: IssueCommentBaseCell, ListBindable, UIWebViewD
8082 imgs[i].addEventListener('click', tapAction);
8183 }
8284 function onElementHeightChange(elm, callback) {
83- var lastHeight = elm.offsetHeight , newHeight;
85+ var lastHeight = elm. \( IssueCommentHtmlCell . JavaScriptHeight ) , newHeight;
8486 (function run() {
85- newHeight = elm.offsetHeight ;
87+ newHeight = elm. \( IssueCommentHtmlCell . JavaScriptHeight ) ;
8688 if(lastHeight != newHeight) {
8789 callback(newHeight);
8890 }
@@ -139,6 +141,15 @@ final class IssueCommentHtmlCell: IssueCommentBaseCell, ListBindable, UIWebViewD
139141 }
140142 }
141143
144+ // MARK: Private API
145+
146+ func changed( height: CGFloat ) {
147+ guard isHidden == false , height != bounds. height else { return }
148+
149+ let size = CGSize ( width: contentView. bounds. width, height: CGFloat ( height) )
150+ delegate? . webViewDidResize ( cell: self , html: body, cellWidth: size. width, size: size)
151+ }
152+
142153 // MARK: ListBindable
143154
144155 func bindViewModel( _ viewModel: Any ) {
@@ -163,9 +174,7 @@ final class IssueCommentHtmlCell: IssueCommentBaseCell, ListBindable, UIWebViewD
163174 return false
164175 } else if url. scheme == IssueCommentHtmlCell . HeightScheme,
165176 let heightString = url. host as NSString ? {
166- webView. alpha = 1
167- let size = CGSize ( width: contentView. bounds. width, height: CGFloat ( heightString. floatValue) )
168- delegate? . webViewDidResize ( cell: self , html: body, cellWidth: size. width, size: size)
177+ changed ( height: CGFloat ( heightString. floatValue) )
169178 return false
170179 }
171180
@@ -182,6 +191,11 @@ final class IssueCommentHtmlCell: IssueCommentBaseCell, ListBindable, UIWebViewD
182191
183192 func webViewDidFinishLoad( _ webView: UIWebView ) {
184193 webView. alpha = 1
194+
195+ if let heightString = webView
196+ . stringByEvaluatingJavaScript ( from: " document.body. \( IssueCommentHtmlCell . JavaScriptHeight) " ) as NSString ? {
197+ changed ( height: CGFloat ( heightString. floatValue) )
198+ }
185199 }
186200
187201}
0 commit comments