YYTextAttachmentExample.m 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. //
  2. // YYTextAttachmentExample.m
  3. // YYKitExample
  4. //
  5. // Created by ibireme on 15/8/21.
  6. // Copyright (c) 2015 ibireme. All rights reserved.
  7. //
  8. #import "YYTextAttachmentExample.h"
  9. #import "YYKit.h"
  10. #import "YYTextExampleHelper.h"
  11. #import "YYImageExampleHelper.h"
  12. @interface YYTextAttachmentExample ()<UIGestureRecognizerDelegate>
  13. @property (nonatomic, strong) YYLabel *label;
  14. @end
  15. @implementation YYTextAttachmentExample
  16. - (void)viewDidLoad {
  17. [super viewDidLoad];
  18. self.view.backgroundColor = [UIColor whiteColor];
  19. [YYTextExampleHelper addDebugOptionToViewController:self];
  20. NSMutableAttributedString *text = [NSMutableAttributedString new];
  21. UIFont *font = [UIFont systemFontOfSize:16];
  22. {
  23. NSString *title = @"This is UIImage attachment:";
  24. [text appendAttributedString:[[NSAttributedString alloc] initWithString:title attributes:nil]];
  25. UIImage *image = [UIImage imageNamed:@"dribbble64_imageio"];
  26. image = [UIImage imageWithCGImage:image.CGImage scale:2 orientation:UIImageOrientationUp];
  27. NSMutableAttributedString *attachText = [NSMutableAttributedString attachmentStringWithContent:image contentMode:UIViewContentModeCenter attachmentSize:image.size alignToFont:font alignment:YYTextVerticalAlignmentCenter];
  28. [text appendAttributedString:attachText];
  29. [text appendAttributedString:[[NSAttributedString alloc] initWithString:@"\n" attributes:nil]];
  30. }
  31. {
  32. NSString *title = @"This is UIView attachment: ";
  33. [text appendAttributedString:[[NSAttributedString alloc] initWithString:title attributes:nil]];
  34. UISwitch *switcher = [UISwitch new];
  35. [switcher sizeToFit];
  36. NSMutableAttributedString *attachText = [NSMutableAttributedString attachmentStringWithContent:switcher contentMode:UIViewContentModeCenter attachmentSize:switcher.size alignToFont:font alignment:YYTextVerticalAlignmentCenter];
  37. [text appendAttributedString:attachText];
  38. [text appendAttributedString:[[NSAttributedString alloc] initWithString:@"\n" attributes:nil]];
  39. }
  40. {
  41. NSString *title = @"This is Animated Image attachment:";
  42. [text appendAttributedString:[[NSAttributedString alloc] initWithString:title attributes:nil]];
  43. NSArray *names = @[@"001", @"022", @"019",@"056",@"085"];
  44. for (NSString *name in names) {
  45. NSString *path = [[NSBundle mainBundle] pathForScaledResource:name ofType:@"gif" inDirectory:@"EmoticonQQ.bundle"];
  46. NSData *data = [NSData dataWithContentsOfFile:path];
  47. YYImage *image = [YYImage imageWithData:data scale:2];
  48. image.preloadAllAnimatedImageFrames = YES;
  49. YYAnimatedImageView *imageView = [[YYAnimatedImageView alloc] initWithImage:image];
  50. NSMutableAttributedString *attachText = [NSMutableAttributedString attachmentStringWithContent:imageView contentMode:UIViewContentModeCenter attachmentSize:imageView.size alignToFont:font alignment:YYTextVerticalAlignmentCenter];
  51. [text appendAttributedString:attachText];
  52. }
  53. YYImage *image = [YYImage imageNamed:@"wall-e.webp"];
  54. image.preloadAllAnimatedImageFrames = YES;
  55. YYAnimatedImageView *imageView = [[YYAnimatedImageView alloc] initWithImage:image];
  56. imageView.autoPlayAnimatedImage = NO;
  57. [imageView startAnimating];
  58. [YYImageExampleHelper addTapControlToAnimatedImageView:imageView];
  59. [YYImageExampleHelper addPanControlToAnimatedImageView:imageView];
  60. NSMutableAttributedString *attachText = [NSMutableAttributedString attachmentStringWithContent:imageView contentMode:UIViewContentModeCenter attachmentSize:imageView.size alignToFont:font alignment:YYTextVerticalAlignmentBottom];
  61. [text appendAttributedString:attachText];
  62. [text appendAttributedString:[[NSAttributedString alloc] initWithString:@"\n" attributes:nil]];
  63. }
  64. text.font = font;
  65. _label = [YYLabel new];
  66. _label.userInteractionEnabled = YES;
  67. _label.numberOfLines = 0;
  68. _label.textVerticalAlignment = YYTextVerticalAlignmentTop;
  69. _label.size = CGSizeMake(260, 260);
  70. _label.center = CGPointMake(self.view.width / 2, self.view.height / 2 - (kiOS7Later ? 0 : 32));
  71. _label.attributedText = text;
  72. [self addSeeMoreButton];
  73. [self.view addSubview:_label];
  74. _label.layer.borderWidth = CGFloatFromPixel(1);
  75. _label.layer.borderColor = [UIColor colorWithRed:0.000 green:0.463 blue:1.000 alpha:1.000].CGColor;
  76. __weak typeof(_label) wlabel = _label;
  77. UIView *dot = [self newDotView];
  78. dot.center = CGPointMake(_label.width, _label.height);
  79. dot.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleTopMargin;
  80. [_label addSubview:dot];
  81. YYGestureRecognizer *gesture = [YYGestureRecognizer new];
  82. gesture.action = ^(YYGestureRecognizer *gesture, YYGestureRecognizerState state) {
  83. if (state != YYGestureRecognizerStateMoved) return;
  84. CGFloat width = gesture.currentPoint.x;
  85. CGFloat height = gesture.currentPoint.y;
  86. wlabel.width = width < 30 ? 30 : width;
  87. wlabel.height = height < 30 ? 30 : height;
  88. };
  89. gesture.delegate = self;
  90. [_label addGestureRecognizer:gesture];
  91. }
  92. - (void)addSeeMoreButton {
  93. __weak typeof(self) _self = self;
  94. NSMutableAttributedString *text = [[NSMutableAttributedString alloc] initWithString:@"...more"];
  95. YYTextHighlight *hi = [YYTextHighlight new];
  96. [hi setColor:[UIColor colorWithRed:0.578 green:0.790 blue:1.000 alpha:1.000]];
  97. hi.tapAction = ^(UIView *containerView, NSAttributedString *text, NSRange range, CGRect rect) {
  98. YYLabel *label = _self.label;
  99. [label sizeToFit];
  100. };
  101. [text setColor:[UIColor colorWithRed:0.000 green:0.449 blue:1.000 alpha:1.000] range:[text.string rangeOfString:@"more"]];
  102. [text setTextHighlight:hi range:[text.string rangeOfString:@"more"]];
  103. text.font = _label.font;
  104. YYLabel *seeMore = [YYLabel new];
  105. seeMore.attributedText = text;
  106. [seeMore sizeToFit];
  107. NSAttributedString *truncationToken = [NSAttributedString attachmentStringWithContent:seeMore contentMode:UIViewContentModeCenter attachmentSize:seeMore.size alignToFont:text.font alignment:YYTextVerticalAlignmentCenter];
  108. _label.truncationToken = truncationToken;
  109. }
  110. - (UIView *)newDotView {
  111. UIView *view = [UIView new];
  112. view.size = CGSizeMake(50, 50);
  113. UIView *dot = [UIView new];
  114. dot.size = CGSizeMake(10, 10);
  115. dot.backgroundColor = [UIColor colorWithRed:0.000 green:0.463 blue:1.000 alpha:1.000];
  116. dot.clipsToBounds = YES;
  117. dot.layer.cornerRadius = dot.height / 2;
  118. dot.center = CGPointMake(view.width / 2, view.height / 2);
  119. [view addSubview:dot];
  120. return view;
  121. }
  122. - (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer {
  123. CGPoint p = [gestureRecognizer locationInView:_label];
  124. if (p.x < _label.width - 20) return NO;
  125. if (p.y < _label.height - 20) return NO;
  126. return YES;
  127. }
  128. @end