0

I want to add a UIImageView and UIButton inside UITextView. UITextView height changes on clicking enter button. I want the UIView with image and cancel button to be placed above the text. Is it possible?enter image description here

I have tried using Bezier path. But my image comes inline with the text, but I want my text to come in next line. Also the button is not tapable when I add it in this method.

UIBezierPath * imgRect = [UIBezierPath bezierPathWithRect:CGRectMake(0, 0, 50, 50)];
        self.commentText.textContainer.exclusionPaths = @[imgRect];

        UIImageView *spacerView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 50, 50)];
        spacerView.image = _pickedImageForComment;

        UIButton *cancelButton = [UIButton buttonWithType:UIButtonTypeCustom];

        [cancelButton addTarget:self
                   action:@selector(buttonsClicked:)
         forControlEvents:UIControlEventTouchUpInside];

        cancelButton.frame = CGRectMake(30, 0, 19, 19);
        [cancelButton setImage:[UIImage imageNamed:@"remove_image"] forState:UIControlStateNormal];

        [spacerView addSubview:cancelButton];

        [_commentText addSubview:spacerView];
JMS
  • 271
  • 4
  • 15
  • http://stackoverflow.com/questions/24010035/how-to-add-image-and-text-in-uitextview-in-ios – Bhavin Bhadani Apr 15 '16 at 11:50
  • http://stackoverflow.com/questions/28424045/combining-text-and-images-in-uitextview#28424571 – Bhavin Bhadani Apr 15 '16 at 11:50
  • I cannot use attributed string because I need to add a `UIView` which contains `UIImageView` and `UIButton` not only image – JMS Apr 15 '16 at 11:53
  • You have to set `userInteractionEnabled` to `YES` on the superview of the UIButton, which in your case is `spaceView`. `spaceView` is a `UIImageView`, so by defaults it's set to `NO`. If you want your text on the next line, dont draw a "square" for the exclusion path, draw a rectangle that take the whole upper part, or simply don't add it into the `UITextView`, just add it into a `UIView` above the `UITextView`. – Larme Jun 21 '16 at 14:28

0 Answers0