Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use UILabel to display placeholder #80

Merged
merged 1 commit into from
Nov 2, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Demo/ViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ class ViewController: UIViewController {
self.growingTextView.layer.cornerRadius = 4
self.growingTextView.backgroundColor = UIColor(white: 0.9, alpha: 1)
self.growingTextView.placeholderAttributedText = NSAttributedString(
string: "Placeholder text",
string: "Placeholder text Placeholder text Placeholder text",
attributes: [
.font: self.growingTextView.textView.font!,
.foregroundColor: UIColor.gray
Expand Down
5 changes: 3 additions & 2 deletions NextGrowingTextView.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,7 @@
developmentRegion = English;
hasScannedForEncodings = 0;
knownRegions = (
English,
en,
Base,
);
Expand Down Expand Up @@ -320,7 +321,7 @@
SDKROOT = iphoneos;
SWIFT_ACTIVE_COMPILATION_CONDITIONS = DEBUG;
SWIFT_OPTIMIZATION_LEVEL = "-Onone";
SWIFT_VERSION = 4.2;
SWIFT_VERSION = 5.0;
TARGETED_DEVICE_FAMILY = "1,2";
VERSIONING_SYSTEM = "apple-generic";
VERSION_INFO_PREFIX = "";
Expand Down Expand Up @@ -366,7 +367,7 @@
MTL_ENABLE_DEBUG_INFO = NO;
SDKROOT = iphoneos;
SWIFT_OPTIMIZATION_LEVEL = "-Owholemodule";
SWIFT_VERSION = 4.2;
SWIFT_VERSION = 5.0;
TARGETED_DEVICE_FAMILY = "1,2";
VALIDATE_PRODUCT = YES;
VERSIONING_SYSTEM = "apple-generic";
Expand Down
57 changes: 27 additions & 30 deletions NextGrowingTextView/NextGrowingInternalTextView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,18 @@ internal class NextGrowingInternalTextView: UITextView {

var didChange: () -> Void = {}
var didUpdateHeightDependencies: () -> Void = {}

private lazy var placeholderDisplayLabel = UILabel()

override init(frame: CGRect, textContainer: NSTextContainer?) {
super.init(frame: frame, textContainer: textContainer)

NotificationCenter.default.addObserver(self, selector: #selector(NextGrowingInternalTextView.textDidChangeNotification(_ :)), name: UITextView.textDidChangeNotification, object: self)

placeholderDisplayLabel.numberOfLines = 0
placeholderDisplayLabel.adjustsFontSizeToFitWidth = true
placeholderDisplayLabel.minimumScaleFactor = 0.4
addSubview(placeholderDisplayLabel)
}

required init?(coder aDecoder: NSCoder) {
Expand Down Expand Up @@ -71,54 +78,44 @@ internal class NextGrowingInternalTextView: UITextView {
didUpdateHeightDependencies()
}
}

var placeholderAttributedText: NSAttributedString? {
didSet {
setNeedsDisplay()
get {
placeholderDisplayLabel.attributedText
}
set {
placeholderDisplayLabel.attributedText = newValue
setNeedsLayout()
}
}

override func layoutSubviews() {
super.layoutSubviews()
setNeedsDisplay()
}

override func draw(_ rect: CGRect) {
super.draw(rect)

guard displayPlaceholder else { return }

let paragraphStyle: NSMutableParagraphStyle = NSMutableParagraphStyle()
paragraphStyle.alignment = textAlignment

let targetRect = CGRect(
x: 5 + textContainerInset.left,
y: textContainerInset.top,
width: frame.size.width - (textContainerInset.left + textContainerInset.right),
height: frame.size.height - (textContainerInset.top + textContainerInset.bottom)

let maxSize = bounds.inset(by: textContainerInset).size

var size = placeholderDisplayLabel.sizeThatFits(maxSize)
size.height = min(size.height, maxSize.height)

placeholderDisplayLabel.frame = CGRect(
origin: .init(
x: 5 + textContainerInset.left,
y: textContainerInset.top
),
size: size
)

let attributedString = placeholderAttributedText
attributedString?.draw(in: targetRect)
}

// MARK: Private

private var displayPlaceholder: Bool = true {
didSet {
if oldValue != displayPlaceholder {
setNeedsDisplay()
}
}
}

@objc
private dynamic func textDidChangeNotification(_ notification: Notification) {
updatePlaceholder()
didChange()
}

private func updatePlaceholder() {
displayPlaceholder = text.isEmpty
placeholderDisplayLabel.isHidden = !text.isEmpty
}
}