CommonEditorWebCell.m 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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() <WKNavigationDelegate>
  10. @end
  11. @implementation CommonEditorWebCell
  12. - (void)awakeFromNib {
  13. [super awakeFromNib];
  14. // Initialization code
  15. [self setup];
  16. }
  17. - (void)setSelected:(BOOL)selected animated:(BOOL)animated {
  18. [super setSelected:selected animated:animated];
  19. // Configure the view for the selected state
  20. }
  21. - (instancetype)initWithCoder:(NSCoder *)aDecoder {
  22. if (self = [super initWithCoder:aDecoder]) {
  23. [self setup];
  24. }
  25. return self;
  26. }
  27. - (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
  28. if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
  29. [self setup];
  30. }
  31. return self;
  32. }
  33. - (void)dealloc {
  34. }
  35. - (void)setup {
  36. self.wkwebview.scrollView.bounces=NO;
  37. self.wkwebview.scrollView.directionalLockEnabled = true;
  38. self.wkwebview.navigationDelegate = self;
  39. if (@available(iOS 11, *)) {
  40. self.wkwebview.scrollView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
  41. }
  42. }
  43. - (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSKeyValueChangeKey,id> *)change context:(void *)context {
  44. if ([keyPath isEqualToString:@"self.wkwebview.scrollView.frame"]) {
  45. [self updateContentHeight];
  46. }
  47. }
  48. #pragma mark - Web Delegate
  49. - (void)updateContentHeight {
  50. // return;
  51. [self.wkwebview evaluateJavaScript:@"document.body.scrollHeight;" completionHandler:^(id _Nullable fitHeight, NSError * _Nullable error) {
  52. NSString *h_str =fitHeight;
  53. float h = [h_str floatValue];
  54. if (self.webDelegate) {
  55. [self.webDelegate commonEditorWebCell:self didChangeContentHeight:h];
  56. }
  57. }];
  58. }
  59. - (void)webView:(WKWebView *)webView didFinishNavigation:(WKNavigation *)navigation
  60. {
  61. [self updateContentHeight];
  62. }
  63. @end