iOS - UITextView 边框顶部的 UILabel
我正在尝试使用uitextview
这是我到目前为止
func createTextFieldOutline() {
textViewBorder = CAShapeLayer()
let bez = UIBezierPath(roundedRect: self.bounds, cornerRadius: 7)
textViewBorder?.path = bez.cgPath
/* some styling code */
if let outline = textViewBorder {
self.layer.addSublayer(outline)
self.createFloatingLabel()
}
}
func createFloatingLabel() {
let titleLabel = UILabel()
/* some styling code */
titleLabel.autoresizingMask = [.flexibleWidth, .flexibleHeight]
titleLabel.sizeToFit()
titleLabel.adjustsFontSizeToFitWidth = true
titleLabel.frame = CGRect(x: 16, y: -7.5, width: 32, height: 15)
titleLabel.text = "Test"
self.layer.insertSublayer(titleLabel.layer, above: textViewBorder)
}
使用这些方法的方式,我已经创建了UitextView的子类,并在便利性初始化。但这并没有将标签放在边界之上。它像这样将其放在边界后面:
相同的方法适用于uitextfield
,但对于uite> uitextview
而不适用。知道我做错了什么?
I am trying to achieve something like this with a UITextView
This is what I have come with so far
func createTextFieldOutline() {
textViewBorder = CAShapeLayer()
let bez = UIBezierPath(roundedRect: self.bounds, cornerRadius: 7)
textViewBorder?.path = bez.cgPath
/* some styling code */
if let outline = textViewBorder {
self.layer.addSublayer(outline)
self.createFloatingLabel()
}
}
func createFloatingLabel() {
let titleLabel = UILabel()
/* some styling code */
titleLabel.autoresizingMask = [.flexibleWidth, .flexibleHeight]
titleLabel.sizeToFit()
titleLabel.adjustsFontSizeToFitWidth = true
titleLabel.frame = CGRect(x: 16, y: -7.5, width: 32, height: 15)
titleLabel.text = "Test"
self.layer.insertSublayer(titleLabel.layer, above: textViewBorder)
}
The way I use these methods is I have created a subclass of UITextView and call above two methods in convenience init
. But it does not put the label on top of border. It puts it behind the border like this:
The same approach works for UITextField
but not for UITextView
. Any idea what am I doing wrong?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在这里,我创建了实现这种 TextField 的最佳 Pod。
pod 'MaterialComponents/TextControls+OutlinedTextFields'
https ://material.io/components/text-fields/ios#outlined-text-field
您可以在此平台上找到更多类似于 android UI 的 UI。
https://material.io/
Here, I have founded best pod for implement this kind of TextField.
pod 'MaterialComponents/TextControls+OutlinedTextFields'
https://material.io/components/text-fields/ios#outlined-text-field
You can find more UI which looks like android UI on this platform.
https://material.io/