-
Notifications
You must be signed in to change notification settings - Fork 40
/
Copy pathUITextView.swift
58 lines (49 loc) · 1.48 KB
/
UITextView.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
//
// UITextView.swift
// UIKit
//
// Created by Michael Knoch on 11.09.17.
// Copyright © 2017 flowkey. All rights reserved.
//
open class UITextView: UIScrollView {
private var label = UILabel()
public var isEditable = false
public var isSelectable = false
public var textColor: UIColor {
get { return label.textColor }
set { label.textColor = newValue }
}
public var text: String? {
get { return label.text }
set {
label.text = newValue
contentOffset = .zero // scroll to top left when new text is set
setNeedsLayout()
}
}
public var font: UIFont {
get { return label.font }
set {
label.font = newValue
setNeedsLayout()
}
}
override public init(frame: CGRect) {
super.init(frame: frame)
addSubview(label)
label.numberOfLines = 0
label.layer.contentsGravity = .topLeft
}
override open func sizeThatFits(_ size: CGSize) -> CGSize {
label.frame.width = bounds.width
return label.sizeThatFits(size)
}
private let spaceToVerticalScrollIndicator: CGFloat = 17.5
override open func layoutSubviews() {
label.frame.width = bounds.width - spaceToVerticalScrollIndicator
label.sizeToFit()
label.frame.origin = .zero
let textHeight = label.frame.height
contentSize = CGSize(width: bounds.width, height: max(textHeight, bounds.height))
}
}