如何动态调整UITextView的高度

时间:2016-07-30 22:59:10

标签: ios uiscrollview uitextview

所以我知道有一些像我这样的帖子,但不是我想要的。所以这就是我想要实现的目标。我有这个看起来像这样的ViewController。

enter image description here

当用户将文本输入到UITextView,其中当前有文字"描述"时,我想动态更改UITextView的高度,如果文字在哪里,变成多行或离开屏幕,用户可以滚动整个视图及其内容。这是我的计划。

@IBOutlet var scrollView: UIScrollView!
var projectNameTextField: UITextField!
var projectCategoryLabel: UIButton!
var dueDateLabel: UIButton!
var projectDescription: UITextView!
var projectDescriptionFrame: CGRect!

override func viewDidAppear(animated: Bool) {
    super.viewDidAppear(animated)

    let scrollViewWidth = scrollView.frame.width
    scrollView.contentSize.width = scrollViewWidth

    let trallingSpace: CGFloat = 12.0
    let contentWidth = scrollViewWidth - (trallingSpace * 2.0)

    projectNameTextField = UITextField(frame: CGRect(x: trallingSpace, y: scrollViewContentHeight(), width: contentWidth, height: 40))
    projectNameTextField.placeholder = "Title"
    updateScrollView(withView: projectNameTextField)


    let categoryLabel = UILabel()
    categoryLabel.text = "Category"

    projectCategoryLabel = UIButton()
    projectCategoryLabel.setTitle("Art", forState: .Normal)
    projectCategoryLabel.setTitleColor(UIColor.blackColor(), forState: .Normal)

    let stackViewHCategory = UIStackView(frame: CGRect(x: trallingSpace, y: scrollViewContentHeight(), width: contentWidth, height: 40))
    stackViewHCategory.axis = .Horizontal
    stackViewHCategory.spacing = 8
    stackViewHCategory.addArrangedSubview(categoryLabel)
    stackViewHCategory.addArrangedSubview(projectCategoryLabel)
    updateScrollView(withView: stackViewHCategory)


    let dueDateTitle = UILabel()
    dueDateTitle.text = "Due Date"

    dueDateLabel = UIButton()
    dueDateLabel.setTitle("No Due Date", forState: .Normal)
    dueDateLabel.setTitleColor(UIColor.blackColor(), forState: .Normal)

    let stackViewHDueDate = UIStackView(frame: CGRect(x: trallingSpace, y: scrollViewContentHeight(), width: contentWidth, height: 40))
    stackViewHDueDate.axis = .Horizontal
    stackViewHDueDate.spacing = 8
    stackViewHDueDate.addArrangedSubview(dueDateTitle)
    stackViewHDueDate.addArrangedSubview(dueDateLabel)
    updateScrollView(withView: stackViewHDueDate)


    projectDescription = UITextView(frame: CGRect(x: trallingSpace, y: scrollViewContentHeight(), width: contentWidth, height: 40))
    projectDescription.text = "Description"

    projectDescription.delegate = self

    projectDescription.scrollEnabled = false
    projectDescription.font = projectDescription.font?.fontWithSize(14)

    updateScrollView(withView: projectDescription)

    projectDescriptionFrame = projectDescription.frame
    projectDescriptionFrame.size.height = projectDescription.contentSize.height
    projectDescription.frame = projectDescriptionFrame
}

func textViewDidChange(textView: UITextView) {

    projectDescriptionFrame.size.height = projectDescription.contentSize.height
    projectDescription.frame = projectDescriptionFrame

    var contentSizeheight = CGFloat()

    for (index, viewInScrollView) in scrollView.subviews.enumerate() {
        if index > 1 {
            contentSizeheight += viewInScrollView.frame.height
        }
    }

    scrollView.contentSize.height = contentSizeheight
}


func updateScrollView(withView withView: UIView) {
    scrollView.addSubview(withView)
    scrollView.contentSize.height += withView.frame.height
}

func scrollViewContentHeight() -> CGFloat {

    var height = CGFloat()

    for (index, viewInScrollView) in scrollView.subviews.enumerate() {
        if index > 1 {
            height += viewInScrollView.frame.height
        }
    }

    return height
}

如何执行此操作以便调整UITextView帧并更新UIScrollView.contentSize(不是UITextView),以便用户可以滚动整个视图。我会说我想要一个用户界面,就像你在iOS Mail应用程序中制作新消息一样。在那里,您可以将电子邮件主题和邮件正文滚动为一个。请帮我解决一下这个。我已经坚持了几天。

enter image description here

1 个答案:

答案 0 :(得分:0)

好吧我明白了。这是我改变的。

在全球属性中。

var oldProjectDescriptionHeight: CGFloat!

var projectDescriptionHeight: CGFloat! {
    willSet {
       oldProjectDescriptionHeight = projectDescriptionHeight
    }
}

UITextViewDelegate textViewDidChange

func textViewDidChange(textView: UITextView) {

    let fixedWidth = textView.frame.size.width
    textView.sizeThatFits(CGSize(width: fixedWidth, height: CGFloat.max))
    let newSize = textView.sizeThatFits(CGSize(width: fixedWidth, height: CGFloat.max))
    var newFrame = textView.frame
    newFrame.size = CGSize(width: max(newSize.width, fixedWidth), height: newSize.height)
    textView.frame = newFrame

    projectDescriptionHeight = newFrame.height

    scrollView.contentSize.height += projectDescriptionHeight - oldProjectDescriptionHeight
}