Friday, 20 May 2016

Copy - Paste Image in Text View

Copy - Paste Image in Text View



Copy Image From Other View : -


- (BOOL)collectionView:(UICollectionView *)collectionView shouldSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    UIPasteboard *pasteBoard=[UIPasteboard generalPasteboard];
    
    NSData *data = [NSData dataWithContentsOfURL:[NSURL URLWithString:[array objectAtIndex:indexPath.row]]];
    
    [pasteBoard setData:data forPasteboardType:@"public.png"];
    
    UITextField  *myLabel= [[UITextField alloc] initWithFrame:CGRectMake(self.view.frame.size.width /2 - 103 , self.view.frame.size.height - 50 , 206 , 25)];
    
    myLabel.text = @"Sticker Copied";
    
    myLabel.borderStyle = UITextBorderStyleRoundedRect ;
    
    myLabel.textColor = [UIColor whiteColor];
    
    myLabel.backgroundColor = [UIColor blackColor];
    
    [self.view addSubview:myLabel];
    
    myLabel.hidden = NO;
    
    myLabel.alpha = 0.8f;
    
    [UIView animateWithDuration:0.5 delay:2.0 options:0 animations:^{
       
        myLabel.alpha = 0.0f;
    } completion:^(BOOL finished) {
       
        myLabel.hidden = YES;
    }];
    
    return NO ;
}





Paste Image In Text View : -



#import "ViewController.h"


@interface ViewController ()
{
    UITextView *textView ;
}
@end

@implementation ViewController


- (void)viewDidLoad
{
    [super viewDidLoad];
    
    textView = [[UITextView alloc] initWithFrame:CGRectMake(50,50,250,500)];
    
/*    NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:@"before after"];
    NSTextAttachment *textAttachment = [[NSTextAttachment alloc] init];
    textAttachment.image = [UIImage imageNamed:@"1.png"];
    
    CGFloat oldWidth = textAttachment.image.size.width;
    

    CGFloat scaleFactor = oldWidth / (textView.frame.size.width - 10);
    textAttachment.image = [UIImage imageWithCGImage:textAttachment.image.CGImage scale:scaleFactor orientation:UIImageOrientationUp];
    NSAttributedString *attrStringWithImage = [NSAttributedString attributedStringWithAttachment:textAttachment];
    [attributedString replaceCharactersInRange:NSMakeRange(6, 1) withAttributedString:attrStringWithImage];
    textView.attributedText = attributedString;
    */
    
    [self.view addSubview:textView];
    
}

- (BOOL)canPerformAction:(SEL)action withSender:(id)sender
{
    if (action == @selector(paste:) && [UIPasteboard generalPasteboard].image)
        return YES;
    else
        return [super canPerformAction:action withSender:sender];
}


- (void)paste:(id)sender{
    
    UIImage *image = [UIPasteboard generalPasteboard].image;
    
    if (image)
    {
        NSTextAttachment *textAttachment = [[NSTextAttachment alloc] init];
        textAttachment.image = image;
        NSAttributedString *imageString = [NSAttributedString attributedStringWithAttachment:textAttachment];
        
        NSAttributedString *attString =  textView.attributedText ;
        
        NSMutableAttributedString *result = [attString mutableCopy];
        [result appendAttributedString:imageString];
        
        textView.attributedText = result;
        
    } else {
        // Call the normal paste action
        [super paste:sender];
    }
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end


No comments:

Post a Comment