YYTextAttachmentExample.m 7.0 KB

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