CommonEditorWebCell.m 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  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. @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. [self removeObserver:self forKeyPath:@"self.webview.scrollView.frame"];
  35. }
  36. - (void)setup {
  37. self.webview.scrollView.bounces=NO;
  38. self.webview.scrollView.directionalLockEnabled = true;
  39. [self addObserver:self forKeyPath:@"self.webview.scrollView.frame" options:NSKeyValueObservingOptionNew context:nil];
  40. }
  41. - (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSKeyValueChangeKey,id> *)change context:(void *)context {
  42. if ([keyPath isEqualToString:@"self.webview.scrollView.frame"]) {
  43. [self updateContentHeight];
  44. }
  45. }
  46. #pragma mark - Web Delegate
  47. - (void)updateContentHeight {
  48. NSString *h_str = [self.webview stringByEvaluatingJavaScriptFromString:@"document.body.scrollHeight;"];
  49. float h = [h_str floatValue];
  50. if (self.webDelegate) {
  51. [self.webDelegate commonEditorWebCell:self didChangeContentHeight:h];
  52. }
  53. }
  54. - (void)webViewDidFinishLoad:(UIWebView *)webView {
  55. [self updateContentHeight];
  56. }
  57. - (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error {
  58. [self updateContentHeight];
  59. }
  60. @end