【问题标题】:Custom UITextView with bottom border带有底部边框的自定义 UITextView
【发布时间】:2018-12-17 01:06:59
【问题描述】:

我正在尝试创建一个自定义 UITextView 类来添加底部边框。使用以下代码,它不显示底部边框。我通过在情节提要中添加视图并根据需要对其进行操作来替代解决方案,但这不是最佳选择,因为我必须在许多地方使用 UITextView 因此我想要自定义 UITextView 类。我有用于自定义 UITextField 的类似代码,它可以工作。

我还需要通过代码更改此边框的颜色。

有什么帮助吗?

import Foundation
import UIKit

class CustomTextView: UITextView {

    var bottomBorder = UIView()

    init(frame: CGRect) {
        super.init(frame: frame, textContainer: nil)
        self.initialize()
    }

    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
    }

    override func awakeFromNib() {
        super.awakeFromNib()
        self.initialize()

        // Setup Bottom-Border
        self.translatesAutoresizingMaskIntoConstraints = false

        bottomBorder = UIView.init(frame: CGRect(x: 0, y: 0, width: 0, height: 0))
        bottomBorder.backgroundColor = .red
        bottomBorder.translatesAutoresizingMaskIntoConstraints = false

        addSubview(bottomBorder)

        bottomBorder.bottomAnchor.constraint(equalTo: bottomAnchor).isActive = true
        bottomBorder.leftAnchor.constraint(equalTo: leftAnchor).isActive = true
        bottomBorder.rightAnchor.constraint(equalTo: rightAnchor).isActive = true
        bottomBorder.heightAnchor.constraint(equalToConstant: 1).isActive = true // Set Border-Strength
    }

    func initialize() {
    }

}

【问题讨论】:

  • 我多次使用底部边框。所以大多数时候我只是简单地取一个UIImage view蚂蚁,只需将黑色订单图像设置为UIImageView,并将高度设置为2px。这不是我所知道的正确方法。但这也是最简单的方法。

标签: ios swift uitextview


【解决方案1】:

您必须在 superview 中添加 bottomBorder。

所以用新的替换下面的行。

addSubview(bottomBorder)

到

self.superview!.addSubview(bottomBorder)

您的文本视图中会出现红色边框。

【讨论】:

    【解决方案2】:

    由于您所做的只是添加底部边框,因此您可以根据需要将其直接添加到 UITextView。您可以将其作为 InspectableVar 执行,以便您可以在情节提要的属性选项卡中为每个文本视图设置它。

    private var borders = [UITextView: Bool]()
    
    extension UITextView {
    
      @IBInspectable var showBottomBorder: Bool {
        get {
          guard let b = borders[self] else {
            return true
          }
          return b
        }
        set {
          borders[self] = newValue
          setUpBottomBorder()
        }
      }
    
      func setUpBottomBorder(){
        let border = UIView()
    
        border.translatesAutoresizingMaskIntoConstraints = false
        border.backgroundColor = UIColor.red
        self.addSubview(border)
    
        border.heightAnchor.constraint(equalToConstant: 1).isActive = true
        border.bottomAnchor.constraint(equalTo: self.bottomAnchor).isActive = true
        border.leadingAnchor.constraint(equalTo: self.leadingAnchor).isActive = true
        border.trailingAnchor.constraint(equalTo: self.trailingAnchor).isActive = true
      }
    }
    

    【讨论】:

    • 我还需要改变这个底部边框的颜色。我如何在代码中做到这一点?
    • 在 setupBottomBorder 函数中将颜色设置为红色。您可以将其更改为您想要的任何内容,或者如果您需要它是动态的,您可以创建另一个 IBInspectable var,只需复制“showBottomBorder”,它会返回 UIColor 而不是 Bool。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-03-04
    • 1970-01-01
    • 2023-03-21
    • 1970-01-01
    • 2012-12-08
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多