1

I have a text view and I need to add image in text view.The text view idea can be assume as text and image and after image text and so on.I can add image on text view but my problem is when I again edit my text view ,the text go behind the image.What I want is suppose if the image is below my cursor position and I am editing the text so my image should also be shift front or back.Please help me how can I achieve the functionality of shifting image on editing text.My app deployment target is 5.0 so we can include the new features provided in ios 5.0.

In the image it is shown how on editing the text the text go behind the image view.That is why I need to shift my image view.

Please Please help me.I am stuck here.

Thanks in advance.

1 Answer 1

1

Dude Use my code...

- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text;
{
if ( [text isEqualToString:@"\n"] ) {

}
else
{
    NSString *yourstring = [[NSString alloc]init];
    yourstring = textView.text;
    CGSize s = [yourstring sizeWithFont:[UIFont systemFontOfSize:15] //define your textview font size
                      constrainedToSize:CGSizeMake(self.view.bounds.size.width - 40, MAXFLOAT)  // - 40 For cell padding
                          lineBreakMode:UILineBreakModeWordWrap];   
    CGRect frame = CGRectMake(0.0f, s.height+10, 320.0f, 20);//use YourImageView. height and width 
    YourImageView.frame=frame;
}
return YES;
}

Let Me know. If you find any Difficulty...

Edited *Additional....*

//imgSwipe2 is my UIImgaeView you can use your imageview. May Be i forgot to release some object and i am sure that you can handle but it's working fine,

-(void)textViewDidChangeSelection:(UITextView *)textView
 {
  NSRange range = textView.selectedRange;
if(range.location<textView.text.length)
{
    NSString * firstHalfString = [txtView.text substringToIndex:range.location]; 

    CGSize s = [firstHalfString sizeWithFont:[UIFont systemFontOfSize:15] 
                           constrainedToSize:CGSizeMake(self.view.bounds.size.width - 40, MAXFLOAT)  // - 40 For cell padding
                               lineBreakMode:UILineBreakModeWordWrap];   

    if(s.height<imgSwipe2.frame.origin.y)
    {
        // You can use this code any number of image

        imgSwipe2.frame = CGRectMake(imgSwipe2.frame.origin.x, s.height, imgSwipe2.frame.size.width, imgSwipe2.frame.size.height);  


    }
    else
    {
        // Do what ever you want to do

        NSString * firstString1 = textView.text;                
        CGSize s = [firstString1 sizeWithFont:[UIFont systemFontOfSize:15] 
                            constrainedToSize:CGSizeMake(self.view.bounds.size.width - 40, MAXFLOAT)  // - 40 For cell padding
                                lineBreakMode:UILineBreakModeWordWrap];   
        imgSwipe2.frame = CGRectMake(imgSwipe2.frame.origin.x, s.height, imgSwipe2.frame.size.width, imgSwipe2.frame.size.height);


    }   
  }
}

Hope,this will help you...Enjoy...

8
  • It worked like a charm for me.I am not having words to thank you.Thanks you so much for your help.I wish I could upvote it a lot of times.but it will work well with one image , can you please suggest how to imaplemnt something like text and image and then writing text after image.Please if you can help me on this.
    – Gypsa
    Apr 24, 2012 at 4:57
  • Do you means you want change order of image and text in current situation? Can you describe your scenario.? So, I can understand what actually you want.
    – Nitin
    Apr 24, 2012 at 5:18
  • My scenario is:-there is a text view in which user can write text and add multiple images.Now user type a line of text and add a image.(eg.abcdefgh and image1).Now user can write the text below image (eg.abcdefgh and image1 and ijklmnop and image2) and text and so on.This is how I have to implement.Now if the user move the cursor on ijk in this case the image 1 will reside on its position but image 2 will move down.If user move the cursor to abc and start writing from there then in this case both image image 1 and image2 will move down.
    – Gypsa
    Apr 24, 2012 at 5:22
  • Please look at the scenario mentioned in above comment and suggest how to do so.
    – Gypsa
    Apr 24, 2012 at 5:22
  • I have work right now..I can solve your problem but it need to do some workout time.So Will reply you midnight you by tomorrow.
    – Nitin
    Apr 24, 2012 at 5:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.