我已经实现了一个 ViewController,它有一个底部 UIView
,其中包含 UITextView
并且禁用了滚动,当您在里面输入时它会调整大小。
当包含的文本高度达到 90 像素时,我启用滚动 ->
scrollEnabled = YES;
应该发生什么 UITextView
和它的 superview
应该保持它们被限制的高度(超过 90 像素限制).
会发生什么:UITextView
调整回其默认值。
更多信息: 我正在使用 Multiline UITextField 的代码作为我的底部 View 。 我正在使用 iOS7。
感谢任何帮助,谢谢。
编辑:我的代码:
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
self.textBox.scrollEnabled = NO;
self.textBox.font = [UIFont fontWithName:@"Helvetica" size:14];
[self registerForKeyboardNotifications];
}
- (void)registerForKeyboardNotifications
{
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(keyboardWasShown:)
name:UIKeyboardWillShowNotification
object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(keyboardWillHide:)
name:UIKeyboardWillHideNotification
object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self
selector: @selector(keyPressed:)
name: UITextViewTextDidChangeNotification
object: nil];
}
- (void)keyboardWasShown:(NSNotification *)notification
{
NSDictionary *info = [notification userInfo];
CGSize kbSize = [info[UIKeyboardFrameBeginUserInfoKey] CGRectValue].size;
[self setViewMovedUp:YES byHeight:kbSize.height];
}
- (void)keyboardWillHide:(NSNotification *)notification
{
NSDictionary *info = [notification userInfo];
CGSize kbSize = [info[UIKeyboardFrameBeginUserInfoKey] CGRectValue].size;
[self setViewMovedUp:NO byHeight:kbSize.height];
}
- (void)keyPressed:(id)sender
{
CGRect textRect = [self.textBox.text boundingRectWithSize:CGSizeMake(255,MAXFLOAT)
options:(NSStringDrawingUsesLineFragmentOrigin)
attributes:@{NSFontAttributeName : [UIFont fontWithName:@"Helvetica" size:14]}
context:nil];
NSInteger newSizeH = textRect.size.height;
if (self.textBox.hasText) {
// if the height of our new chatbox is
// below 90 we can set the height
if (newSizeH <= 90) {
self.textBox.scrollEnabled = NO;
[self.textBox scrollRectToVisible:CGRectMake(0,0,1,1)
animated:NO];
// chatbox
CGRect chatBoxFrame = self.textBox.frame;
chatBoxFrame.size.height = newSizeH + 12;
self.textBox.frame = chatBoxFrame;
// form view
CGRect formFrame = self.commentBox.frame;
formFrame.size.height = 30 + newSizeH;
self.commentBox.frame = formFrame;
}
// if our new height is greater than 90
// sets not set the height or move things
// around and enable scrolling
if (newSizeH > 90) {
self.textBox.scrollEnabled = YES;
CGRect frame = self.textBox.frame;
frame.size.height = 102;
self.textBox.frame = frame;
CGRect formFrame = self.commentBox.frame;
formFrame.size.height = 30 + 90;
self.commentBox.frame = formFrame;
}
}
}
- (void)setViewMovedUp:(BOOL)movedUp byHeight:(CGFloat)height
{
int movement = movedUp ? -height : height;
[UIView animateWithDuration:0.3
animations:^{
self.dataView.frame = CGRectOffset(self.dataView.frame, 0.0, movement);
}];
}
最佳答案
虽然来晚了一点,但以防万一。 我自己也遇到了同样的问题。 TextView 在滚动时返回到原始大小。我解决它的方法是更新 UITextView 上的高度约束。 即每次更新 UITextView 的尺寸时,也需要更新相应的约束条件。
关于ios - 带有滚动的 UITextView 动态设置调整大小为默认值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20632830/