I am trying to create a UITextView that my app outputs data to. I would like to be able to scroll through it, as the user.
I create it with this function, called within ViewDidLoad:
func createStatusField() -> UITextView {
let myStatus = UITextView(frame: CGRect(x: 50, y: 50, width: 100, height: 300))
myStatus.autocorrectionType = .no
myStatus.text = "hello there"
myStatus.backgroundColor = .secondarySystemBackground
myStatus.textColor = .secondaryLabel
myStatus.font = UIFont.preferredFont(forTextStyle: .body)
myStatus.layer.zPosition = 1
myStatus.isScrollEnabled = true
myStatus.showsVerticalScrollIndicator = true
return myStatus
}
I then change its positioning and size with constraints:
mySession.anchorXCenter(to: myView)
mySession.anchorSizePercent(to: myView, sFactor: CGSize(width: 0.9, height: 0.3))
mySession.anchor(top: myView.topAnchor, leading: nil, bottom: nil, trailing: nil, padding: .init(top: 20, left: 0, bottom: 0, right: 0))
And then, just to test it, I give it a large text value then scroll to the end of whatever text it holds:
mySession.text = "Sed ut perspiciatis,\n\n unde omnis iste natus error sit voluptatem accusantium doloremque laudantium, totam rem aperiam eaque ipsa, quae ab illo inventore veritatis et quasi architecto beatae vitae dicta sunt, explicabo. Nemo enim ipsam voluptatem, quia voluptas sit, aspernatur aut odit aut fugit, sed quia consequuntur magni dolores eos, qui ratione voluptatem sequi nesciunt, neque porro quisquam est, qui dolorem ipsum, quia dolor sit amet consectetur adipisci[ng] velit, sed quia non numquam [do] eius modi tempora inci[di]dunt, ut labore et dolore magnam aliquam quaerat voluptatem. Ut enim ad minima veniam, quis nostrum[d] exercitationem ullam corporis suscipit laboriosam, nisi ut aliquid ex ea commodi consequatur? [D]Quis autem vel eum iure reprehenderit, qui in ea voluptate velit esse, quam nihil molestiae consequatur, vel illum, qui dolorem eum fugiat, quo voluptas nulla pariatur? [33] At vero eos et accusamus et iusto odio dignissimos ducimus, qui blanditiis praesentium voluptatum deleniti atque corrupti, quos dolores et quas molestias excepturi sint, obcaecati cupiditate non provident, similique sunt in culpa, qui officia deserunt mollitia animi, id est laborum et dolorum fuga. Et harum quidem rerum facilis est et expedita distinctio. Nam libero tempore, cum soluta nobis est eligendi optio, cumque nihil impedit, quo minus id, quod maxime placeat, facere possimus, omnis voluptas assumenda est, omnis dolor repellendus. Temporibus autem quibusdam et aut officiis debitis aut rerum necessitatibus saepe eveniet, ut et voluptates repudiandae sint et molestiae non recusandae. Itaque earum rerum hic tenetur a sapiente delectus, ut aut reiciendis voluptatibus maiores alias consequatur aut perferendis doloribus asperiores repellat"
let range = NSMakeRange(mySession.text.count - 1, 0)
mySession.scrollRangeToVisible(range)
While I all the above works, I was under the impression that as the user, I could just scroll through the entire text. But I cannot. Are there properties that I am not setting? Or do I have to build the scrolling detection myself? I ask that last question because if that's the case, I cannot find an example of it via Google.
CodePudding user response:
First of all a UItextView always scroll to end of the text you don't need to manage scroll range here.
Secondly there is some constraints issue if you are not seeing the text properly.
And last I have checked this text within a simple textView just giving a bottom 10 inset it will scroll to end of the text and also show the 10 points empty space to bottom that I given as inset.
lazy var nameTxtFVirew: UITextView = {
let txtView = UITextView()
txtView.text = "Sed ut perspiciatis,\n\n unde omnis iste natus error sit voluptatem accusantium doloremque laudantium, totam rem aperiam eaque ipsa, quae ab illo inventore veritatis et quasi architecto beatae vitae dicta sunt, explicabo. Nemo enim ipsam voluptatem, quia voluptas sit, aspernatur aut odit aut fugit, sed quia consequuntur magni dolores eos, qui ratione voluptatem sequi nesciunt, neque porro quisquam est, qui dolorem ipsum, quia dolor sit amet consectetur adipisci[ng] velit, sed quia non numquam [do] eius modi tempora inci[di]dunt, ut labore et dolore magnam aliquam quaerat voluptatem. Ut enim ad minima veniam, quis nostrum[d] exercitationem ullam corporis suscipit laboriosam, nisi ut aliquid ex ea commodi consequatur? [D]Quis autem vel eum iure reprehenderit, qui in ea voluptate velit esse, quam nihil molestiae consequatur, vel illum, qui dolorem eum fugiat, quo voluptas nulla pariatur? [33] At vero eos et accusamus et iusto odio dignissimos ducimus, qui blanditiis praesentium voluptatum deleniti atque corrupti, quos dolores et quas molestias excepturi sint, obcaecati cupiditate non provident, similique sunt in culpa, qui officia deserunt mollitia animi, id est laborum et dolorum fuga. Et harum quidem rerum facilis est et expedita distinctio. Nam libero tempore, cum soluta nobis est eligendi optio, cumque nihil impedit, quo minus id, quod maxime placeat, facere possimus, omnis voluptas assumenda est, omnis dolor repellendus. Temporibus autem quibusdam et aut officiis debitis aut rerum necessitatibus saepe eveniet, ut et voluptates repudiandae sint et molestiae non recusandae. Itaque earum rerum hic tenetur a sapiente delectus, ut aut reiciendis voluptatibus maiores alias consequatur aut perferendis doloribus asperiores repellat"
txtView.contentInset = .init(top: 0, left: 0, bottom: 10, right: 0)
txtView.layer.zPosition = 1
txtView.showsVerticalScrollIndicator = true
txtView.textColor = .lightGray
return txtView
}()
CodePudding user response:
Answering my own question here for anyone else who comes across this.
Within my main UIViewController I had the UITextView, multiple UIBottons, and a UIView. They all were subviews of the UIViewController.
Although I never figured out why, it appeared that the UIView was somehow interfering with the UITextView getting its gesture recognizers. That is my assumption since I took out every item, put them back one by one, and this error only occurred with the UIView present.
So I removed that, and the UITextView behaves as it should.