Как сделать прокрутку UITextView в tvOS? - PullRequest
0 голосов
/ 28 апреля 2019

Мне нужно использовать UITextView для отображения контента , Но textView не смог прокрутить, я нахожу много ответов, таких как UITextView не прокручивается в tvOS , но те не работают. Как я могу это сделать?

import UIKit

class ViewController: UIViewController {
var textView = UITextView(frame: CGRect(x: 100, y: 100, width: UIScreen.main.bounds.size.width - 200, height: 200))

override var preferredFocusEnvironments: [UIFocusEnvironment]{
    return [textView]
}

override func shouldUpdateFocus(in context: UIFocusUpdateContext) -> Bool {
    return true
}

override func viewDidLoad() {
    super.viewDidLoad()
    let path = Bundle.main.path(forResource: "Sample", ofType: "html")
    let htmlStr = try! String.init(contentsOfFile: path!)
    let attributeStr = htmlStr.htmlToAttributedString

    textView.panGestureRecognizer.allowedPressTypes = [NSNumber(value: UITouch.TouchType.indirect.rawValue)]
    textView.isSelectable = true
    textView.isScrollEnabled = true
    textView.isUserInteractionEnabled = true
    textView.bounces = true
    textView.showsVerticalScrollIndicator = true
    textView.autoresizingMask = [.flexibleWidth, .flexibleHeight]
    textView.attributedText = attributeStr
    view.addSubview(textView)
}

override func didUpdateFocus(in context: UIFocusUpdateContext, with coordinator: UIFocusAnimationCoordinator) {
    context.nextFocusedView?.backgroundColor = UIColor.green
}
}

extension Data {
var htmlToAttributedString: NSAttributedString? {
    do {
        return try NSAttributedString(data: self, options: [.documentType: NSAttributedString.DocumentType.html, .characterEncoding: String.Encoding.utf8.rawValue], documentAttributes: nil)
    } catch {
        print("error:", error)
        return  nil
    }
}
var htmlToString: String {
    return htmlToAttributedString?.string ?? ""
}
}

extension String {
var htmlToAttributedString: NSAttributedString? {
    return Data(utf8).htmlToAttributedString
}
var htmlToString: String {
    return htmlToAttributedString?.string ?? ""
}
}

Ниже приведен файл "Sample.html"

<font face="Montserrat-ExtraBold" size="30" style="background-color:dardGray">A wonderful &gt; serenity &gt; has taken <b>possession</b> of my entire soul, <a href="LINK1" style="background-color:gray">like these sweet like these sweet like these sweet like these sweet</a> mornings of spring which I enjoy with my <b><i>whole heart</i></b>.
  I am alone, and feel the <a href="http://example.com/" rel="nofollow" style="background-color:gray">charm of existence charm of existence charm of existence charm of existence charm of existence charm of existence charm of existence</a> in this spot,
  which was <i>created for the bliss of souls like mine</i>. I am so happy, my dear friend, so absorbed in the exquisite sense of mere tranquil existence, that I neglect my talents. I should be incapable of drawing a single stroke at the present moment;
  and yet I feel that I never was a greater artist than now. When, while the lovely <i>valley teems</i> with vapour around me, and the meridian sun strikes the upper surface of the impenetrable foliage of my trees, and but a few stray gleams steal into
  the inner sanctuary, I throw myself down among the tall grass by the trickling stream; and, as I lie close to the earth, a thousand unknown plants are noticed by me: when I hear the buzz of the little world among the stalks, and grow familiar with the
  countless indescribable forms of the insects and flies, then I feel the presence of the Almighty, who formed us in his own image.</font>

Это все о моем коде, большое спасибо!

...