-
Notifications
You must be signed in to change notification settings - Fork 6
/
TvOSTextViewerViewController.swift
100 lines (79 loc) · 3.71 KB
/
TvOSTextViewerViewController.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
//
// TvOSTextViewerViewController.swift
// TvOSTextViewer
//
// Created by David Cordero on 15.02.17.
// Copyright © 2017 David Cordero. All rights reserved.
//
import UIKit
private let defaultTextColor: UIColor = .white
private let defaultFontSize: CGFloat = 41
private let defaultFont: UIFont = .systemFont(ofSize: defaultFontSize)
private let defaultBackgroundBlurEffectStyle: UIBlurEffect.Style = .dark
public class TvOSTextViewerViewController: UIViewController {
@objc public var text: String = ""
@objc public var textEdgeInsets: UIEdgeInsets = .zero
@objc public var backgroundBlurEffectSyle = UIBlurEffect(style: defaultBackgroundBlurEffectStyle)
@objc public var textAttributes: [NSAttributedString.Key: Any] = [.foregroundColor: defaultTextColor,
.font: defaultFont]
private var backgroundView: UIVisualEffectView!
private var textView: FadedTextView!
// MARK: UIViewController
override public func viewDidLoad() {
super.viewDidLoad()
setUpView()
}
public override func viewDidLayoutSubviews() {
super.viewDidLayoutSubviews()
if textView.contentSize.height > textView.bounds.height {
textView.contentInset = .zero
}
else {
moveTextViewToTheCenter()
}
}
// MARK: Private
private func setUpView() {
setUpBackgroundView()
setUpTextView()
setUpConstraints()
}
private func setUpBackgroundView() {
backgroundView = UIVisualEffectView(effect: backgroundBlurEffectSyle)
view.addSubview(backgroundView)
}
private func setUpTextView() {
textView = FadedTextView()
textView.panGestureRecognizer.allowedTouchTypes = [NSNumber(integerLiteral: UITouch.TouchType.indirect.rawValue)]
textView.isUserInteractionEnabled = true
textView.attributedText = NSAttributedString(string: text,
attributes: textAttributes)
view.addSubview(textView)
}
private func moveTextViewToTheCenter() {
var contentInset = UIEdgeInsets.zero
contentInset.top = textView.bounds.height / 2
contentInset.top -= textView.contentSize.height / 2
textView.contentInset = contentInset
}
private func setUpConstraints() {
setUpBackgroundConstraints()
setUpTextViewConstraints()
}
private func setUpBackgroundConstraints() {
backgroundView.translatesAutoresizingMaskIntoConstraints = false
backgroundView.widthAnchor.constraint(equalTo: view.widthAnchor).isActive = true
backgroundView.heightAnchor.constraint(equalTo: view.heightAnchor).isActive = true
backgroundView.centerXAnchor.constraint(equalTo: view.centerXAnchor).isActive = true
backgroundView.centerYAnchor.constraint(equalTo: view.centerYAnchor).isActive = true
}
private func setUpTextViewConstraints() {
textView.translatesAutoresizingMaskIntoConstraints = false
textView.centerXAnchor.constraint(equalTo: view.centerXAnchor).isActive = true
textView.centerYAnchor.constraint(equalTo: view.centerYAnchor).isActive = true
let widthConstant = -(textEdgeInsets.left + textEdgeInsets.right)
textView.widthAnchor.constraint(equalTo: view.widthAnchor, constant: widthConstant).isActive = true
textView.topAnchor.constraint(greaterThanOrEqualTo: view.topAnchor, constant: textEdgeInsets.top).isActive = true
textView.bottomAnchor.constraint(greaterThanOrEqualTo: view.bottomAnchor, constant: -(textEdgeInsets.bottom)).isActive = true
}
}