मैंने पाया है कि यह उन स्थितियों में पूरी तरह से असामान्य नहीं है जहां आपको अभी भी आवश्यकता हो सकती है। उचित यूआई इंटरैक्शन की अनुमति देने के लिए सच में सेट किए गए सक्षम होना चाहिए। इसका एक साधारण मामला यह है कि जब आप एक ऑटो विस्तार पाठ दृश्य की अनुमति देना चाहते हैं, लेकिन फिर भी यह सीमित है कि यह यूआईटेबल व्यू में कुछ उचित है।
यहाँ UITextView का एक उपवर्ग दिया गया है, जो ऑटो लेआउट के साथ ऑटो विस्तार की अनुमति देता है, लेकिन आप अभी भी अधिकतम ऊंचाई तक सीमित कर सकते हैं और जो प्रबंधन करेगा कि क्या ऊंचाई के आधार पर स्क्रॉल करने योग्य है या नहीं। डिफ़ॉल्ट रूप से दृश्य अनिश्चित रूप से विस्तारित होगा यदि आपके पास इस तरह से अपनी बाधाएं हैं।
import UIKit
class FlexibleTextView: UITextView {
// limit the height of expansion per intrinsicContentSize
var maxHeight: CGFloat = 0.0
private let placeholderTextView: UITextView = {
let tv = UITextView()
tv.translatesAutoresizingMaskIntoConstraints = false
tv.backgroundColor = .clear
tv.isScrollEnabled = false
tv.textColor = .disabledTextColor
tv.isUserInteractionEnabled = false
return tv
}()
var placeholder: String? {
get {
return placeholderTextView.text
}
set {
placeholderTextView.text = newValue
}
}
override init(frame: CGRect, textContainer: NSTextContainer?) {
super.init(frame: frame, textContainer: textContainer)
isScrollEnabled = false
autoresizingMask = [.flexibleWidth, .flexibleHeight]
NotificationCenter.default.addObserver(self, selector: #selector(UITextInputDelegate.textDidChange(_:)), name: Notification.Name.UITextViewTextDidChange, object: self)
placeholderTextView.font = font
addSubview(placeholderTextView)
NSLayoutConstraint.activate([
placeholderTextView.leadingAnchor.constraint(equalTo: leadingAnchor),
placeholderTextView.trailingAnchor.constraint(equalTo: trailingAnchor),
placeholderTextView.topAnchor.constraint(equalTo: topAnchor),
placeholderTextView.bottomAnchor.constraint(equalTo: bottomAnchor),
])
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
override var text: String! {
didSet {
invalidateIntrinsicContentSize()
placeholderTextView.isHidden = !text.isEmpty
}
}
override var font: UIFont? {
didSet {
placeholderTextView.font = font
invalidateIntrinsicContentSize()
}
}
override var contentInset: UIEdgeInsets {
didSet {
placeholderTextView.contentInset = contentInset
}
}
override var intrinsicContentSize: CGSize {
var size = super.intrinsicContentSize
if size.height == UIViewNoIntrinsicMetric {
// force layout
layoutManager.glyphRange(for: textContainer)
size.height = layoutManager.usedRect(for: textContainer).height + textContainerInset.top + textContainerInset.bottom
}
if maxHeight > 0.0 && size.height > maxHeight {
size.height = maxHeight
if !isScrollEnabled {
isScrollEnabled = true
}
} else if isScrollEnabled {
isScrollEnabled = false
}
return size
}
@objc private func textDidChange(_ note: Notification) {
// needed incase isScrollEnabled is set to true which stops automatically calling invalidateIntrinsicContentSize()
invalidateIntrinsicContentSize()
placeholderTextView.isHidden = !text.isEmpty
}
}
एक बोनस के रूप में वहाँ UILabel के समान प्लेसहोल्डर पाठ को शामिल करने के लिए समर्थन है।