2016-04-30 67 views
1

我有一個UITextView,其中UIButton位於右下角。我的UITextView允許使用多行文本,即在輸入Enter鍵時textView高度會動態增加(類似於SMS應用程序)。但我的問題是,在輸入文字時,文字落後於UIButton。我希望文字在到達圖像時停止,並在下一行繼續而不會有任何文字中斷。這可能嗎? enter image description hereUITextView:使用UITextView文本內聯UIButton iOS

+0

嗨@JMS,你找到了一個合適的解決方案? – adnako

+0

我使用排除路徑來實現它。但是每當'UITextView'中的文本發生變化時,我們需要重置排除路徑(嘗試在'scrollViewDidScroll'方法中) – JMS

+0

謝謝,我將繼承UITextView的子類以便就地修改排除路徑。 – adnako

回答

0

使用由蘋果提供的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

1

對於我這種解決方案的工作。我有我把這個代碼的UItextview的子類。

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

是的,我嘗試了上面的一樣。但問題是,我的'UITextView'高度不斷增加,這導致y值發生變化。在這種情況下我該怎麼辦? – JMS

0

試試下面的代碼:

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

}