123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140 |
- //
- // YYTextAttachmentExample.m
- // YYKitExample
- //
- // Created by ibireme on 15/8/21.
- // Copyright (c) 2015 ibireme. All rights reserved.
- //
- #import "YYTextAttachmentExample.h"
- #import "YYText.h"
- #import "YYImage.h"
- #import "UIImage+YYWebImage.h"
- #import "UIView+YYAdd.h"
- #import "NSBundle+YYAdd.h"
- #import "NSString+YYAdd.h"
- #import "YYTextExampleHelper.h"
- #import "YYGestureRecognizer.h"
- @interface YYTextAttachmentExample ()<UIGestureRecognizerDelegate>
- @property (nonatomic, strong) YYLabel *label;
- @end
- @implementation YYTextAttachmentExample
- - (void)viewDidLoad {
- [super viewDidLoad];
- self.view.backgroundColor = [UIColor whiteColor];
- [YYTextExampleHelper addDebugOptionToViewController:self];
-
- NSMutableAttributedString *text = [NSMutableAttributedString new];
- UIFont *font = [UIFont systemFontOfSize:16];
-
- {
- NSString *title = @"This is UIImage attachment:";
- [text appendAttributedString:[[NSAttributedString alloc] initWithString:title attributes:nil]];
-
- UIImage *image = [UIImage imageNamed:@"dribbble64_imageio"];
- image = [UIImage imageWithCGImage:image.CGImage scale:2 orientation:UIImageOrientationUp];
-
- NSMutableAttributedString *attachText = [NSMutableAttributedString yy_attachmentStringWithContent:image contentMode:UIViewContentModeCenter attachmentSize:image.size alignToFont:font alignment:YYTextVerticalAlignmentCenter];
- [text appendAttributedString:attachText];
- [text appendAttributedString:[[NSAttributedString alloc] initWithString:@"\n" attributes:nil]];
- }
-
- {
- NSString *title = @"This is UIView attachment: ";
- [text appendAttributedString:[[NSAttributedString alloc] initWithString:title attributes:nil]];
-
- UISwitch *switcher = [UISwitch new];
- [switcher sizeToFit];
-
- NSMutableAttributedString *attachText = [NSMutableAttributedString yy_attachmentStringWithContent:switcher contentMode:UIViewContentModeCenter attachmentSize:switcher.size alignToFont:font alignment:YYTextVerticalAlignmentCenter];
- [text appendAttributedString:attachText];
- [text appendAttributedString:[[NSAttributedString alloc] initWithString:@"\n" attributes:nil]];
- }
-
- {
- NSString *title = @"This is Animated Image attachment:";
- [text appendAttributedString:[[NSAttributedString alloc] initWithString:title attributes:nil]];
-
- NSArray *names = @[@"001", @"022", @"019",@"056",@"085"];
- for (NSString *name in names) {
- NSString *path = [[NSBundle mainBundle] pathForScaledResource:name ofType:@"gif" inDirectory:@"EmoticonQQ.bundle"];
- NSData *data = [NSData dataWithContentsOfFile:path];
- YYImage *image = [YYImage imageWithData:data scale:2];
- image.preloadAllAnimatedImageFrames = YES;
- YYAnimatedImageView *imageView = [[YYAnimatedImageView alloc] initWithImage:image];
-
- NSMutableAttributedString *attachText = [NSMutableAttributedString yy_attachmentStringWithContent:imageView contentMode:UIViewContentModeCenter attachmentSize:imageView.size alignToFont:font alignment:YYTextVerticalAlignmentCenter];
- [text appendAttributedString:attachText];
- }
-
- YYImage *image = [YYImage imageNamed:@"pia"];
- image.preloadAllAnimatedImageFrames = YES;
- YYAnimatedImageView *imageView = [[YYAnimatedImageView alloc] initWithImage:image];
- imageView.autoPlayAnimatedImage = NO;
- [imageView startAnimating];
-
- NSMutableAttributedString *attachText = [NSMutableAttributedString yy_attachmentStringWithContent:imageView contentMode:UIViewContentModeCenter attachmentSize:imageView.size alignToFont:font alignment:YYTextVerticalAlignmentBottom];
- [text appendAttributedString:attachText];
-
- [text appendAttributedString:[[NSAttributedString alloc] initWithString:@"\n" attributes:nil]];
- }
-
-
-
- text.yy_font = font;
-
- _label = [YYLabel new];
- _label.userInteractionEnabled = YES;
- _label.numberOfLines = 0;
- _label.size = CGSizeMake(260, 260);
- _label.center = CGPointMake(self.view.width / 2, self.view.height / 2);
- _label.attributedText = text;
- [self.view addSubview:_label];
-
- _label.layer.borderWidth = 0.5;
- _label.layer.borderColor = [UIColor colorWithRed:0.000 green:0.463 blue:1.000 alpha:1.000].CGColor;
-
-
- __weak typeof(_label) wlabel = _label;
- UIView *dot = [self newDotView];
- dot.center = CGPointMake(_label.width, _label.height);
- dot.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleTopMargin;
- [_label addSubview:dot];
- YYGestureRecognizer *gesture = [YYGestureRecognizer new];
- gesture.action = ^(YYGestureRecognizer *gesture, YYGestureRecognizerState state) {
- if (state != YYGestureRecognizerStateMoved) return;
- CGFloat width = gesture.currentPoint.x;
- CGFloat height = gesture.currentPoint.y;
- wlabel.width = width < 30 ? 30 : width;
- wlabel.height = height < 30 ? 30 : height;
- };
- gesture.delegate = self;
- [_label addGestureRecognizer:gesture];
- }
- - (UIView *)newDotView {
- UIView *view = [UIView new];
- view.size = CGSizeMake(50, 50);
-
- UIView *dot = [UIView new];
- dot.size = CGSizeMake(10, 10);
- dot.backgroundColor = [UIColor colorWithRed:0.000 green:0.463 blue:1.000 alpha:1.000];
- dot.clipsToBounds = YES;
- dot.layer.cornerRadius = dot.height / 2;
- dot.center = CGPointMake(view.width / 2, view.height / 2);
- [view addSubview:dot];
-
- return view;
- }
- - (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer {
- CGPoint p = [gestureRecognizer locationInView:_label];
- if (p.x < _label.width - 20) return NO;
- if (p.y < _label.height - 20) return NO;
- return YES;
- }
- @end
|