簡體   English   中英

占位符文本未出現在單行文本字段中

[英]Placeholder Text Not Appearing In Single Line Text Field

我正在嘗試使用占位符文本創建一個帶下划線的文本字段,我得到了以下擴展名來提供幫助。

extension UITextField {

  func setBottomLine(borderColor: UIColor) {
    
    borderStyle = .none

    backgroundColor = .clear

    let borderLine = CALayer()

    borderLine.frame = CGRect(x: 0, y: frame.height - 2, width: frame.width, height: 2)

    layer.addSublayer(borderLine)

 }

我在我的視圖 controller 的布局子視圖中調用此擴展,並且每當我分配占位符文本時,什么都不會顯示。 我對問題所在感到非常困惑。 謝謝!

    extension UITextField {
    
        func setUnderLine() {
            let border = CALayer()
            let width = CGFloat(0.5)
            border.borderColor = UIColor.darkGray.cgColor
            border.frame = CGRect(x: 0, y: self.frame.size.height - width, width:  self.frame.size.width - 10, height: self.frame.size.height)
            border.borderWidth = width
            self.layer.addSublayer(border)
            self.layer.masksToBounds = true
        }
    }
override func viewDidLoad() {
            super.viewDidLoad()
            mytextField.setUnderLine()
}

看一下這個。

import UIKit

class ViewController: UIViewController {

@IBOutlet weak var yourTextField: UITextField!
override func viewDidLoad() {
    super.viewDidLoad()
    yourTextField.textFieldUnderscoreAndPlaceholder()

    }
}

extension UITextField {

    //MARK: Text underscore with placeholder
    func textFieldUnderscoreAndPlaceholder() {
    
    //Underscore
    let textFieldUnderscore = CALayer()
    textFieldUnderscore.frame = CGRect(x: 0.0, y: frame.height - 1, width: frame.width, height: 1.0)
    bounds = bounds.insetBy(dx: 0, dy: -2)
    textFieldUnderscore.backgroundColor = UIColor(red: 0, green: 0, blue: 0, alpha: 1).cgColor
    layer.addSublayer(textFieldUnderscore)
    
    //Placeholder
    placeholder = "Type some text here..."
    
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM