├── TMAnimatedTextView.gif ├── TMAnimatedTextView ├── Images.xcassets │ ├── MadisonSquarePark.imageset │ │ ├── MadisonSquarePark.jpg │ │ └── Contents.json │ ├── AppIcon.appiconset │ │ └── Contents.json │ └── LaunchImage.launchimage │ │ └── Contents.json ├── TMAnimatedTextView.h └── TMAnimatedTextView.m ├── .gitignore ├── Demo ├── main.m ├── AppDelegate.h ├── ViewController.h ├── Info.plist ├── AppDelegate.m ├── Base.lproj │ ├── LaunchScreen.xib │ └── Main.storyboard └── ViewController.m ├── TMAnimatedTextView.podspec ├── README.md ├── LICENSE └── TMAnimatedTextView.xcodeproj └── project.pbxproj /TMAnimatedTextView.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/TapMesh/TMAnimatedTextView/HEAD/TMAnimatedTextView.gif -------------------------------------------------------------------------------- /TMAnimatedTextView/Images.xcassets/MadisonSquarePark.imageset/MadisonSquarePark.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/TapMesh/TMAnimatedTextView/HEAD/TMAnimatedTextView/Images.xcassets/MadisonSquarePark.imageset/MadisonSquarePark.jpg -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Xcode 2 | # 3 | build/ 4 | *.pbxuser 5 | !default.pbxuser 6 | *.mode1v3 7 | !default.mode1v3 8 | *.mode2v3 9 | !default.mode2v3 10 | *.perspectivev3 11 | !default.perspectivev3 12 | project.xcworkspace 13 | !default.xcworkspace 14 | xcuserdata 15 | *.xccheckout 16 | *.moved-aside 17 | DerivedData 18 | *.hmap 19 | *.ipa 20 | *.xcuserstate 21 | .DS_Store -------------------------------------------------------------------------------- /Demo/main.m: -------------------------------------------------------------------------------- 1 | // 2 | // main.m 3 | // TMAnimatedTextView 4 | // 5 | // Created by Bob Wieler on 4/7/15. 6 | // Copyright (c) 2015 tapmesh. All rights reserved. 7 | // 8 | 9 | 10 | #import 11 | #import "AppDelegate.h" 12 | 13 | 14 | int main(int argc, char * argv[]) { 15 | @autoreleasepool { 16 | return UIApplicationMain(argc, argv, nil, NSStringFromClass([AppDelegate class])); 17 | } 18 | 19 | } -------------------------------------------------------------------------------- /TMAnimatedTextView/Images.xcassets/MadisonSquarePark.imageset/Contents.json: -------------------------------------------------------------------------------- 1 | { 2 | "images" : [ 3 | { 4 | "idiom" : "universal", 5 | "scale" : "1x", 6 | "filename" : "MadisonSquarePark.jpg" 7 | }, 8 | { 9 | "idiom" : "universal", 10 | "scale" : "2x" 11 | }, 12 | { 13 | "idiom" : "universal", 14 | "scale" : "3x" 15 | } 16 | ], 17 | "info" : { 18 | "version" : 1, 19 | "author" : "xcode" 20 | } 21 | } -------------------------------------------------------------------------------- /Demo/AppDelegate.h: -------------------------------------------------------------------------------- 1 | // 2 | // Created by Bob Wieler on 4/7/15. 3 | // Copyright (c) 2015 TapMesh, Inc. All rights reserved. 4 | // 5 | // Licensed under the Apache License, Version 2.0 (the "License"); 6 | // you may not use this file except in compliance with the License. 7 | // You may obtain a copy of the License at 8 | // 9 | // http://www.apache.org/licenses/LICENSE-2.0 10 | // 11 | // Unless required by applicable law or agreed to in writing, software 12 | // distributed under the License is distributed on an "AS IS" BASIS, 13 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | // See the License for the specific language governing permissions and 15 | // limitations under the License. 16 | 17 | 18 | #import 19 | 20 | 21 | @interface AppDelegate : UIResponder 22 | 23 | @property (strong, nonatomic) UIWindow *window; 24 | 25 | 26 | @end 27 | -------------------------------------------------------------------------------- /TMAnimatedTextView.podspec: -------------------------------------------------------------------------------- 1 | Pod::Spec.new do |s| 2 | s.name = "TMAnimatedTextView" 3 | s.version = "1.0.0" 4 | s.summary = "A UITextView subclass that allows animating NSTextAttachment attachments." 5 | 6 | s.description = <<-DESC 7 | The basic UITextView allows you to attach images however it does not allow you to 8 | animate each individual attachment in the text view. This component allows you to 9 | add an image attachment and animate that attachment. 10 | DESC 11 | 12 | s.homepage = "https://github.com/TapMesh/TMAnimatedTextView" 13 | s.license = "Apache License, Version 2.0" 14 | s.author = { "bobwieler" => "bob.wieler@tapmesh.com" } 15 | s.social_media_url = "http://twitter.com/bobwieler" 16 | s.platform = :ios, '7.1' 17 | s.source = { :git => "https://github.com/TapMesh/TMAnimatedTextView.git", :tag => s.version.to_s } 18 | s.source_files = "TMAnimatedTextView", "TMAnimatedTextView/**/*.{h,m}" 19 | s.requires_arc = true 20 | 21 | end 22 | -------------------------------------------------------------------------------- /Demo/ViewController.h: -------------------------------------------------------------------------------- 1 | // 2 | // Created by Bob Wieler on 4/7/15. 3 | // Copyright (c) 2015 TapMesh, Inc. All rights reserved. 4 | // 5 | // Licensed under the Apache License, Version 2.0 (the "License"); 6 | // you may not use this file except in compliance with the License. 7 | // You may obtain a copy of the License at 8 | // 9 | // http://www.apache.org/licenses/LICENSE-2.0 10 | // 11 | // Unless required by applicable law or agreed to in writing, software 12 | // distributed under the License is distributed on an "AS IS" BASIS, 13 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | // See the License for the specific language governing permissions and 15 | // limitations under the License. 16 | 17 | 18 | #import 19 | #import "TMAnimatedTextView.h" 20 | 21 | @class TMAnimatedTextView; 22 | 23 | @interface ViewController : UIViewController 24 | 25 | @property (nonatomic, weak) IBOutlet TMAnimatedTextView *textView; 26 | 27 | - (IBAction) addText:(id)sender; 28 | - (IBAction) addImage:(id)sender; 29 | 30 | @end 31 | -------------------------------------------------------------------------------- /TMAnimatedTextView/Images.xcassets/AppIcon.appiconset/Contents.json: -------------------------------------------------------------------------------- 1 | { 2 | "images" : [ 3 | { 4 | "idiom" : "iphone", 5 | "size" : "29x29", 6 | "scale" : "2x" 7 | }, 8 | { 9 | "idiom" : "iphone", 10 | "size" : "40x40", 11 | "scale" : "2x" 12 | }, 13 | { 14 | "idiom" : "iphone", 15 | "size" : "60x60", 16 | "scale" : "2x" 17 | }, 18 | { 19 | "idiom" : "iphone", 20 | "size" : "60x60", 21 | "scale" : "3x" 22 | }, 23 | { 24 | "idiom" : "ipad", 25 | "size" : "29x29", 26 | "scale" : "1x" 27 | }, 28 | { 29 | "idiom" : "ipad", 30 | "size" : "29x29", 31 | "scale" : "2x" 32 | }, 33 | { 34 | "idiom" : "ipad", 35 | "size" : "40x40", 36 | "scale" : "1x" 37 | }, 38 | { 39 | "idiom" : "ipad", 40 | "size" : "40x40", 41 | "scale" : "2x" 42 | }, 43 | { 44 | "idiom" : "ipad", 45 | "size" : "76x76", 46 | "scale" : "1x" 47 | }, 48 | { 49 | "idiom" : "ipad", 50 | "size" : "76x76", 51 | "scale" : "2x" 52 | } 53 | ], 54 | "info" : { 55 | "version" : 1, 56 | "author" : "xcode" 57 | } 58 | } -------------------------------------------------------------------------------- /TMAnimatedTextView/Images.xcassets/LaunchImage.launchimage/Contents.json: -------------------------------------------------------------------------------- 1 | { 2 | "images" : [ 3 | { 4 | "orientation" : "portrait", 5 | "idiom" : "ipad", 6 | "minimum-system-version" : "7.0", 7 | "extent" : "full-screen", 8 | "scale" : "2x" 9 | }, 10 | { 11 | "orientation" : "landscape", 12 | "idiom" : "ipad", 13 | "minimum-system-version" : "7.0", 14 | "extent" : "full-screen", 15 | "scale" : "1x" 16 | }, 17 | { 18 | "orientation" : "landscape", 19 | "idiom" : "ipad", 20 | "minimum-system-version" : "7.0", 21 | "extent" : "full-screen", 22 | "scale" : "2x" 23 | }, 24 | { 25 | "orientation" : "portrait", 26 | "idiom" : "iphone", 27 | "minimum-system-version" : "7.0", 28 | "scale" : "2x" 29 | }, 30 | { 31 | "orientation" : "portrait", 32 | "idiom" : "iphone", 33 | "minimum-system-version" : "7.0", 34 | "subtype" : "retina4", 35 | "scale" : "2x" 36 | }, 37 | { 38 | "orientation" : "portrait", 39 | "idiom" : "ipad", 40 | "minimum-system-version" : "7.0", 41 | "extent" : "full-screen", 42 | "scale" : "1x" 43 | } 44 | ], 45 | "info" : { 46 | "version" : 1, 47 | "author" : "xcode" 48 | } 49 | } -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # TMAnimatedTextView 2 | 3 | ![](https://github.com/TapMesh/TMAnimatedTextView/blob/master/TMAnimatedTextView.gif) 4 | 5 | Summary 6 | ------- 7 | 8 | TMAnimatedTextView is a UITextView subclass that allows animating NSTextAttachment attachments. UITextView allows adding images as an attachment but they are static and cannot be animated. This class: 9 | 10 | * allows you to define custom animations for each attachment 11 | * informs a delegate of changes to each attachment's lifecycle (additions, deletions) 12 | * informs a delegate of interactions with the attachment (touches) 13 | * automatically expands up to handle the size of the attachment image 14 | 15 | Instructions 16 | ------------ 17 | 18 | With CocoaPods add the following to your Podfile. 19 | 20 | pod 'TMAnimatedTextView' 21 | 22 | Alternatively, you can add the TMAnimatedTextView.h and TMAnimatedTextView.m files to your project. 23 | 24 | Storyboard Setup 25 | ---------------- 26 | 27 | TMAnimatedTextView works well with storyboards. 28 | 29 | 1. Add a UITextView to your storyboard 30 | 2. Change the Class to a TMAnimatedTextView 31 | 3. Set a height constraint on the TMAnimatedTextView component (this is used to handle the component expansion when adding images) 32 | 4. Have your controller implement the TMAnimatedTextViewDelegate protocol (all methods are optional) 33 | 5. Connect your view controller to the animatedTextViewDelegate outlet on the component 34 | 35 | Demo 36 | ---- 37 | 38 | A demo application is included that utilizes TMAnimatedTextView and includes custom animations for when the attachment is added as well as when the attachment is touched. This demo also shows how to set up the constraints for the component. 39 | -------------------------------------------------------------------------------- /Demo/Info.plist: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | CFBundleDevelopmentRegion 6 | en 7 | CFBundleExecutable 8 | $(EXECUTABLE_NAME) 9 | CFBundleIcons 10 | 11 | CFBundleIcons~ipad 12 | 13 | CFBundleIdentifier 14 | net.tapmesh.$(PRODUCT_NAME:rfc1034identifier) 15 | CFBundleInfoDictionaryVersion 16 | 6.0 17 | CFBundleName 18 | $(PRODUCT_NAME) 19 | CFBundlePackageType 20 | APPL 21 | CFBundleShortVersionString 22 | 1.0 23 | CFBundleSignature 24 | ???? 25 | CFBundleVersion 26 | 1 27 | LSRequiresIPhoneOS 28 | 29 | UILaunchStoryboardName 30 | LaunchScreen 31 | UIMainStoryboardFile 32 | Main 33 | UIRequiredDeviceCapabilities 34 | 35 | armv7 36 | 37 | UISupportedInterfaceOrientations 38 | 39 | UIInterfaceOrientationPortrait 40 | UIInterfaceOrientationLandscapeLeft 41 | UIInterfaceOrientationLandscapeRight 42 | 43 | UISupportedInterfaceOrientations~ipad 44 | 45 | UIInterfaceOrientationPortrait 46 | UIInterfaceOrientationPortraitUpsideDown 47 | UIInterfaceOrientationLandscapeLeft 48 | UIInterfaceOrientationLandscapeRight 49 | 50 | 51 | 52 | -------------------------------------------------------------------------------- /TMAnimatedTextView/TMAnimatedTextView.h: -------------------------------------------------------------------------------- 1 | // 2 | // Created by Bob Wieler on 9/22/14. 3 | // Copyright (c) 2015 TapMesh, Inc. All rights reserved. 4 | // 5 | // Licensed under the Apache License, Version 2.0 (the "License"); 6 | // you may not use this file except in compliance with the License. 7 | // You may obtain a copy of the License at 8 | // 9 | // http://www.apache.org/licenses/LICENSE-2.0 10 | // 11 | // Unless required by applicable law or agreed to in writing, software 12 | // distributed under the License is distributed on an "AS IS" BASIS, 13 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | // See the License for the specific language governing permissions and 15 | // limitations under the License. 16 | 17 | #import 18 | 19 | @class TMAnimatedTextView; 20 | 21 | @protocol TMAttachmentData 22 | @required 23 | - (NSString *) identifier; 24 | - (UIImage *) image; 25 | @end 26 | 27 | @protocol TMAnimatedTextViewDelegate 28 | @optional 29 | - (void) viewBoundsWillChangeFrom:(CGRect)oldBounds to:(CGRect)newBounds; 30 | - (void) viewBoundsDidChangeFrom:(CGRect)oldBounds to:(CGRect)newBounds; 31 | 32 | - (BOOL) shouldAddAttachmentData:(id )data; 33 | - (void) didAddAttachmentData:(id )data; 34 | 35 | - (BOOL) shouldDeleteAttachmentData:(id )data; 36 | - (void) didDeleteAttachmentData:(id )data; 37 | 38 | - (BOOL) shouldInteractWithAttachmentData:(id )data; 39 | - (void) didInteractWithAttachmentData:(id )data; 40 | 41 | - (void) animatedTextViewDidChange:(TMAnimatedTextView *)textView; 42 | 43 | - (void) pasteImage:(UIImage *)image; 44 | 45 | @end 46 | 47 | @interface TMAnimatedTextView : UITextView 48 | 49 | @property(nonatomic) CGFloat maxHeight; 50 | @property(nonatomic) CGFloat minHeight; 51 | 52 | @property(weak, nonatomic) IBOutlet id animatedTextViewDelegate; 53 | 54 | - (void) attachData:(id )data; 55 | - (void) animateData:(id )data withAnimation:(void (^)(UIImageView *imageView))animationBlock; 56 | 57 | @end -------------------------------------------------------------------------------- /Demo/AppDelegate.m: -------------------------------------------------------------------------------- 1 | // 2 | // Created by Bob Wieler on 4/7/15. 3 | // Copyright (c) 2015 TapMesh, Inc. All rights reserved. 4 | // 5 | // Licensed under the Apache License, Version 2.0 (the "License"); 6 | // you may not use this file except in compliance with the License. 7 | // You may obtain a copy of the License at 8 | // 9 | // http://www.apache.org/licenses/LICENSE-2.0 10 | // 11 | // Unless required by applicable law or agreed to in writing, software 12 | // distributed under the License is distributed on an "AS IS" BASIS, 13 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | // See the License for the specific language governing permissions and 15 | // limitations under the License. 16 | 17 | 18 | #import "AppDelegate.h" 19 | 20 | 21 | @interface AppDelegate () 22 | 23 | @end 24 | 25 | @implementation AppDelegate 26 | 27 | 28 | 29 | 30 | - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { 31 | // Override point for customization after application launch. 32 | return YES; 33 | } 34 | 35 | - (void)applicationWillResignActive:(UIApplication *)application { 36 | // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state. 37 | // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game. 38 | 39 | } 40 | 41 | - (void)applicationDidEnterBackground:(UIApplication *)application { 42 | // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later. 43 | // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits. 44 | 45 | } 46 | 47 | - (void)applicationWillEnterForeground:(UIApplication *)application { 48 | // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background. 49 | } 50 | 51 | - (void)applicationDidBecomeActive:(UIApplication *)application { 52 | // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface. 53 | } 54 | 55 | - (void)applicationWillTerminate:(UIApplication *)application { 56 | // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:. 57 | } 58 | 59 | 60 | @end -------------------------------------------------------------------------------- /Demo/Base.lproj/LaunchScreen.xib: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 20 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | -------------------------------------------------------------------------------- /Demo/ViewController.m: -------------------------------------------------------------------------------- 1 | // 2 | // Created by Bob Wieler on 4/7/15. 3 | // Copyright (c) 2015 TapMesh, Inc. All rights reserved. 4 | // 5 | // Licensed under the Apache License, Version 2.0 (the "License"); 6 | // you may not use this file except in compliance with the License. 7 | // You may obtain a copy of the License at 8 | // 9 | // http://www.apache.org/licenses/LICENSE-2.0 10 | // 11 | // Unless required by applicable law or agreed to in writing, software 12 | // distributed under the License is distributed on an "AS IS" BASIS, 13 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | // See the License for the specific language governing permissions and 15 | // limitations under the License. 16 | 17 | 18 | #import "ViewController.h" 19 | 20 | @interface AttachmentData : NSObject 21 | @property (nonatomic, readonly) NSString *identifier; 22 | @property (nonatomic, readonly) UIImage *image; 23 | - (instancetype) initWithIdentifier:(NSString *)identifier image:(UIImage *)image; 24 | @end 25 | 26 | @implementation AttachmentData { 27 | @private 28 | NSString *_identifier; 29 | UIImage *_image; 30 | } 31 | @synthesize identifier = _identifier; 32 | @synthesize image = _image; 33 | 34 | - (instancetype) initWithIdentifier:(NSString *)identifier image:(UIImage *)image { 35 | self = [super init]; 36 | if (self) { 37 | _identifier = identifier; 38 | _image = image; 39 | } 40 | 41 | return self; 42 | } 43 | 44 | @end 45 | 46 | @implementation ViewController { 47 | @private 48 | NSUInteger _identifierCounter; 49 | } 50 | 51 | - (void) viewDidLoad { 52 | [super viewDidLoad]; 53 | 54 | _identifierCounter = 0; 55 | [self.textView becomeFirstResponder]; 56 | } 57 | 58 | - (UIImage *) createImageWithText:(NSString *)text color:(UIColor *)color { 59 | NSAttributedString *attributedString = [[NSAttributedString alloc] 60 | initWithString:text 61 | attributes:@{ 62 | NSFontAttributeName : [UIFont systemFontOfSize:22], 63 | NSForegroundColorAttributeName : [UIColor whiteColor] 64 | }]; 65 | CGRect textRect = [attributedString boundingRectWithSize:(CGSize) {152, CGFLOAT_MAX} 66 | options:NSStringDrawingUsesLineFragmentOrigin 67 | context:nil]; 68 | CGSize imageSize = CGSizeMake(textRect.size.width + 20, 40); 69 | 70 | CALayer *imageLayer = [CALayer layer]; 71 | imageLayer.bounds = CGRectMake(0, 0, imageSize.width, 40); 72 | imageLayer.masksToBounds = YES; 73 | imageLayer.cornerRadius = 10; 74 | imageLayer.backgroundColor = color.CGColor; 75 | 76 | UILabel *label = [[UILabel alloc] init]; 77 | label.attributedText = attributedString; 78 | 79 | UIGraphicsBeginImageContext(imageSize); 80 | 81 | CGContextRef context = UIGraphicsGetCurrentContext(); 82 | [imageLayer renderInContext:context]; 83 | 84 | [label drawTextInRect:CGRectMake(10, 10, textRect.size.width, 20)]; 85 | 86 | UIImage *finalImage = UIGraphicsGetImageFromCurrentImageContext(); 87 | 88 | UIGraphicsEndImageContext(); 89 | return finalImage; 90 | } 91 | 92 | - (UIImage*)scaleImage:(UIImage *)image toSize:(CGSize)size { 93 | UIGraphicsBeginImageContext(size); 94 | CGRect contextRect = CGRectMake(0, 0, size.width, size.height); 95 | [image drawInRect: contextRect]; 96 | UIImage *scaledImage = UIGraphicsGetImageFromCurrentImageContext(); 97 | UIGraphicsEndImageContext(); 98 | 99 | return scaledImage; 100 | } 101 | 102 | - (CGRect) calculateImageBoundsWithImage:(UIImage *)image { 103 | CGFloat smallestSize; 104 | CGFloat insetX = 0.0f; 105 | CGFloat insetY = 0.0f; 106 | if (image.size.width > image.size.height) { 107 | smallestSize = image.size.height; 108 | insetX = image.size.width - smallestSize; 109 | } 110 | else { 111 | smallestSize = image.size.width; 112 | insetY = image.size.height - smallestSize; 113 | } 114 | 115 | return CGRectMake(insetX, insetY, smallestSize, smallestSize); 116 | } 117 | 118 | - (UIImage *) createCroppedImageWithImage:(UIImage *)image { 119 | CGRect imageBounds = [self calculateImageBoundsWithImage:image]; 120 | UIGraphicsBeginImageContext(imageBounds.size); 121 | [[UIBezierPath bezierPathWithRoundedRect:CGRectMake(0.0f, 0.0f, imageBounds.size.width, imageBounds.size.height) 122 | cornerRadius:imageBounds.size.width / 8.0f] addClip]; 123 | [image drawAtPoint:CGPointMake(-imageBounds.origin.x, -imageBounds.origin.y)]; 124 | UIImage *finalImage = UIGraphicsGetImageFromCurrentImageContext(); 125 | UIGraphicsEndImageContext(); 126 | 127 | return finalImage; 128 | } 129 | 130 | - (IBAction) addText:(id)sender { 131 | [self.textView attachData:[[AttachmentData alloc] initWithIdentifier:[NSString stringWithFormat:@"%lu", (unsigned long)_identifierCounter++] 132 | image:[self createImageWithText:@"Test" color:[UIColor redColor]]]]; 133 | } 134 | 135 | - (IBAction) addImage:(id)sender { 136 | [self.textView attachData:[[AttachmentData alloc] initWithIdentifier:[NSString stringWithFormat:@"%lu", (unsigned long)_identifierCounter++] 137 | image:[self scaleImage:[self createCroppedImageWithImage:[UIImage imageNamed:@"MadisonSquarePark.jpg"]] 138 | toSize:CGSizeMake(100.0f, 100.0f)]]]; 139 | } 140 | 141 | - (void) pasteImage:(UIImage *)image { 142 | [self.textView attachData:[[AttachmentData alloc] initWithIdentifier:[NSString stringWithFormat:@"%lu", (unsigned long)_identifierCounter++] 143 | image:[self scaleImage:[self createCroppedImageWithImage:image] toSize:CGSizeMake(100.0f, 100.0f)]]]; 144 | } 145 | 146 | - (void) didAddAttachmentData:(id )data { 147 | [self.textView animateData:data withAnimation:^(UIImageView *imageView) { 148 | CABasicAnimation *scaleAnimation = [CABasicAnimation animationWithKeyPath:@"transform.scale"]; 149 | scaleAnimation.duration = 0.1; 150 | scaleAnimation.repeatCount = 0; 151 | scaleAnimation.autoreverses = NO; 152 | scaleAnimation.fromValue = @0.1f; 153 | scaleAnimation.toValue = @1.0f; 154 | [imageView.layer addAnimation:scaleAnimation forKey:@"scale"]; 155 | }]; 156 | } 157 | 158 | - (void) didInteractWithAttachmentData:(id )data { 159 | [self.textView animateData:data withAnimation:^(UIImageView *imageView) { 160 | CABasicAnimation *rotate = [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"]; 161 | rotate.fromValue = @(0.0); 162 | rotate.toValue = @(M_PI * 2.0); 163 | rotate.repeatCount = 0; 164 | rotate.duration = 0.5; 165 | [imageView.layer addAnimation:rotate forKey:@"rotate"]; 166 | }]; 167 | } 168 | 169 | @end -------------------------------------------------------------------------------- /Demo/Base.lproj/Main.storyboard: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 47 | 60 | 61 | 62 | 63 | 64 | 65 | 66 | 67 | 68 | 69 | 70 | 71 | 72 | 73 | 74 | 75 | 76 | 77 | 78 | 79 | 80 | 81 | 82 | 83 | 84 | 85 | 86 | 87 | 88 | 89 | 90 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Apache License 2 | Version 2.0, January 2004 3 | http://www.apache.org/licenses/ 4 | 5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 6 | 7 | 1. Definitions. 8 | 9 | "License" shall mean the terms and conditions for use, reproduction, 10 | and distribution as defined by Sections 1 through 9 of this document. 11 | 12 | "Licensor" shall mean the copyright owner or entity authorized by 13 | the copyright owner that is granting the License. 14 | 15 | "Legal Entity" shall mean the union of the acting entity and all 16 | other entities that control, are controlled by, or are under common 17 | control with that entity. For the purposes of this definition, 18 | "control" means (i) the power, direct or indirect, to cause the 19 | direction or management of such entity, whether by contract or 20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 21 | outstanding shares, or (iii) beneficial ownership of such entity. 22 | 23 | "You" (or "Your") shall mean an individual or Legal Entity 24 | exercising permissions granted by this License. 25 | 26 | "Source" form shall mean the preferred form for making modifications, 27 | including but not limited to software source code, documentation 28 | source, and configuration files. 29 | 30 | "Object" form shall mean any form resulting from mechanical 31 | transformation or translation of a Source form, including but 32 | not limited to compiled object code, generated documentation, 33 | and conversions to other media types. 34 | 35 | "Work" shall mean the work of authorship, whether in Source or 36 | Object form, made available under the License, as indicated by a 37 | copyright notice that is included in or attached to the work 38 | (an example is provided in the Appendix below). 39 | 40 | "Derivative Works" shall mean any work, whether in Source or Object 41 | form, that is based on (or derived from) the Work and for which the 42 | editorial revisions, annotations, elaborations, or other modifications 43 | represent, as a whole, an original work of authorship. For the purposes 44 | of this License, Derivative Works shall not include works that remain 45 | separable from, or merely link (or bind by name) to the interfaces of, 46 | the Work and Derivative Works thereof. 47 | 48 | "Contribution" shall mean any work of authorship, including 49 | the original version of the Work and any modifications or additions 50 | to that Work or Derivative Works thereof, that is intentionally 51 | submitted to Licensor for inclusion in the Work by the copyright owner 52 | or by an individual or Legal Entity authorized to submit on behalf of 53 | the copyright owner. For the purposes of this definition, "submitted" 54 | means any form of electronic, verbal, or written communication sent 55 | to the Licensor or its representatives, including but not limited to 56 | communication on electronic mailing lists, source code control systems, 57 | and issue tracking systems that are managed by, or on behalf of, the 58 | Licensor for the purpose of discussing and improving the Work, but 59 | excluding communication that is conspicuously marked or otherwise 60 | designated in writing by the copyright owner as "Not a Contribution." 61 | 62 | "Contributor" shall mean Licensor and any individual or Legal Entity 63 | on behalf of whom a Contribution has been received by Licensor and 64 | subsequently incorporated within the Work. 65 | 66 | 2. Grant of Copyright License. Subject to the terms and conditions of 67 | this License, each Contributor hereby grants to You a perpetual, 68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 69 | copyright license to reproduce, prepare Derivative Works of, 70 | publicly display, publicly perform, sublicense, and distribute the 71 | Work and such Derivative Works in Source or Object form. 72 | 73 | 3. Grant of Patent License. Subject to the terms and conditions of 74 | this License, each Contributor hereby grants to You a perpetual, 75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 76 | (except as stated in this section) patent license to make, have made, 77 | use, offer to sell, sell, import, and otherwise transfer the Work, 78 | where such license applies only to those patent claims licensable 79 | by such Contributor that are necessarily infringed by their 80 | Contribution(s) alone or by combination of their Contribution(s) 81 | with the Work to which such Contribution(s) was submitted. If You 82 | institute patent litigation against any entity (including a 83 | cross-claim or counterclaim in a lawsuit) alleging that the Work 84 | or a Contribution incorporated within the Work constitutes direct 85 | or contributory patent infringement, then any patent licenses 86 | granted to You under this License for that Work shall terminate 87 | as of the date such litigation is filed. 88 | 89 | 4. Redistribution. You may reproduce and distribute copies of the 90 | Work or Derivative Works thereof in any medium, with or without 91 | modifications, and in Source or Object form, provided that You 92 | meet the following conditions: 93 | 94 | (a) You must give any other recipients of the Work or 95 | Derivative Works a copy of this License; and 96 | 97 | (b) You must cause any modified files to carry prominent notices 98 | stating that You changed the files; and 99 | 100 | (c) You must retain, in the Source form of any Derivative Works 101 | that You distribute, all copyright, patent, trademark, and 102 | attribution notices from the Source form of the Work, 103 | excluding those notices that do not pertain to any part of 104 | the Derivative Works; and 105 | 106 | (d) If the Work includes a "NOTICE" text file as part of its 107 | distribution, then any Derivative Works that You distribute must 108 | include a readable copy of the attribution notices contained 109 | within such NOTICE file, excluding those notices that do not 110 | pertain to any part of the Derivative Works, in at least one 111 | of the following places: within a NOTICE text file distributed 112 | as part of the Derivative Works; within the Source form or 113 | documentation, if provided along with the Derivative Works; or, 114 | within a display generated by the Derivative Works, if and 115 | wherever such third-party notices normally appear. The contents 116 | of the NOTICE file are for informational purposes only and 117 | do not modify the License. You may add Your own attribution 118 | notices within Derivative Works that You distribute, alongside 119 | or as an addendum to the NOTICE text from the Work, provided 120 | that such additional attribution notices cannot be construed 121 | as modifying the License. 122 | 123 | You may add Your own copyright statement to Your modifications and 124 | may provide additional or different license terms and conditions 125 | for use, reproduction, or distribution of Your modifications, or 126 | for any such Derivative Works as a whole, provided Your use, 127 | reproduction, and distribution of the Work otherwise complies with 128 | the conditions stated in this License. 129 | 130 | 5. Submission of Contributions. Unless You explicitly state otherwise, 131 | any Contribution intentionally submitted for inclusion in the Work 132 | by You to the Licensor shall be under the terms and conditions of 133 | this License, without any additional terms or conditions. 134 | Notwithstanding the above, nothing herein shall supersede or modify 135 | the terms of any separate license agreement you may have executed 136 | with Licensor regarding such Contributions. 137 | 138 | 6. Trademarks. This License does not grant permission to use the trade 139 | names, trademarks, service marks, or product names of the Licensor, 140 | except as required for reasonable and customary use in describing the 141 | origin of the Work and reproducing the content of the NOTICE file. 142 | 143 | 7. Disclaimer of Warranty. Unless required by applicable law or 144 | agreed to in writing, Licensor provides the Work (and each 145 | Contributor provides its Contributions) on an "AS IS" BASIS, 146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 147 | implied, including, without limitation, any warranties or conditions 148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 149 | PARTICULAR PURPOSE. You are solely responsible for determining the 150 | appropriateness of using or redistributing the Work and assume any 151 | risks associated with Your exercise of permissions under this License. 152 | 153 | 8. Limitation of Liability. In no event and under no legal theory, 154 | whether in tort (including negligence), contract, or otherwise, 155 | unless required by applicable law (such as deliberate and grossly 156 | negligent acts) or agreed to in writing, shall any Contributor be 157 | liable to You for damages, including any direct, indirect, special, 158 | incidental, or consequential damages of any character arising as a 159 | result of this License or out of the use or inability to use the 160 | Work (including but not limited to damages for loss of goodwill, 161 | work stoppage, computer failure or malfunction, or any and all 162 | other commercial damages or losses), even if such Contributor 163 | has been advised of the possibility of such damages. 164 | 165 | 9. Accepting Warranty or Additional Liability. While redistributing 166 | the Work or Derivative Works thereof, You may choose to offer, 167 | and charge a fee for, acceptance of support, warranty, indemnity, 168 | or other liability obligations and/or rights consistent with this 169 | License. However, in accepting such obligations, You may act only 170 | on Your own behalf and on Your sole responsibility, not on behalf 171 | of any other Contributor, and only if You agree to indemnify, 172 | defend, and hold each Contributor harmless for any liability 173 | incurred by, or claims asserted against, such Contributor by reason 174 | of your accepting any such warranty or additional liability. 175 | 176 | END OF TERMS AND CONDITIONS 177 | 178 | APPENDIX: How to apply the Apache License to your work. 179 | 180 | To apply the Apache License to your work, attach the following 181 | boilerplate notice, with the fields enclosed by brackets "{}" 182 | replaced with your own identifying information. (Don't include 183 | the brackets!) The text should be enclosed in the appropriate 184 | comment syntax for the file format. We also recommend that a 185 | file or class name and description of purpose be included on the 186 | same "printed page" as the copyright notice for easier 187 | identification within third-party archives. 188 | 189 | Copyright {yyyy} {name of copyright owner} 190 | 191 | Licensed under the Apache License, Version 2.0 (the "License"); 192 | you may not use this file except in compliance with the License. 193 | You may obtain a copy of the License at 194 | 195 | http://www.apache.org/licenses/LICENSE-2.0 196 | 197 | Unless required by applicable law or agreed to in writing, software 198 | distributed under the License is distributed on an "AS IS" BASIS, 199 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 200 | See the License for the specific language governing permissions and 201 | limitations under the License. 202 | 203 | -------------------------------------------------------------------------------- /TMAnimatedTextView.xcodeproj/project.pbxproj: -------------------------------------------------------------------------------- 1 | // !$*UTF8*$! 2 | { 3 | archiveVersion = 1; 4 | classes = { 5 | }; 6 | objectVersion = 46; 7 | objects = { 8 | 9 | /* Begin PBXBuildFile section */ 10 | B4DCC3171AD5CFF300C5913D /* Images.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = B4DCC3161AD5CFF300C5913D /* Images.xcassets */; }; 11 | DDFF82C5C3090577F35816DC /* main.m in Sources */ = {isa = PBXBuildFile; fileRef = DDFF8B785FEB78DDF35E9973 /* main.m */; }; 12 | DDFF87E9014A412F799615BF /* TMAnimatedTextView.m in Sources */ = {isa = PBXBuildFile; fileRef = DDFF81704C0E3F19087BC554 /* TMAnimatedTextView.m */; }; 13 | DDFF88A19C9A8ED366F62CB9 /* ViewController.m in Sources */ = {isa = PBXBuildFile; fileRef = DDFF8DAA9F3797DBEAA10F22 /* ViewController.m */; }; 14 | DDFF88E41F67E55EED9E9139 /* Info.plist in Resources */ = {isa = PBXBuildFile; fileRef = DDFF850BB4053BF26ABE509D /* Info.plist */; }; 15 | DDFF8CFFBAECC079AEABA1F2 /* LaunchScreen.xib in Resources */ = {isa = PBXBuildFile; fileRef = DDFF818FC98E1E0FDD7F5721 /* LaunchScreen.xib */; }; 16 | DDFF8D66BA1C00D46CF5C939 /* Main.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = DDFF83515CD5B343165FED24 /* Main.storyboard */; }; 17 | DDFF8F8CF6FC558CBB327FE2 /* AppDelegate.m in Sources */ = {isa = PBXBuildFile; fileRef = DDFF85759A48CFD644EDAD46 /* AppDelegate.m */; }; 18 | /* End PBXBuildFile section */ 19 | 20 | /* Begin PBXFileReference section */ 21 | B4DCC3161AD5CFF300C5913D /* Images.xcassets */ = {isa = PBXFileReference; lastKnownFileType = folder.assetcatalog; name = Images.xcassets; path = TMAnimatedTextView/Images.xcassets; sourceTree = ""; }; 22 | DDFF81704C0E3F19087BC554 /* TMAnimatedTextView.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = TMAnimatedTextView.m; sourceTree = ""; }; 23 | DDFF81F34A1F280D50D548FB /* Base */ = {isa = PBXFileReference; lastKnownFileType = file.storyboard; name = Base; path = Main.storyboard; sourceTree = ""; }; 24 | DDFF848DFF8B614E0F6A262F /* TMAnimatedTextView.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = TMAnimatedTextView.app; sourceTree = BUILT_PRODUCTS_DIR; }; 25 | DDFF84A6900C4202D3D94077 /* TMAnimatedTextView.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = TMAnimatedTextView.h; sourceTree = ""; }; 26 | DDFF850BB4053BF26ABE509D /* Info.plist */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.plist.info; path = Info.plist; sourceTree = ""; }; 27 | DDFF8555138F693B4C7D0F79 /* Base */ = {isa = PBXFileReference; lastKnownFileType = file.xib; name = Base; path = LaunchScreen.xib; sourceTree = ""; }; 28 | DDFF85759A48CFD644EDAD46 /* AppDelegate.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = AppDelegate.m; sourceTree = ""; }; 29 | DDFF8B10AA0E17ACBF20D736 /* ViewController.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = ViewController.h; sourceTree = ""; }; 30 | DDFF8B785FEB78DDF35E9973 /* main.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = main.m; sourceTree = ""; }; 31 | DDFF8DAA9F3797DBEAA10F22 /* ViewController.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = ViewController.m; sourceTree = ""; }; 32 | DDFF8E25AF94F699EFFE7550 /* AppDelegate.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = AppDelegate.h; sourceTree = ""; }; 33 | /* End PBXFileReference section */ 34 | 35 | /* Begin PBXFrameworksBuildPhase section */ 36 | DDFF899EB3611F41DE70C090 /* Frameworks */ = { 37 | isa = PBXFrameworksBuildPhase; 38 | buildActionMask = 2147483647; 39 | files = ( 40 | ); 41 | runOnlyForDeploymentPostprocessing = 0; 42 | }; 43 | /* End PBXFrameworksBuildPhase section */ 44 | 45 | /* Begin PBXGroup section */ 46 | DDFF87264A7451D3C53C5FC0 /* Base.lproj */ = { 47 | isa = PBXGroup; 48 | children = ( 49 | DDFF83515CD5B343165FED24 /* Main.storyboard */, 50 | DDFF818FC98E1E0FDD7F5721 /* LaunchScreen.xib */, 51 | ); 52 | path = Base.lproj; 53 | sourceTree = ""; 54 | }; 55 | DDFF87395F5C7FD943F9827E /* Demo */ = { 56 | isa = PBXGroup; 57 | children = ( 58 | DDFF8B785FEB78DDF35E9973 /* main.m */, 59 | DDFF87264A7451D3C53C5FC0 /* Base.lproj */, 60 | DDFF850BB4053BF26ABE509D /* Info.plist */, 61 | DDFF8E25AF94F699EFFE7550 /* AppDelegate.h */, 62 | DDFF85759A48CFD644EDAD46 /* AppDelegate.m */, 63 | DDFF8B10AA0E17ACBF20D736 /* ViewController.h */, 64 | DDFF8DAA9F3797DBEAA10F22 /* ViewController.m */, 65 | ); 66 | path = Demo; 67 | sourceTree = ""; 68 | }; 69 | DDFF897CC9B544561E29DDB8 /* TMAnimatedTextView */ = { 70 | isa = PBXGroup; 71 | children = ( 72 | DDFF84A6900C4202D3D94077 /* TMAnimatedTextView.h */, 73 | DDFF81704C0E3F19087BC554 /* TMAnimatedTextView.m */, 74 | ); 75 | path = TMAnimatedTextView; 76 | sourceTree = ""; 77 | }; 78 | DDFF8C2FE6514A586DB21865 /* Products */ = { 79 | isa = PBXGroup; 80 | children = ( 81 | DDFF848DFF8B614E0F6A262F /* TMAnimatedTextView.app */, 82 | ); 83 | name = Products; 84 | sourceTree = ""; 85 | }; 86 | DDFF8DF4A46913E972768F84 = { 87 | isa = PBXGroup; 88 | children = ( 89 | DDFF8C2FE6514A586DB21865 /* Products */, 90 | DDFF87395F5C7FD943F9827E /* Demo */, 91 | DDFF897CC9B544561E29DDB8 /* TMAnimatedTextView */, 92 | B4DCC3161AD5CFF300C5913D /* Images.xcassets */, 93 | ); 94 | sourceTree = ""; 95 | }; 96 | /* End PBXGroup section */ 97 | 98 | /* Begin PBXNativeTarget section */ 99 | DDFF81DCB2493F53A89181D3 /* TMAnimatedTextView */ = { 100 | isa = PBXNativeTarget; 101 | buildConfigurationList = DDFF848BDA0A733FAA033D2D /* Build configuration list for PBXNativeTarget "TMAnimatedTextView" */; 102 | buildPhases = ( 103 | DDFF82022066BAD24E4116B1 /* Sources */, 104 | DDFF899EB3611F41DE70C090 /* Frameworks */, 105 | DDFF8650C89CFF7108351EDE /* Resources */, 106 | ); 107 | buildRules = ( 108 | ); 109 | dependencies = ( 110 | ); 111 | name = TMAnimatedTextView; 112 | productName = TMAnimatedTextView; 113 | productReference = DDFF848DFF8B614E0F6A262F /* TMAnimatedTextView.app */; 114 | productType = "com.apple.product-type.application"; 115 | }; 116 | /* End PBXNativeTarget section */ 117 | 118 | /* Begin PBXProject section */ 119 | DDFF8ED699F0DFB3A8DD4C9E /* Project object */ = { 120 | isa = PBXProject; 121 | attributes = { 122 | ORGANIZATIONNAME = tapmesh; 123 | }; 124 | buildConfigurationList = DDFF882F87D703B98B2F1842 /* Build configuration list for PBXProject "TMAnimatedTextView" */; 125 | compatibilityVersion = "Xcode 3.2"; 126 | developmentRegion = English; 127 | hasScannedForEncodings = 0; 128 | knownRegions = ( 129 | en, 130 | ); 131 | mainGroup = DDFF8DF4A46913E972768F84; 132 | productRefGroup = DDFF8C2FE6514A586DB21865 /* Products */; 133 | projectDirPath = ""; 134 | projectRoot = ""; 135 | targets = ( 136 | DDFF81DCB2493F53A89181D3 /* TMAnimatedTextView */, 137 | ); 138 | }; 139 | /* End PBXProject section */ 140 | 141 | /* Begin PBXResourcesBuildPhase section */ 142 | DDFF8650C89CFF7108351EDE /* Resources */ = { 143 | isa = PBXResourcesBuildPhase; 144 | buildActionMask = 2147483647; 145 | files = ( 146 | DDFF8D66BA1C00D46CF5C939 /* Main.storyboard in Resources */, 147 | DDFF8CFFBAECC079AEABA1F2 /* LaunchScreen.xib in Resources */, 148 | B4DCC3171AD5CFF300C5913D /* Images.xcassets in Resources */, 149 | DDFF88E41F67E55EED9E9139 /* Info.plist in Resources */, 150 | ); 151 | runOnlyForDeploymentPostprocessing = 0; 152 | }; 153 | /* End PBXResourcesBuildPhase section */ 154 | 155 | /* Begin PBXSourcesBuildPhase section */ 156 | DDFF82022066BAD24E4116B1 /* Sources */ = { 157 | isa = PBXSourcesBuildPhase; 158 | buildActionMask = 2147483647; 159 | files = ( 160 | DDFF82C5C3090577F35816DC /* main.m in Sources */, 161 | DDFF8F8CF6FC558CBB327FE2 /* AppDelegate.m in Sources */, 162 | DDFF88A19C9A8ED366F62CB9 /* ViewController.m in Sources */, 163 | DDFF87E9014A412F799615BF /* TMAnimatedTextView.m in Sources */, 164 | ); 165 | runOnlyForDeploymentPostprocessing = 0; 166 | }; 167 | /* End PBXSourcesBuildPhase section */ 168 | 169 | /* Begin PBXVariantGroup section */ 170 | DDFF818FC98E1E0FDD7F5721 /* LaunchScreen.xib */ = { 171 | isa = PBXVariantGroup; 172 | children = ( 173 | DDFF8555138F693B4C7D0F79 /* Base */, 174 | ); 175 | name = LaunchScreen.xib; 176 | sourceTree = ""; 177 | }; 178 | DDFF83515CD5B343165FED24 /* Main.storyboard */ = { 179 | isa = PBXVariantGroup; 180 | children = ( 181 | DDFF81F34A1F280D50D548FB /* Base */, 182 | ); 183 | name = Main.storyboard; 184 | sourceTree = ""; 185 | }; 186 | /* End PBXVariantGroup section */ 187 | 188 | /* Begin XCBuildConfiguration section */ 189 | DDFF8370DBD7E7E21C9603E3 /* Release */ = { 190 | isa = XCBuildConfiguration; 191 | buildSettings = { 192 | ALWAYS_SEARCH_USER_PATHS = NO; 193 | CLANG_CXX_LANGUAGE_STANDARD = "gnu++0x"; 194 | CLANG_CXX_LIBRARY = "libc++"; 195 | CLANG_ENABLE_MODULES = YES; 196 | CLANG_ENABLE_OBJC_ARC = YES; 197 | CLANG_WARN_BOOL_CONVERSION = YES; 198 | CLANG_WARN_CONSTANT_CONVERSION = YES; 199 | CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR; 200 | CLANG_WARN_EMPTY_BODY = YES; 201 | CLANG_WARN_ENUM_CONVERSION = YES; 202 | CLANG_WARN_INT_CONVERSION = YES; 203 | CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR; 204 | CLANG_WARN_UNREACHABLE_CODE = YES; 205 | CLANG_WARN__DUPLICATE_METHOD_MATCH = YES; 206 | "CODE_SIGN_IDENTITY[sdk=iphoneos*]" = "iPhone Developer"; 207 | COPY_PHASE_STRIP = NO; 208 | ENABLE_NS_ASSERTIONS = NO; 209 | ENABLE_STRICT_OBJC_MSGSEND = YES; 210 | GCC_C_LANGUAGE_STANDARD = gnu99; 211 | GCC_WARN_64_TO_32_BIT_CONVERSION = YES; 212 | GCC_WARN_ABOUT_RETURN_TYPE = YES_ERROR; 213 | GCC_WARN_UNDECLARED_SELECTOR = YES; 214 | GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE; 215 | GCC_WARN_UNUSED_FUNCTION = YES; 216 | GCC_WARN_UNUSED_VARIABLE = YES; 217 | IPHONEOS_DEPLOYMENT_TARGET = 8.2; 218 | MTL_ENABLE_DEBUG_INFO = NO; 219 | SDKROOT = iphoneos; 220 | TARGETED_DEVICE_FAMILY = "1,2"; 221 | VALIDATE_PRODUCT = YES; 222 | }; 223 | name = Release; 224 | }; 225 | DDFF8429BDD48ABBB64887CB /* Debug */ = { 226 | isa = XCBuildConfiguration; 227 | buildSettings = { 228 | ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon; 229 | ASSETCATALOG_COMPILER_LAUNCHIMAGE_NAME = LaunchImage; 230 | INFOPLIST_FILE = "$(SRCROOT)/Demo/Info.plist"; 231 | LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks"; 232 | PRODUCT_NAME = "$(TARGET_NAME)"; 233 | }; 234 | name = Debug; 235 | }; 236 | DDFF8E3EC9D6FE709279CE6E /* Debug */ = { 237 | isa = XCBuildConfiguration; 238 | buildSettings = { 239 | ALWAYS_SEARCH_USER_PATHS = NO; 240 | CLANG_CXX_LANGUAGE_STANDARD = "gnu++0x"; 241 | CLANG_CXX_LIBRARY = "libc++"; 242 | CLANG_ENABLE_MODULES = YES; 243 | CLANG_ENABLE_OBJC_ARC = YES; 244 | CLANG_WARN_BOOL_CONVERSION = YES; 245 | CLANG_WARN_CONSTANT_CONVERSION = YES; 246 | CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR; 247 | CLANG_WARN_EMPTY_BODY = YES; 248 | CLANG_WARN_ENUM_CONVERSION = YES; 249 | CLANG_WARN_INT_CONVERSION = YES; 250 | CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR; 251 | CLANG_WARN_UNREACHABLE_CODE = YES; 252 | CLANG_WARN__DUPLICATE_METHOD_MATCH = YES; 253 | "CODE_SIGN_IDENTITY[sdk=iphoneos*]" = "iPhone Developer"; 254 | COPY_PHASE_STRIP = NO; 255 | ENABLE_STRICT_OBJC_MSGSEND = YES; 256 | GCC_C_LANGUAGE_STANDARD = gnu99; 257 | GCC_DYNAMIC_NO_PIC = NO; 258 | GCC_OPTIMIZATION_LEVEL = 0; 259 | GCC_PREPROCESSOR_DEFINITIONS = ( 260 | "DEBUG=1", 261 | "$(inherited)", 262 | ); 263 | GCC_SYMBOLS_PRIVATE_EXTERN = NO; 264 | GCC_WARN_64_TO_32_BIT_CONVERSION = YES; 265 | GCC_WARN_ABOUT_RETURN_TYPE = YES_ERROR; 266 | GCC_WARN_UNDECLARED_SELECTOR = YES; 267 | GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE; 268 | GCC_WARN_UNUSED_FUNCTION = YES; 269 | GCC_WARN_UNUSED_VARIABLE = YES; 270 | IPHONEOS_DEPLOYMENT_TARGET = 8.2; 271 | MTL_ENABLE_DEBUG_INFO = YES; 272 | ONLY_ACTIVE_ARCH = YES; 273 | SDKROOT = iphoneos; 274 | TARGETED_DEVICE_FAMILY = "1,2"; 275 | }; 276 | name = Debug; 277 | }; 278 | DDFF8F0F04BF841A4430EBF4 /* Release */ = { 279 | isa = XCBuildConfiguration; 280 | buildSettings = { 281 | ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon; 282 | ASSETCATALOG_COMPILER_LAUNCHIMAGE_NAME = LaunchImage; 283 | INFOPLIST_FILE = "$(SRCROOT)/Demo/Info.plist"; 284 | LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks"; 285 | PRODUCT_NAME = "$(TARGET_NAME)"; 286 | }; 287 | name = Release; 288 | }; 289 | /* End XCBuildConfiguration section */ 290 | 291 | /* Begin XCConfigurationList section */ 292 | DDFF848BDA0A733FAA033D2D /* Build configuration list for PBXNativeTarget "TMAnimatedTextView" */ = { 293 | isa = XCConfigurationList; 294 | buildConfigurations = ( 295 | DDFF8429BDD48ABBB64887CB /* Debug */, 296 | DDFF8F0F04BF841A4430EBF4 /* Release */, 297 | ); 298 | defaultConfigurationIsVisible = 0; 299 | defaultConfigurationName = Release; 300 | }; 301 | DDFF882F87D703B98B2F1842 /* Build configuration list for PBXProject "TMAnimatedTextView" */ = { 302 | isa = XCConfigurationList; 303 | buildConfigurations = ( 304 | DDFF8E3EC9D6FE709279CE6E /* Debug */, 305 | DDFF8370DBD7E7E21C9603E3 /* Release */, 306 | ); 307 | defaultConfigurationIsVisible = 0; 308 | defaultConfigurationName = Release; 309 | }; 310 | /* End XCConfigurationList section */ 311 | }; 312 | rootObject = DDFF8ED699F0DFB3A8DD4C9E /* Project object */; 313 | } 314 | -------------------------------------------------------------------------------- /TMAnimatedTextView/TMAnimatedTextView.m: -------------------------------------------------------------------------------- 1 | // 2 | // Created by Bob Wieler on 9/22/14. 3 | // Copyright (c) 2015 TapMesh, Inc. All rights reserved. 4 | // 5 | // Licensed under the Apache License, Version 2.0 (the "License"); 6 | // you may not use this file except in compliance with the License. 7 | // You may obtain a copy of the License at 8 | // 9 | // http://www.apache.org/licenses/LICENSE-2.0 10 | // 11 | // Unless required by applicable law or agreed to in writing, software 12 | // distributed under the License is distributed on an "AS IS" BASIS, 13 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | // See the License for the specific language governing permissions and 15 | // limitations under the License. 16 | 17 | #import "TMAnimatedTextView.h" 18 | 19 | #pragma mark - TMAnimatedTextView (Private) 20 | 21 | @interface TMAnimatedTextView () 22 | 23 | - (BOOL) attachmentExistsWithIdentifier:(NSString *)identifier; 24 | - (CGRect) frameOfIdentifier:(NSString *)identifier; 25 | - (id ) attachmentDataAtPoint:(CGPoint)point; 26 | 27 | @end 28 | 29 | #pragma mark - TMTextAttachment (Interface) 30 | 31 | @interface TMTextAttachment : NSTextAttachment 32 | 33 | @property(nonatomic, readonly) CGSize expectedSize; 34 | @property(nonatomic, readonly) id data; 35 | 36 | - (instancetype) initWithAttachmentData:(id )data bounds:(CGRect)bounds; 37 | 38 | @end 39 | 40 | #pragma mark - TMTextAttachment (Implementation) 41 | 42 | @implementation TMTextAttachment { 43 | @private 44 | CGSize _expectedSize; 45 | id _data; 46 | } 47 | 48 | @synthesize expectedSize = _expectedSize; 49 | @synthesize data = _data; 50 | 51 | - (instancetype) initWithAttachmentData:(id )data bounds:(CGRect)bounds { 52 | self = [super init]; 53 | if (self) { 54 | _data = data; 55 | _expectedSize = bounds.size; 56 | self.bounds = bounds; 57 | } 58 | return self; 59 | } 60 | 61 | @end 62 | 63 | #pragma mark - Overview 64 | 65 | @interface TMAnimatedTextViewOverview : UIView 66 | 67 | @property(nonatomic, weak) TMAnimatedTextView *textView; 68 | 69 | - (instancetype) initWithFrame:(CGRect)frame chatTextView:(TMAnimatedTextView *)textView; 70 | 71 | - (void) registerData:(id )data actualImage:(UIImage *)image; 72 | - (void) unregisterData:(id )data; 73 | 74 | - (void) animateData:(id )data withAnimation:(void (^)(UIImageView *imageView))animationBlock; 75 | 76 | @end 77 | 78 | @implementation TMAnimatedTextViewOverview { 79 | @private 80 | NSMutableDictionary *_attachmentViews; 81 | } 82 | 83 | - (instancetype) initWithFrame:(CGRect)frame chatTextView:(TMAnimatedTextView *)textView { 84 | self = [super initWithFrame:frame]; 85 | if (self) { 86 | self.textView = textView; 87 | _attachmentViews = [NSMutableDictionary dictionary]; 88 | } 89 | 90 | return self; 91 | } 92 | 93 | - (void) calculateAll { 94 | NSMutableArray *cardIdsToRemove = [NSMutableArray array]; 95 | for (NSString *identifier in _attachmentViews.keyEnumerator) { 96 | UIImageView *imageView = _attachmentViews[identifier]; 97 | 98 | if ([self.textView attachmentExistsWithIdentifier:identifier]) { 99 | imageView.frame = [self.textView frameOfIdentifier:identifier]; 100 | } 101 | else { 102 | [cardIdsToRemove addObject:identifier]; 103 | [imageView stopAnimating]; 104 | [imageView removeFromSuperview]; 105 | } 106 | } 107 | [_attachmentViews removeObjectsForKeys:cardIdsToRemove]; 108 | } 109 | 110 | - (void) registerData:(id )data actualImage:(UIImage *)image { 111 | UIImageView *imageView = [[UIImageView alloc] initWithImage:image]; 112 | [self addSubview:imageView]; 113 | _attachmentViews[data.identifier] = imageView; 114 | } 115 | 116 | - (void) unregisterData:(id )data { 117 | UIImageView *existingImageView = _attachmentViews[data.identifier]; 118 | if (existingImageView) { 119 | [existingImageView removeFromSuperview]; 120 | [existingImageView stopAnimating]; 121 | [_attachmentViews removeObjectForKey:data.identifier]; 122 | } 123 | } 124 | 125 | - (void) animateData:(id )data withAnimation:(void (^)(UIImageView *imageView))animationBlock { 126 | UIImageView *imageView = _attachmentViews[data.identifier]; 127 | animationBlock(imageView); 128 | } 129 | 130 | @end 131 | 132 | #pragma mark - ChatTextView 133 | 134 | @implementation TMAnimatedTextView { 135 | @private 136 | NSLayoutConstraint *_heightConstraint; 137 | UITapGestureRecognizer *_tapGestureRecognizer; 138 | __weak id _animatedTextViewDelegate; 139 | TMAnimatedTextViewOverview *_overview; 140 | } 141 | 142 | @synthesize animatedTextViewDelegate = _animatedTextViewDelegate; 143 | 144 | - (instancetype) initWithFrame:(CGRect)frame { 145 | self = [super initWithFrame:frame]; 146 | if (self) { 147 | [self commonInitializer]; 148 | } 149 | 150 | return self; 151 | } 152 | 153 | - (instancetype) initWithCoder:(NSCoder *)aDecoder { 154 | self = [super initWithCoder:aDecoder]; 155 | if (self) { 156 | [self commonInitializer]; 157 | } 158 | 159 | return self; 160 | } 161 | 162 | - (void) commonInitializer { 163 | for (NSLayoutConstraint *constraint in self.constraints) { 164 | if (constraint.firstAttribute == NSLayoutAttributeHeight) { 165 | _heightConstraint = constraint; 166 | break; 167 | } 168 | } 169 | 170 | if (!_heightConstraint) 171 | [NSException raise:NSInternalInconsistencyException format:@"Animated text view requires a height constraint"]; 172 | 173 | self.layer.borderColor = [[[UIColor grayColor] colorWithAlphaComponent:0.5] CGColor]; 174 | self.layer.borderWidth = 1.0f; 175 | self.layer.cornerRadius = 5; 176 | self.layer.masksToBounds = YES; 177 | 178 | self.font = [UIFont systemFontOfSize:14]; 179 | self.typingAttributes = @{ 180 | NSFontAttributeName : self.font 181 | }; 182 | 183 | self.delegate = self; 184 | 185 | _tapGestureRecognizer = [[UITapGestureRecognizer alloc] 186 | initWithTarget:self action:@selector(handleTextViewTapGestureRecognizer:)]; 187 | _tapGestureRecognizer.delegate = self; 188 | _tapGestureRecognizer.numberOfTouchesRequired = 1; 189 | _tapGestureRecognizer.numberOfTapsRequired = 1; 190 | [self addGestureRecognizer:_tapGestureRecognizer]; 191 | 192 | _overview = [[TMAnimatedTextViewOverview alloc] initWithFrame:CGRectMake(0, 0, 400, 400) chatTextView:self]; 193 | _overview.backgroundColor = [UIColor clearColor]; 194 | _overview.userInteractionEnabled = NO; 195 | [self addSubview:_overview]; 196 | } 197 | 198 | - (void) scrollViewDidEndScrollingAnimation:(UIScrollView *)scrollView { 199 | 200 | } 201 | 202 | - (void) setBounds:(CGRect)bounds { 203 | BOOL changed = NO; 204 | CGSize currentSize = self.bounds.size; 205 | CGSize newSize = bounds.size; 206 | if (newSize.height != currentSize.height || newSize.width != currentSize.width) { 207 | changed = YES; 208 | if (_animatedTextViewDelegate && [_animatedTextViewDelegate respondsToSelector:@selector(viewBoundsWillChangeFrom:to:)]) { 209 | [_animatedTextViewDelegate viewBoundsWillChangeFrom:CGRectMake(0, 0, currentSize.width, currentSize.height) 210 | to:CGRectMake(0, 0, newSize.width, newSize.height)]; 211 | } 212 | } 213 | [super setBounds:bounds]; 214 | if (changed) { 215 | if (_animatedTextViewDelegate && [_animatedTextViewDelegate respondsToSelector:@selector(viewBoundsDidChangeFrom:to:)]) { 216 | [_animatedTextViewDelegate viewBoundsDidChangeFrom:CGRectMake(0, 0, currentSize.width, currentSize.height) 217 | to:CGRectMake(0, 0, newSize.width, newSize.height)]; 218 | } 219 | } 220 | } 221 | 222 | - (void) layoutSubviews { 223 | [super layoutSubviews]; 224 | 225 | CGSize intrinsicSize = self.intrinsicContentSize; 226 | if (self.minHeight) 227 | intrinsicSize.height = MAX(intrinsicSize.height, self.minHeight); 228 | 229 | if (self.maxHeight) 230 | intrinsicSize.height = MIN(intrinsicSize.height, self.maxHeight); 231 | 232 | _heightConstraint.constant = intrinsicSize.height; 233 | 234 | if (self.intrinsicContentSize.height <= self.bounds.size.height) { 235 | CGFloat topCorrect = (CGFloat)((self.bounds.size.height - self.contentSize.height * [self zoomScale]) / 2.0); 236 | topCorrect = (CGFloat)(topCorrect < 0.0 ? 0.0 : topCorrect); 237 | self.contentOffset = CGPointMake(0, -topCorrect); 238 | } 239 | _overview.frame = CGRectMake(0.0f, 0.0f, self.frame.size.width, self.frame.size.height); 240 | [_overview calculateAll]; 241 | } 242 | 243 | - (CGSize) intrinsicContentSize { 244 | CGSize intrinsicContentSize = self.contentSize; 245 | 246 | intrinsicContentSize.width += (self.textContainerInset.left + self.textContainerInset.right) / 2.0f; 247 | 248 | return intrinsicContentSize; 249 | } 250 | 251 | - (UIImage *) createPlaceholderImageForImage:(UIImage *)image { 252 | CALayer *imageLayer = [CALayer layer]; 253 | imageLayer.bounds = CGRectMake(0, 0, image.size.width, image.size.height); 254 | imageLayer.masksToBounds = YES; 255 | imageLayer.cornerRadius = 10; 256 | imageLayer.backgroundColor = [UIColor clearColor].CGColor; 257 | 258 | UIGraphicsBeginImageContext(image.size); 259 | 260 | CGContextRef context = UIGraphicsGetCurrentContext(); 261 | [imageLayer renderInContext:context]; 262 | 263 | UIImage *finalImage = UIGraphicsGetImageFromCurrentImageContext(); 264 | 265 | UIGraphicsEndImageContext(); 266 | return finalImage; 267 | }; 268 | 269 | - (void) attachData:(id )data { 270 | BOOL shouldAdd = YES; 271 | if (_animatedTextViewDelegate && [_animatedTextViewDelegate respondsToSelector:@selector(shouldAddAttachmentData:)]) { 272 | shouldAdd = [_animatedTextViewDelegate shouldAddAttachmentData:data]; 273 | } 274 | 275 | if (shouldAdd) { 276 | UIImage *visibleImage = data.image; 277 | UIImage *placeholderImage = [self createPlaceholderImageForImage:visibleImage]; 278 | 279 | UITextRange *selRange = self.selectedTextRange; 280 | UITextPosition *selStartPos = selRange.start; 281 | NSUInteger index = (NSUInteger)[self offsetFromPosition:self.beginningOfDocument toPosition:selStartPos]; 282 | 283 | TMTextAttachment *attachment = [[TMTextAttachment alloc] initWithAttachmentData:data bounds:CGRectMake(0, -5, visibleImage.size.width / 2, visibleImage.size.height / 2)]; 284 | attachment.image = placeholderImage; 285 | [_overview registerData:data actualImage:visibleImage]; 286 | 287 | NSMutableAttributedString *string = [[NSMutableAttributedString alloc] initWithAttributedString:self.attributedText]; 288 | 289 | NSMutableAttributedString *attributedString = [[NSAttributedString attributedStringWithAttachment:attachment] mutableCopy]; 290 | [attributedString addAttribute:NSFontAttributeName value:self.font range:(NSRange){0, [attributedString length]}]; 291 | [string insertAttributedString:attributedString atIndex:index]; 292 | 293 | self.attributedText = string; 294 | [self layoutSubviews]; 295 | 296 | if (_animatedTextViewDelegate && [_animatedTextViewDelegate respondsToSelector:@selector(didAddAttachmentData:)]) 297 | [_animatedTextViewDelegate didAddAttachmentData:data]; 298 | 299 | dispatch_async(dispatch_get_main_queue(), ^{ 300 | [self becomeFirstResponder]; 301 | [TMAnimatedTextView selectTextForInput:self atRange:NSMakeRange((NSUInteger)index + 1, 0)]; 302 | }); 303 | } 304 | } 305 | 306 | - (void) animateData:(id )data withAnimation:(void (^)(UIImageView *imageView))animationBlock { 307 | [_overview animateData:data withAnimation:animationBlock]; 308 | } 309 | 310 | + (void) selectTextForInput:(UITextView *)input atRange:(NSRange)range { 311 | UITextPosition *startPosition = [input positionFromPosition:[input beginningOfDocument] 312 | offset:range.location]; 313 | UITextPosition *endPosition = [input positionFromPosition:startPosition 314 | offset:range.length]; 315 | [input setSelectedTextRange:[input textRangeFromPosition:startPosition toPosition:endPosition]]; 316 | } 317 | 318 | - (BOOL) attachmentExistsWithIdentifier:(NSString *)identifier { 319 | __block BOOL result = NO; 320 | 321 | NSRange limitRange = NSMakeRange(0, [self.attributedText length]); 322 | [self.attributedText enumerateAttribute:NSAttachmentAttributeName inRange:limitRange options:NSAttributedStringEnumerationLongestEffectiveRangeNotRequired usingBlock:^(NSTextAttachment *attachment, NSRange range, BOOL *stop) { 323 | if (attachment) { 324 | TMTextAttachment *textAttachment = (TMTextAttachment *)attachment; 325 | if ([textAttachment.data.identifier isEqualToString:identifier]) { 326 | result = YES; 327 | *stop = YES; 328 | } 329 | } 330 | }]; 331 | 332 | return result; 333 | } 334 | 335 | - (CGRect) frameOfIdentifier:(NSString *)identifier { 336 | __block CGRect result = CGRectMake(0, 0, 0, 0); 337 | 338 | NSRange limitRange = NSMakeRange(0, [self.attributedText length]); 339 | [self.attributedText enumerateAttribute:NSAttachmentAttributeName inRange:limitRange options:NSAttributedStringEnumerationLongestEffectiveRangeNotRequired usingBlock:^(NSTextAttachment *attachment, NSRange range, BOOL *stop) { 340 | if (attachment) { 341 | TMTextAttachment *textAttachment = (TMTextAttachment *)attachment; 342 | if ([textAttachment.data.identifier isEqualToString:identifier]) { 343 | result = [self.layoutManager boundingRectForGlyphRange:range inTextContainer:self.textContainer]; 344 | result.origin.x += self.textContainerInset.left; 345 | result.origin.y += self.textContainerInset.top; 346 | 347 | if (result.size.height > textAttachment.expectedSize.height) { 348 | result.origin.y += (result.size.height - textAttachment.expectedSize.height); 349 | result.size.height -= (result.size.height - textAttachment.expectedSize.height); 350 | } 351 | *stop = YES; 352 | } 353 | } 354 | }]; 355 | 356 | return result; 357 | } 358 | 359 | - (id ) attachmentDataAtPoint:(CGPoint)point { 360 | NSTextContainer *textContainer = self.textContainer; 361 | NSLayoutManager *layoutManager = self.layoutManager; 362 | 363 | point.x -= self.textContainerInset.left; 364 | point.y -= self.textContainerInset.top; 365 | 366 | NSUInteger characterIndex = [layoutManager characterIndexForPoint:point inTextContainer:textContainer fractionOfDistanceBetweenInsertionPoints:nil]; 367 | if (characterIndex >= self.text.length) 368 | return nil; 369 | 370 | NSRange range; 371 | TMTextAttachment *attachment = [self.attributedText attribute:NSAttachmentAttributeName atIndex:characterIndex effectiveRange:&range]; 372 | 373 | // The NSLayoutManager characterIndexForPoint:inTextContainer:fractionOfDistanceBetweenInsertionPoints method returns the closest glyph but we want 374 | // to only select one if it's actually under the point. 375 | CGRect boundingRect = [layoutManager boundingRectForGlyphRange:range inTextContainer:textContainer]; 376 | if (CGRectContainsPoint(boundingRect, point)) 377 | return attachment.data; 378 | 379 | return nil; 380 | } 381 | 382 | #pragma mark - UIGestureRecognizerDelegate 383 | 384 | - (BOOL) gestureRecognizer:(UIGestureRecognizer *)recognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer { 385 | // If we're touching an attachment then we don't want the normal selection functionality to be done 386 | CGPoint point = [recognizer locationInView:self]; 387 | id data = [self attachmentDataAtPoint:point]; 388 | return data == nil; 389 | } 390 | 391 | - (void) handleTextViewTapGestureRecognizer:(UITapGestureRecognizer *)recognizer { 392 | if (recognizer.state == UIGestureRecognizerStateFailed) return; 393 | 394 | CGPoint point = [recognizer locationInView:self]; 395 | id data = [self attachmentDataAtPoint:point]; 396 | if (data) { 397 | BOOL shouldInteract = YES; 398 | if (_animatedTextViewDelegate && [_animatedTextViewDelegate respondsToSelector:@selector(shouldInteractWithAttachmentData:)]) { 399 | shouldInteract = [_animatedTextViewDelegate shouldInteractWithAttachmentData:data]; 400 | } 401 | 402 | if (shouldInteract && _animatedTextViewDelegate && [_animatedTextViewDelegate respondsToSelector:@selector(didInteractWithAttachmentData:)]) { 403 | [_animatedTextViewDelegate didInteractWithAttachmentData:data]; 404 | } 405 | } 406 | } 407 | 408 | #pragma mark - UITextViewDelegate 409 | 410 | - (BOOL) textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)textRange replacementText:(NSString *)text { 411 | __block BOOL shouldDelete = YES; 412 | 413 | if (_animatedTextViewDelegate && [_animatedTextViewDelegate respondsToSelector:@selector(shouldDeleteAttachmentData:)]) { 414 | [textView.attributedText enumerateAttribute:NSAttachmentAttributeName inRange:textRange options:NSAttributedStringEnumerationLongestEffectiveRangeNotRequired usingBlock:^(NSTextAttachment *attachment, NSRange range, BOOL *stop) { 415 | if (attachment && shouldDelete) { 416 | TMTextAttachment *textAttachment = (TMTextAttachment *)attachment; 417 | shouldDelete &= [_animatedTextViewDelegate shouldDeleteAttachmentData:textAttachment.data]; 418 | } 419 | }]; 420 | } 421 | 422 | if (shouldDelete && _animatedTextViewDelegate && [_animatedTextViewDelegate respondsToSelector:@selector(didDeleteAttachmentData:)]) { 423 | [textView.attributedText enumerateAttribute:NSAttachmentAttributeName inRange:textRange options:NSAttributedStringEnumerationLongestEffectiveRangeNotRequired usingBlock:^(NSTextAttachment *attachment, NSRange range, BOOL *stop) { 424 | if (attachment) { 425 | TMTextAttachment *textAttachment = (TMTextAttachment *)attachment; 426 | [_overview unregisterData:textAttachment.data]; 427 | [_animatedTextViewDelegate didDeleteAttachmentData:textAttachment.data]; 428 | } 429 | }]; 430 | } 431 | 432 | return shouldDelete; 433 | } 434 | 435 | - (void) textViewDidChange:(UITextView *)textView { 436 | [_overview calculateAll]; 437 | if (_animatedTextViewDelegate && [_animatedTextViewDelegate respondsToSelector:@selector(animatedTextViewDidChange:)]) { 438 | [_animatedTextViewDelegate animatedTextViewDidChange:self]; 439 | } 440 | } 441 | 442 | #pragma mark - Pasteboard 443 | 444 | - (BOOL) canPerformAction:(SEL)action withSender:(id)sender { 445 | if (action == @selector(paste:)) { 446 | UIPasteboard *pasteboard = [UIPasteboard generalPasteboard]; 447 | if (pasteboard.image != nil) 448 | return YES; 449 | } 450 | 451 | return [super canPerformAction:action withSender:sender]; 452 | } 453 | 454 | - (void) paste:(id)sender { 455 | UIPasteboard *pasteboard = [UIPasteboard generalPasteboard]; 456 | UIImage *image = pasteboard.image; 457 | if (image) { 458 | if ([_animatedTextViewDelegate respondsToSelector:@selector(pasteImage:)]) { 459 | [_animatedTextViewDelegate pasteImage:image]; 460 | } 461 | } 462 | else 463 | [super paste:sender]; 464 | } 465 | 466 | @end 467 | --------------------------------------------------------------------------------