UITextView:使用UITextView文本内联UIButton iOS

问题描述:

我有一个UITextView,其中UIButton位于右下角。我的UITextView允许使用多行文本,即在输入Enter键时textView高度会动态增加(类似于SMS应用程序)。但我的问题是,在输入文字时,文字落后于UIButton。我希望文字在到达图像时停止,并在下一行继续而不会有任何文字中断。这可能吗? enter image description hereUITextView:使用UITextView文本内联UIButton iOS

+0

嗨@JMS,你找到了一个合适的解决方案? – adnako

+0

我使用排除路径来实现它。但是每当'UITextView'中的文本发生变化时,我们需要重置排除路径(尝试在'scrollViewDidScroll'方法中) – JMS

+0

谢谢,我将继承UITextView的子类以便就地修改排除路径。 – adnako

使用由苹果提供的Text Kit Framework。你可以做到这一点

这里是射线wenderlich啧啧 https://www.raywenderlich.com/50151/text-kit-tutorial

具体来说,你所要做的部分设置exclusionPaths为您的UITextView

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

谢谢。我会尝试一下。 – JMS

对于我这种解决方案的工作。我有我把这个代码的UItextview的子类。

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

是的,我尝试了上面的一样。但问题是,我的'UITextView'高度不断增加,这导致y值发生变化。在这种情况下我该怎么办? – JMS

试试下面的代码:

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

使用UITextView的代表和这样做,

- (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; 
    } 

}