TextView高度根据内容编程

时间:2017-11-15 07:08:10

标签: ios uitextview

我想根据内容调整textview的高度。整个视图放在scrollView中。还有一个顶部栏。顶部栏和textView以编程方式创建。 TextView的文本也是动态添加的。代码如下:

import UIKit

class DiaryViewViewController: UIViewController, UITextViewDelegate, UIScrollViewDelegate{

var scrollView: UIScrollView!
var containerView = UIView()
let diaryEntryText = UITextView()

override func viewDidLoad() {
    super.viewDidLoad()

    self.scrollView = UIScrollView()
    self.scrollView.delegate = self
    containerView = UIView()
    self.scrollView.delegate = self
    scrollView.isDirectionalLockEnabled = true

    self.scrollView.contentSize = CGSize(width: self.view.frame.size.width, height: 1000)

    //to fetch height of screen
    let screenSize = UIScreen.main.bounds
    let width = screenSize.width
    let height = screenSize.height

    //Top Bar
    let topBar = UIView(frame:CGRect(x: 0,y: 0, width: width, height: 60))
    topBar.backgroundColor = UIColor.white
    topBar.layer.shadowColor = UIColor.gray.cgColor
    topBar.layer.shadowOffset = CGSize(width: 0, height: 3)
    topBar.layer.shadowOpacity = 1
    topBar.layer.masksToBounds = false
    topBar.layer.shadowRadius = 8.0;
    //ImageView - Back Button
    let backBtn = UIButton(frame:CGRect(x: 25, y: 18, width: 18, height: 34))
    let backBtnImage = UIImage(named: "back_button") as UIImage?
    backBtn.setImage(backBtnImage, for: .normal)
    backBtn.layer.masksToBounds = true
    backBtn.addTarget(self,action:#selector(backButtonClicked),
                      for:.touchUpInside)
    //Label - Title
    let titleLabel = UILabel(frame:CGRect(x: width * 0.3, y: 13, width: width * 0.55, height: 40))
    titleLabel.text = "23 December 2018"
    titleLabel.contentMode = UIViewContentMode.center
    //include all in view
    topBar.addSubview(titleLabel)
    topBar.addSubview(backBtn)
    containerView.addSubview(topBar)

    //The textView
    let contentSize = self.diaryEntryText.sizeThatFits(self.diaryEntryText.bounds.size)
     //content of textView
    diaryEntryText.text = "------Lorem ipsum dolor sit er elit lamet, consectetaur cillium adipisicing pecu, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum. Nam liber te conscient to factor tum poen legum odioque civiuda.Lorem ipsum dolor sit er elit lamet, consectetaur cillium adipisicing pecu, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum. Nam liber te conscient to factor tum poen legum odioque civiuda.Lorem ipsum dolor sit er elit lamet, consectetaur cillium adipisicing pecu, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur  magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum. Nam liber te conscient to factor tum poen legum odioque civiuda.-----------------"
    diaryEntryText.frame = CGRect(x: 20, y: 80, width: self.view.frame.width - 2, height: contentSize.height)
    diaryEntryText.textAlignment = .justified

    diaryEntryText.translatesAutoresizingMaskIntoConstraints = false
    diaryEntryText.isScrollEnabled = false

    containerView.addSubview(diaryEntryText)
    scrollView.addSubview(containerView)
    self.view.addSubview(scrollView)

}

@objc func backButtonClicked(sender:UIButton)
{
    let nextViewController = self.storyboard?.instantiateViewController(withIdentifier: "DiaryDayViewController") as! DiaryDayViewController
    self.navigationController?.pushViewController(nextViewController, animated: true)
    self.present(nextViewController, animated: true, completion: nil)
}

//to disable horizontal Scroll
func scrollViewDidScroll(_ scrollView: UIScrollView) {

    if scrollView.contentOffset.x>0 {
        scrollView.contentOffset.x = 0
    }

}


override func viewDidLayoutSubviews() {
    super.viewDidLayoutSubviews()
    scrollView.frame = view.bounds
    containerView.frame = CGRect(x:0, y:0, width:scrollView.contentSize.width, height:scrollView.contentSize.height)
    /* I tried to add this, but it doesnt show up properly in iPhone X
        let contentSize = self.diaryEntryText.sizeThatFits(self.diaryEntryText.bounds.size)
    var frame = self.diaryEntryText.frame
    frame.size.height = contentSize.height
    frame.size.width = self.view.frame.width - 40
    self.diaryEntryText.frame = frame
    self.scrollView.contentSize = CGSize(width: self.view.frame.size.width, height: (contentSize.height + 60))
    */
}


override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}


override var prefersStatusBarHidden: Bool {
    return true
}

}

我试图在viewDidLayoutsSubview中设置框架的高度,但似乎没有任何工作正常。

0 个答案:

没有答案