YYTextContainerView.m 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. //
  2. // YYTextContainerView.m
  3. // YYKit <https://github.com/ibireme/YYKit>
  4. //
  5. // Created by ibireme on 15/4/21.
  6. // Copyright (c) 2015 ibireme.
  7. //
  8. // This source code is licensed under the MIT-style license found in the
  9. // LICENSE file in the root directory of this source tree.
  10. //
  11. #import "YYTextContainerView.h"
  12. @implementation YYTextContainerView {
  13. BOOL _attachmentChanged;
  14. NSMutableArray *_attachmentViews;
  15. NSMutableArray *_attachmentLayers;
  16. }
  17. - (instancetype)initWithFrame:(CGRect)frame {
  18. self = [super initWithFrame:frame];
  19. if (!self) return nil;
  20. self.backgroundColor = [UIColor clearColor];
  21. _attachmentViews = [NSMutableArray array];
  22. _attachmentLayers = [NSMutableArray array];
  23. return self;
  24. }
  25. - (void)setDebugOption:(YYTextDebugOption *)debugOption {
  26. BOOL needDraw = _debugOption.needDrawDebug;
  27. _debugOption = debugOption.copy;
  28. if (_debugOption.needDrawDebug != needDraw) {
  29. [self setNeedsDisplay];
  30. }
  31. }
  32. - (void)setTextVerticalAlignment:(YYTextVerticalAlignment)textVerticalAlignment {
  33. if (_textVerticalAlignment == textVerticalAlignment) return;
  34. _textVerticalAlignment = textVerticalAlignment;
  35. [self setNeedsDisplay];
  36. }
  37. - (void)setContentsFadeDuration:(NSTimeInterval)contentsFadeDuration {
  38. if (_contentsFadeDuration == contentsFadeDuration) return;
  39. _contentsFadeDuration = contentsFadeDuration;
  40. if (contentsFadeDuration <= 0) {
  41. [self.layer removeAnimationForKey:@"contents"];
  42. }
  43. }
  44. - (void)setLayout:(YYTextLayout *)layout {
  45. if (_layout == layout) return;
  46. _layout = layout;
  47. _attachmentChanged = YES;
  48. [self setNeedsDisplay];
  49. }
  50. - (void)setLayout:(YYTextLayout *)layout withFadeDuration:(NSTimeInterval)fadeDuration {
  51. self.contentsFadeDuration = fadeDuration;
  52. self.layout = layout;
  53. }
  54. - (void)drawRect:(CGRect)rect {
  55. // fade content
  56. [self.layer removeAnimationForKey:@"contents"];
  57. if (_contentsFadeDuration > 0) {
  58. CATransition *transition = [CATransition animation];
  59. transition.duration = _contentsFadeDuration;
  60. transition.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseOut];
  61. transition.type = kCATransitionFade;
  62. [self.layer addAnimation:transition forKey:@"contents"];
  63. }
  64. // update attachment
  65. if (_attachmentChanged) {
  66. for (UIView *view in _attachmentViews) {
  67. if (view.superview == self) [view removeFromSuperview];
  68. }
  69. for (CALayer *layer in _attachmentLayers) {
  70. if (layer.superlayer == self.layer) [layer removeFromSuperlayer];
  71. }
  72. [_attachmentViews removeAllObjects];
  73. [_attachmentLayers removeAllObjects];
  74. }
  75. // draw layout
  76. CGSize boundingSize = _layout.textBoundingSize;
  77. CGPoint point = CGPointZero;
  78. if (_textVerticalAlignment == YYTextVerticalAlignmentCenter) {
  79. if (_layout.container.isVerticalForm) {
  80. point.x = -(self.bounds.size.width - boundingSize.width) * 0.5;
  81. } else {
  82. point.y = (self.bounds.size.height - boundingSize.height) * 0.5;
  83. }
  84. } else if (_textVerticalAlignment == YYTextVerticalAlignmentBottom) {
  85. if (_layout.container.isVerticalForm) {
  86. point.x = -(self.bounds.size.width - boundingSize.width);
  87. } else {
  88. point.y = (self.bounds.size.height - boundingSize.height);
  89. }
  90. }
  91. [_layout drawInContext:UIGraphicsGetCurrentContext() size:self.bounds.size point:point view:self layer:self.layer debug:_debugOption cancel:nil];
  92. // update attachment
  93. if (_attachmentChanged) {
  94. _attachmentChanged = NO;
  95. for (YYTextAttachment *a in _layout.attachments) {
  96. if ([a.content isKindOfClass:[UIView class]]) [_attachmentViews addObject:a.content];
  97. if ([a.content isKindOfClass:[CALayer class]]) [_attachmentLayers addObject:a.content];
  98. }
  99. }
  100. }
  101. - (void)setFrame:(CGRect)frame {
  102. CGSize oldSize = self.bounds.size;
  103. [super setFrame:frame];
  104. if (!CGSizeEqualToSize(oldSize, self.bounds.size)) {
  105. [self setNeedsLayout];
  106. }
  107. }
  108. - (void)setBounds:(CGRect)bounds {
  109. CGSize oldSize = self.bounds.size;
  110. [super setBounds:bounds];
  111. if (!CGSizeEqualToSize(oldSize, self.bounds.size)) {
  112. [self setNeedsLayout];
  113. }
  114. }
  115. #pragma mark - UIResponder forward
  116. - (BOOL)canBecomeFirstResponder {
  117. return YES;
  118. }
  119. - (BOOL)canPerformAction:(SEL)action withSender:(id)sender {
  120. return [self.hostView canPerformAction:action withSender:sender];
  121. }
  122. - (id)forwardingTargetForSelector:(SEL)aSelector {
  123. return self.hostView;
  124. }
  125. @end