📅  最后修改于: 2023-12-03 14:48:09.450000             🧑  作者: Mango
在 iOS 开发中,我们经常需要使用 UITextView 来显示文本内容。但是有时需要再 UITextView 中设置一些占位符文本,提示用户需要输入的内容。本文将介绍如何使用 Swift 5 设置 UITextView 的占位符文本。
首先,在 Xcode 中创建一个新的项目,并添加一个 UITextView。可以通过拖动和放置的方式将 UITextView 添加到视图中,也可以手动在代码中创建。
// 在代码中创建 UITextView
let textView = UITextView(frame: CGRect(x: 0, y: 0, width: 200, height: 100))
textView.font = UIFont.systemFont(ofSize: 16)
textView.layer.borderWidth = 1.0
textView.layer.borderColor = UIColor.lightGray.cgColor
view.addSubview(textView)
在 UITextView 中设置占位符文本需要满足以下两个条件:
在 UITextView 的 viewDidLoad
中设置文本颜色为占位符颜色。在此示例中,占位符颜色为浅灰色。
class ViewController: UIViewController, UITextViewDelegate {
let textView = UITextView()
override func viewDidLoad() {
super.viewDidLoad()
textView.delegate = self
view.addSubview(textView)
textView.translatesAutoresizingMaskIntoConstraints = false
NSLayoutConstraint.activate([
textView.topAnchor.constraint(equalTo: view.safeAreaLayoutGuide.topAnchor),
textView.leadingAnchor.constraint(equalTo: view.safeAreaLayoutGuide.leadingAnchor),
textView.trailingAnchor.constraint(equalTo: view.safeAreaLayoutGuide.trailingAnchor),
textView.bottomAnchor.constraint(equalTo: view.safeAreaLayoutGuide.bottomAnchor)
])
textView.font = UIFont.systemFont(ofSize: 16)
textView.textColor = UIColor.lightGray // 设置文本颜色为占位符颜色
textView.layer.borderWidth = 1.0
textView.layer.borderColor = UIColor.lightGray.cgColor
}
}
监听 UITextView 的文本变化需要实现 UITextViewDelegate 的 textViewDidChange(_:)
方法。在此方法中,判断 UITextView 的文本是否为空,并根据情况显示或隐藏占位符文本。
class ViewController: UIViewController, UITextViewDelegate {
let textView = UITextView()
let placeholder = "请输入文本" // 占位符文本
override func viewDidLoad() {
super.viewDidLoad()
textView.delegate = self
view.addSubview(textView)
textView.translatesAutoresizingMaskIntoConstraints = false
NSLayoutConstraint.activate([
textView.topAnchor.constraint(equalTo: view.safeAreaLayoutGuide.topAnchor),
textView.leadingAnchor.constraint(equalTo: view.safeAreaLayoutGuide.leadingAnchor),
textView.trailingAnchor.constraint(equalTo: view.safeAreaLayoutGuide.trailingAnchor),
textView.bottomAnchor.constraint(equalTo: view.safeAreaLayoutGuide.bottomAnchor)
])
textView.font = UIFont.systemFont(ofSize: 16)
textView.textColor = UIColor.lightGray
textView.layer.borderWidth = 1.0
textView.layer.borderColor = UIColor.lightGray.cgColor
textView.text = placeholder // 设置占位符文本
}
func textViewDidChange(_ textView: UITextView) {
if textView.text.isEmpty { // 文本为空,显示占位符文本
textView.text = placeholder
textView.textColor = UIColor.lightGray
} else { // 隐藏占位符文本
textView.textColor = UIColor.black
}
}
}
class ViewController: UIViewController, UITextViewDelegate {
let textView = UITextView()
let placeholder = "请输入文本"
override func viewDidLoad() {
super.viewDidLoad()
textView.delegate = self
view.addSubview(textView)
textView.translatesAutoresizingMaskIntoConstraints = false
NSLayoutConstraint.activate([
textView.topAnchor.constraint(equalTo: view.safeAreaLayoutGuide.topAnchor),
textView.leadingAnchor.constraint(equalTo: view.safeAreaLayoutGuide.leadingAnchor),
textView.trailingAnchor.constraint(equalTo: view.safeAreaLayoutGuide.trailingAnchor),
textView.bottomAnchor.constraint(equalTo: view.safeAreaLayoutGuide.bottomAnchor)
])
textView.font = UIFont.systemFont(ofSize: 16)
textView.textColor = UIColor.lightGray
textView.layer.borderWidth = 1.0
textView.layer.borderColor = UIColor.lightGray.cgColor
textView.text = placeholder
}
func textViewDidChange(_ textView: UITextView) {
if textView.text.isEmpty {
textView.text = placeholder
textView.textColor = UIColor.lightGray
} else {
textView.textColor = UIColor.black
}
}
}
以上就是使用 Swift 5 设置 UITextView 的占位符文本的详细介绍。