YYTextAttachmentExample.m 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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.size = CGSizeMake(260, 260);
  72. _label.center = CGPointMake(self.view.width / 2, self.view.height / 2);
  73. _label.attributedText = text;
  74. [self.view addSubview:_label];
  75. _label.layer.borderWidth = 0.5;
  76. _label.layer.borderColor = [UIColor colorWithRed:0.000 green:0.463 blue:1.000 alpha:1.000].CGColor;
  77. __weak typeof(_label) wlabel = _label;
  78. UIView *dot = [self newDotView];
  79. dot.center = CGPointMake(_label.width, _label.height);
  80. dot.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleTopMargin;
  81. [_label addSubview:dot];
  82. YYGestureRecognizer *gesture = [YYGestureRecognizer new];
  83. gesture.action = ^(YYGestureRecognizer *gesture, YYGestureRecognizerState state) {
  84. if (state != YYGestureRecognizerStateMoved) return;
  85. CGFloat width = gesture.currentPoint.x;
  86. CGFloat height = gesture.currentPoint.y;
  87. wlabel.width = width < 30 ? 30 : width;
  88. wlabel.height = height < 30 ? 30 : height;
  89. };
  90. gesture.delegate = self;
  91. [_label addGestureRecognizer:gesture];
  92. }
  93. - (UIView *)newDotView {
  94. UIView *view = [UIView new];
  95. view.size = CGSizeMake(50, 50);
  96. UIView *dot = [UIView new];
  97. dot.size = CGSizeMake(10, 10);
  98. dot.backgroundColor = [UIColor colorWithRed:0.000 green:0.463 blue:1.000 alpha:1.000];
  99. dot.clipsToBounds = YES;
  100. dot.layer.cornerRadius = dot.height / 2;
  101. dot.center = CGPointMake(view.width / 2, view.height / 2);
  102. [view addSubview:dot];
  103. return view;
  104. }
  105. - (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer {
  106. CGPoint p = [gestureRecognizer locationInView:_label];
  107. if (p.x < _label.width - 20) return NO;
  108. if (p.y < _label.height - 20) return NO;
  109. return YES;
  110. }
  111. @end