通过动态调整大小查看定位

时间:2014-05-20 07:09:36

标签: ios objective-c view uiview uitextview

我正在尝试创建一个动态增加高度的UITextView。 有UIViewUITextViewUIButton进行分组。

我已为[{1}}通知的NSnotificationCenter添加了观察者。

根据当前点击文字的高度,我创建了一个新的UITextViewTextDidChangeNotification

CGRect

然后,我改变了当前CGRect newRect = [contentTextView.text boundingRectWithSize:CGSizeMake(222, CGFLOAT_MAX) options:(NSStringDrawingUsesLineFragmentOrigin|NSStringDrawingUsesFontLeading) attributes:@{NSFontAttributeName:font} context:nil]; 的大小:

UIView

最后,我更改了CGRect tmp = commentView.frame; CGRect fitRectForView = CGRectMake(tmp.origin.x, tmp.origin.y - abs(newRectH - commentView.frame.size.height), tmp.size.width, newRect.size.height); commentView.frame = fitRectForView;

的高度
UITextView

以下是拟合前后CGRect fitRectForTextView = CGRectMake(contentTextView.frame.origin.x, contentTextView.frame.origin.y, contentTextView.frame.size.width,newRectH); contentTextView.frame = fitRectForTextView; 变量的原点和大小的对数:

contentTextView

Screenshot of the result

通过此屏幕截图,您可以看到只有//Before contentTextView origin : 0.000000 ; 0.000000 contentTextView size : 33.000000 ; 251.000000 //After contentTextView origin : 0.000000 ; 0.000000 contentTextView size : 84.000000 ; 251.000000 的高度正在变化。 我在commentView按钮上设置了约束,使其保持在send(commentView)的底部,但它无效。

我找不到我的错误。

这是我的第一个来源:create-a-multi-line-uitextfield

1 个答案:

答案 0 :(得分:0)

如果您的UITextView位于View Controller的视图中而不在键盘输入附件视图中,那么我通常会使用约束来执行此操作:

-(void)viewDidLoad
{
    ...

    myTextView = [[UITextView alloc] init];
    myTextView.scrollEnabled = NO;

    myTextView.translateAutoresizingMaskIntoConstraints = NO;

    // -----------------------------------------------
    // set textview delegate for delegate method
    // -----------------------------------------------
    myTextView.delegate = self;

    // -----------------------------------------------
    // put your code to setup all the constraint 
    // for your textView here
    //
    // Note: alternatively, you can set your constraints
    // using Interface Builder.
    // -----------------------------------------------


}

// -----------------------------------------------
// This is the UITextView delegate method so make
// sure your .h file has conformed to 
// UITextViewDelegate method
// -----------------------------------------------
-(BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text
{
    ...

    // let autolayout know the textView needs to relayout
    [textView invalidateIntrinsicContentSize];

    return YES;
}

其他信息

如果您希望按钮显示在视图的底部,则需要将按钮设置为固定在UIView容器的底部。

由于代码中的限制,这就是它的完成方式:

id views = @{"containerView": self.containerView, @"contentTextView": self. contentTextView, @"myButton": self.myButton};

// make your container view expand to fill up your root view
[self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|[containerView]|" options:0 metrics:nil views:views]];
[self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|[containerView]|" options:0 metrics:nil views:views]];

// text view constraints
[self.containerView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-10-[contentTextView]-10-[myButton]-10-|" options:0 metrics:nil views:views]];
[self.containerView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|-10-[contentTextView]-10-|" options:0 metrics:nil views:views]];

// button constraints

// note here, we make the button bottom edge always same as text view's bottom edge.

[self.containerView addConstraint:[NSLayoutConstraint constraintWithItem:self.myButton attribute:NSLayoutAttributeBottom relatedBy:NSLayoutRelationEqual toItem:self.contentTextView attribute:NSLayoutAttributeBottom multiplier:1.0 constant:0.0]];