【发布时间】:2019-09-26 21:52:56
【问题描述】:
当用户在我的 textView 中键入时,我想知道新行何时开始,因为我想在发生这种情况时更改我的 textView 的高度。也就是说,如果按下返回按钮,我知道如何完成此操作 - 但如果新行只是自动开始(例如,仅仅因为用户不断输入而开始新行),这似乎并不那么简单。当点击“返回”时,我在下面使用的代码可以成功运行 - 但我似乎无法让它检测到任何类型的换行。
希望我的措辞足够好。谢谢!
- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range
replacementText:(NSString *)text
{
if ([text isEqualToString:@"\n"]) {
self.replyField.text=[NSString stringWithFormat:@"%@\n",self.replyField.text];
CGFloat fixedWidth = self.replyField.frame.size.width;
CGSize newSize = [self.replyField sizeThatFits:CGSizeMake(fixedWidth, MAXFLOAT)];
CGRect newFrame = self.replyField.frame;
newFrame.size = CGSizeMake(fmaxf(newSize.width, fixedWidth), newSize.height);
self.replyField.frame = newFrame;
CGRect newFrame1 = self.upView.frame;
NSLog(@"WHAT IS THE NEW HEIGHT %f", newFrame1.size.height);
self.upView.frame = CGRectOffset(self.upView.frame, 0, -15);
return NO;
}
// For any other character return TRUE so that the text gets added to the view
return YES;
}
【问题讨论】:
-
@Gagan_iOS - #1 实际上在每次输入字符时都会检测到一个新行,这是行不通的。 #2 用于 swift,#3 在大多数情况下也不起作用...
-
我没有时间给出正确的答案,也找不到好的副本,但在文本视图的
contentSize上使用 KVO。
标签: ios objective-c