JMS
JMS

Reputation: 281

UITextView : Make a UIButton inline with UITextView text iOS

I have a UITextView in which there is UIButton at the bottom right corner. My UITextView allows multiline text, i.e The textView height dynamically increases on typing enter key (similar to SMS app). But my problem is that on typing text, the text goes behind the UIButton. I want the text to stop when it reaches image and continue in next line without any break in text. Is this possible?enter image description here

Upvotes: 1

Views: 1044

Answers (3)

Ronak Chaniyara
Ronak Chaniyara

Reputation: 5436

Try below code:

UIBezierPath* exclusionPath = [UIBezierPath bezierPathWithRect:Button.frame];
        self.textContainer.exclusionPaths  = @[exclusionPath];

Use UITextView Delegate and do something like,

- (void)textViewDidChange:(UITextView *)textView
{

    //Get Buttons Y position and Allow TextView Frame changes until it is less than or equal buttons Y position

    if (textView.frame.origin.y+textView.frame.size.height < Button.frame.origin.y) {

        CGFloat fixedWidth = textView.frame.size.width;
        CGSize newSize = [textView sizeThatFits:CGSizeMake(fixedWidth, MAXFLOAT)];
        CGRect newFrame = textView.frame;
        newFrame.size = CGSizeMake(fmaxf(newSize.width, fixedWidth), newSize.height);
        textView.frame = newFrame;
    }

}

Upvotes: 0

Big b
Big b

Reputation: 21

this solution work for me. I have subclass of UItextview where i put this code.

UIBezierPath* exclusionPath = [UIBezierPath bezierPathWithRect:_buttonClear.frame];
        self.textContainer.exclusionPaths  = @[exclusionPath];

Upvotes: 1

Shubhank
Shubhank

Reputation: 21805

Using the Text Kit Framework provided by apple. you can achieve this

Here is the ray wenderlich tut https://www.raywenderlich.com/50151/text-kit-tutorial

Specifically the part you have to do is set exclusionPaths for your UITextView

UIBezierPath* exclusionPath = [_timeView curvePathWithOrigin:myButton.center];
_textView.textContainer.exclusionPaths  = @[exclusionPath];

Upvotes: 0

Related Questions