Jun*_*Kim 0 uitextfield uikit ios swift
从之前发布的答案中,我了解到这editingRect是在编辑文本时显示的矩形,也是textRect在未编辑文本时显示的矩形。但我不知道placeholderRect和确切地borderRect显示在 UITextField 控件中的位置。我认为它borderRect与 UITextField 的框架矩形相同,因为其中有“border”一词,但在执行此操作之后:
override func borderRect(forBounds bounds: CGRect) -> CGRect {
let rect = bounds
rect.size.height = 150 //the height of my UITextField currently is 100
return rect
}
Run Code Online (Sandbox Code Playgroud)
我意识到事实并非如此。那么placeholderRect和是什么borderRect,它们位于 UITextField 中的什么位置?
borderRect指边框的框架(如果 则存在于文本字段borderStyle != .none),并placeholderRect指占位符文本的框架 - 为text空时出现的文本。
这是一个可以粘贴到游乐场中的小示例,它显示了所有矩形的位置。我bounds为每个文本字段矩形返回了不同部分的矩形。
class TextFieldWithDifferentRects: UITextField {
override func textRect(forBounds bounds: CGRect) -> CGRect {
// top left
CGRect(x: bounds.minX, y: bounds.minY, width: bounds.width / 2, height: bounds.height / 2)
}
override func placeholderRect(forBounds bounds: CGRect) -> CGRect {
// top right
CGRect(x: bounds.midX, y: bounds.minY, width: bounds.width / 2, height: bounds.height / 2)
}
override func borderRect(forBounds bounds: CGRect) -> CGRect {
// bottom left
CGRect(x: bounds.minX, y: bounds.midY, width: bounds.width / 2, height: bounds.height / 2)
}
override func editingRect(forBounds bounds: CGRect) -> CGRect {
// bottom right
CGRect(x: bounds.midX, y: bounds.midY, width: bounds.width / 2, height: bounds.height / 2)
}
}
let view = TextFieldWithDifferentRects(frame: CGRect(x: 0, y: 0, width: 200, height: 200))
view.backgroundColor = .white // make the placeholder text more visible
view.borderStyle = .line
view.placeholder = "a" // use the playground quick view button here
view.text = "b" // and here
// by selecting everything, we kick the text field into edit mode, to show the editingRect
view.selectAll(nil) // and lastly here
Run Code Online (Sandbox Code Playgroud)
输出:
| 归档时间: |
|
| 查看次数: |
836 次 |
| 最近记录: |