Skip to content

Commit

Permalink
Merge pull request #80 from muukii/muukii/placeholder-label
Browse files Browse the repository at this point in the history
Use UILabel to display placeholder
  • Loading branch information
muukii authored Nov 2, 2019
2 parents aa9203e + 55d797b commit 2b530d9
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 33 deletions.
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
}
}

0 comments on commit 2b530d9

Please sign in to comment.