当值为空字符串Material Swift时,TextField不显示底线

Posted

技术标签:

【中文标题】当值为空字符串Material Swift时,TextField不显示底线【英文标题】:TextField not show bottom line when value is empty string Material Swift 【发布时间】:2017-12-01 07:03:12 【问题描述】:

我在视图中使用material textfield。但是当这个textfield's 值为空字符串时,不会显示下划线。我该如何解决?

在下图中:email2 有空的string 并且没有显示下划线。

这是我的看法:

import UIKit
import SnapKit
import Material

class EditMailView: UIView 

    public let email1: UILabel = 
        let lb = UILabel()
        lb.text = "email1"
        lb.font = UIFont.systemFont(ofSize: 14)
        return lb
    ()

    public let email2: UILabel = 
        let lb = UILabel()
        lb.text = "email2"
        lb.font = UIFont.systemFont(ofSize: 14)
        return lb
    ()

    public let email1Input: TextField = 
        let field = TextField()
        field.font = UIFont.systemFont(ofSize: 15)
        return field
    ()

    public let email2Input: TextField = 
        let field = TextField()
        field.font = UIFont.systemFont(ofSize: 15)
        return field
    ()


    public let email1Counter: UILabel = 
        let lb = UILabel()
        lb.font = UIFont.systemFont(ofSize: 14)
        return lb
    ()

    public let email2Counter: UILabel = 
        let lb = UILabel()
        lb.font = UIFont.systemFont(ofSize: 14)
        return lb
    ()


    convenience init(email1 e1: String, email2 e2: String) 
        self.init(frame: .zero)

        email1Input.text = e1
        email2Input.text = e2

        addSubview(email1)
        addSubview(email2)
        addSubview(email1Input)
        addSubview(email2Input)
        addSubview(email1Counter)
        addSubview(email2Counter)

        email1.snp.makeConstraints  make in
            make.top.equalTo(10)
            make.left.equalTo(10)
            make.right.equalTo(10)
            make.bottom.equalTo(email1Input.snp.top).offset(-10)
        

        email1Input.snp.makeConstraints  make in
            make.top.equalTo(email1.snp.bottom).offset(10)
            make.left.equalTo(10)
            make.right.equalTo(10)
            make.bottom.equalTo(email1Counter.snp.top).offset(-10)
        

        email1Counter.snp.makeConstraints  make in
            make.top.equalTo(email1Input.snp.bottom).offset(10)
            make.left.equalTo(10)
            make.right.equalTo(10)
            make.bottom.equalTo(email2.snp.top).offset(-10)
        

        email2.snp.makeConstraints  make in
            make.top.equalTo(email1Counter.snp.bottom).offset(10)
            make.left.equalTo(10)
            make.right.equalTo(10)
            make.bottom.equalTo(email2Input.snp.top).offset(-10)
        

        email2Input.snp.makeConstraints  make in
            make.top.equalTo(email2.snp.bottom).offset(10)
            make.left.equalTo(10)
            make.right.equalTo(10)
            make.bottom.equalTo(email2Counter.snp.top).offset(-10)
        

        email2Counter.snp.makeConstraints  make in
            make.top.equalTo(email2Input.snp.bottom).offset(10)
            make.left.equalTo(10)
            make.right.equalTo(10)
            make.bottom.equalToSuperview().offset(-10)
        
    

和控制器:

import UIKit
import Snapkit

class EditMailController: UIViewController 

    let editView = EditMailView(email1: "123@abc.xyz", email2: "")

    override func viewDidLoad() 
        super.viewDidLoad()


        view.addSubview(editView)

        editView.snp.makeConstraints  (maker) in
            maker.edges.equalToSuperview()
        

        editView.email1.becomeFirstResponder()

    

【问题讨论】:

你的代码在哪里? @iPatel 将更新问题。 @iPatel 我更新了我的问题。你能帮帮我吗? 【参考方案1】:

此问题已解决。而是尝试使用空字符串为 email2 设置值,例如:

email1Input.text = e1
email2Input.text = e2

当 e2 是这样的空字符串时我忽略了:

email1Input.text = e1
if !e2.isEmpty 
    email2Input.text = e2

【讨论】:

以上是关于当值为空字符串Material Swift时,TextField不显示底线的主要内容,如果未能解决你的问题,请参考以下文章

当值有太多有效数字时,read.csv 将数字转换为字符/因子

Material-ui <Autocomplete /> getOptionLabel - 将空字符串作为值传递

当值为空时如何取消设置属性?

Swift / SwiftUI:如何检查环境 /binding var 是不是为空字符串(.isEmpty 出现构建错误)

使用 JSONEncoders swift 跳过空字符串

复选框输入返回空字符串而不是 true/false,使用 React hook dom 和 material-ui FormControlLabel