ASCellNode.m 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. /* Copyright (c) 2014-present, Facebook, Inc.
  2. * All rights reserved.
  3. *
  4. * This source code is licensed under the BSD-style license found in the
  5. * LICENSE file in the root directory of this source tree. An additional grant
  6. * of patent rights can be found in the PATENTS file in the same directory.
  7. */
  8. #import "ASCellNode.h"
  9. #import <AsyncDisplayKit/_ASDisplayView.h>
  10. #import <AsyncDisplayKit/ASDisplayNode+Subclasses.h>
  11. #import <AsyncDisplayKit/ASTextNode.h>
  12. #pragma mark -
  13. #pragma mark ASCellNode
  14. @implementation ASCellNode
  15. - (instancetype)init
  16. {
  17. if (!(self = [super init]))
  18. return nil;
  19. // use UITableViewCell defaults
  20. _selectionStyle = UITableViewCellSelectionStyleDefault;
  21. self.clipsToBounds = YES;
  22. return self;
  23. }
  24. - (instancetype)initWithLayerBlock:(ASDisplayNodeLayerBlock)viewBlock
  25. {
  26. ASDisplayNodeAssertNotSupported();
  27. return nil;
  28. }
  29. - (instancetype)initWithViewBlock:(ASDisplayNodeViewBlock)viewBlock
  30. {
  31. ASDisplayNodeAssertNotSupported();
  32. return nil;
  33. }
  34. - (void)setLayerBacked:(BOOL)layerBacked
  35. {
  36. // ASRangeController expects ASCellNodes to be view-backed. (Layer-backing is supported on ASCellNode subnodes.)
  37. ASDisplayNodeAssert(!layerBacked, @"ASCellNode does not support layer-backing.");
  38. }
  39. - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
  40. {
  41. ASDisplayNodeAssertMainThread();
  42. ASDisplayNodeAssert([self.view isKindOfClass:_ASDisplayView.class], @"ASCellNode views must be of type _ASDisplayView");
  43. [(_ASDisplayView *)self.view __forwardTouchesBegan:touches withEvent:event];
  44. }
  45. - (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
  46. {
  47. ASDisplayNodeAssertMainThread();
  48. ASDisplayNodeAssert([self.view isKindOfClass:_ASDisplayView.class], @"ASCellNode views must be of type _ASDisplayView");
  49. [(_ASDisplayView *)self.view __forwardTouchesMoved:touches withEvent:event];
  50. }
  51. - (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
  52. {
  53. ASDisplayNodeAssertMainThread();
  54. ASDisplayNodeAssert([self.view isKindOfClass:_ASDisplayView.class], @"ASCellNode views must be of type _ASDisplayView");
  55. [(_ASDisplayView *)self.view __forwardTouchesEnded:touches withEvent:event];
  56. }
  57. - (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event
  58. {
  59. ASDisplayNodeAssertMainThread();
  60. ASDisplayNodeAssert([self.view isKindOfClass:_ASDisplayView.class], @"ASCellNode views must be of type _ASDisplayView");
  61. [(_ASDisplayView *)self.view __forwardTouchesCancelled:touches withEvent:event];
  62. }
  63. @end
  64. #pragma mark -
  65. #pragma mark ASTextCellNode
  66. @interface ASTextCellNode () {
  67. NSString *_text;
  68. ASTextNode *_textNode;
  69. }
  70. @end
  71. @implementation ASTextCellNode
  72. static const CGFloat kHorizontalPadding = 15.0f;
  73. static const CGFloat kVerticalPadding = 11.0f;
  74. static const CGFloat kFontSize = 18.0f;
  75. - (instancetype)init
  76. {
  77. if (!(self = [super init]))
  78. return nil;
  79. _textNode = [[ASTextNode alloc] init];
  80. [self addSubnode:_textNode];
  81. return self;
  82. }
  83. - (CGSize)calculateSizeThatFits:(CGSize)constrainedSize
  84. {
  85. CGSize availableSize = CGSizeMake(constrainedSize.width - 2 * kHorizontalPadding,
  86. constrainedSize.height - 2 * kVerticalPadding);
  87. availableSize.width = MAX(0, availableSize.width);
  88. availableSize.height = MAX(0, availableSize.height);
  89. CGSize textNodeSize = [_textNode measure:availableSize];
  90. return CGSizeMake(ceilf(2 * kHorizontalPadding + textNodeSize.width),
  91. ceilf(2 * kVerticalPadding + textNodeSize.height));
  92. }
  93. - (void)layout
  94. {
  95. _textNode.frame = CGRectInset(self.bounds, kHorizontalPadding, kVerticalPadding);
  96. }
  97. - (void)setText:(NSString *)text
  98. {
  99. if (_text == text)
  100. return;
  101. _text = [text copy];
  102. _textNode.attributedString = [[NSAttributedString alloc] initWithString:_text
  103. attributes:@{NSFontAttributeName: [UIFont systemFontOfSize:kFontSize]}];
  104. [self invalidateCalculatedSize];
  105. }
  106. @end