How to let TextView be able to scroll horizontally

前端 未结 4 1278
心在旅途
心在旅途 2020-12-31 12:38

I know the TextView is embedded in a ScrollView. And if there is a fairly long String(Which contains no \"\\n\")<

4条回答
  •  误落风尘
    2020-12-31 13:16

    @IBOutlet weak var scrollView: UIScrollView!
    @IBOutlet weak var textView: UITextView!
    
    var yourText: String = "Whatever your text is going to be." 
    
    override func viewDidLayoutSubviews() {
    
        super.viewDidLayoutSubviews()
        self.scrollView.layoutIfNeeded()
        self.scrollView.contentSize = self.textView.bounds.size
    
    }
    
    
    override func viewDidLoad() {
        super.viewDidLoad()
    
    textView.text = yourText
    
        textView.sizeThatFits(CGSizeMake(textView.frame.size.width, 80)) //height to equal the actual size of your text view.
    
    }
    

    I think this should plug and play, but I know for sure that textView.sizeThatFits works. You have to make sure that you constrain your text view to it's parent scrollview for it to work. (Leading, Trailing, Top, Bottom and the Height.)

提交回复
热议问题