CommonEditorWebCell.m 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. //
  2. // CommonEditorWebCell.m
  3. // RedAnt Mobile
  4. //
  5. // Created by Jack on 2017/11/17.
  6. // Copyright © 2017年 Ray. All rights reserved.
  7. //
  8. #import "CommonEditorWebCell.h"
  9. //@interface CommonEditorWebCell() <UIWebViewDelegate>
  10. @interface CommonEditorWebCell() <WKNavigationDelegate>
  11. @end
  12. @implementation CommonEditorWebCell
  13. - (void)awakeFromNib {
  14. [super awakeFromNib];
  15. // Initialization code
  16. [self setup];
  17. }
  18. - (void)setSelected:(BOOL)selected animated:(BOOL)animated {
  19. [super setSelected:selected animated:animated];
  20. // Configure the view for the selected state
  21. }
  22. - (instancetype)initWithCoder:(NSCoder *)aDecoder {
  23. if (self = [super initWithCoder:aDecoder]) {
  24. [self setup];
  25. }
  26. return self;
  27. }
  28. - (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
  29. if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
  30. [self setup];
  31. }
  32. return self;
  33. }
  34. - (void)dealloc {
  35. // [self removeObserver:self forKeyPath:@"self.webview.scrollView.frame"];
  36. }
  37. - (void)setup {
  38. self.webview.scrollView.bounces=NO;
  39. self.webview.scrollView.directionalLockEnabled = true;
  40. self.webview.navigationDelegate = self;
  41. if (@available(iOS 11, *)) {
  42. self.webview.scrollView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
  43. }
  44. // [self addObserver:self forKeyPath:@"self.webview.scrollView.frame" options:NSKeyValueObservingOptionNew context:nil];
  45. }
  46. - (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSKeyValueChangeKey,id> *)change context:(void *)context {
  47. if ([keyPath isEqualToString:@"self.webview.scrollView.frame"]) {
  48. [self updateContentHeight];
  49. }
  50. }
  51. #pragma mark - Web Delegate
  52. - (void)updateContentHeight {
  53. // return;
  54. [self.webview evaluateJavaScript:@"document.body.scrollHeight;" completionHandler:^(id _Nullable fitHeight, NSError * _Nullable error) {
  55. // NSString *h_str = [self.webview stringByEvaluatingJavaScriptFromString:@"document.body.scrollHeight;"];
  56. NSString *h_str =fitHeight;
  57. float h = [h_str floatValue];
  58. if (self.webDelegate) {
  59. [self.webDelegate commonEditorWebCell:self didChangeContentHeight:h];
  60. }
  61. }];
  62. }
  63. - (void)webView:(WKWebView *)webView didFinishNavigation:(WKNavigation *)navigation
  64. {
  65. [self updateContentHeight];
  66. }
  67. //- (void)webViewDidFinishLoad:(UIWebView *)webView {
  68. //
  69. // [self updateContentHeight];
  70. //}
  71. //- (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error {
  72. //// [self updateContentHeight];
  73. //}
  74. @end