将底线边框添加到TextView-iOS

Ali*_*him 6 uitextview ios swift

将底线边框添加到的最佳方法是TextView什么?我已经尝试过了,但似乎行不通。它受滚动能力的影响,textview并且在TextView

func addBottomBorderWithColor(color: UIColor, width: CGFloat) {
        let border = CALayer()
        border.backgroundColor = color.CGColor
        border.frame = CGRectMake(0, self.frame.size.height - width, self.frame.size.width, width)
        self.layer.addSublayer(border)
        self.layer.masksToBounds = true
 }
Run Code Online (Sandbox Code Playgroud)

编辑:TextView具有动态高度。

Car*_*oni 5

Swift 4版本

func addBottomBorderWithColor() {
    let border = CALayer()
    border.backgroundColor = UIColor.black.cgColor
    border.frame = CGRect(x: 0, y: yourTextArea.frame.height - 1, width: yourTextArea.frame.width, height: 1)
    yourTextArea.layer.addSublayer(border)
    self.view.layer.masksToBounds = true
}
Run Code Online (Sandbox Code Playgroud)

  • 当用户滚动大量文本时边框是错误的 (2认同)

Cok*_*eoi 4

更新:

我对最初的解决方案进行了很多思考。

如果您要子类化UITextView以添加底线,那么底线最好是文本视图本身的子视图,而不是其超级视图的子视图。

最后,我找到了一种解决方案,可以将底线添加为TextView自身的子视图,并且当用户滚动 的文本时,底线不会移动TextView。在你的视图控制器中,你还可以动态改变框架TextView,底线也会粘在底部。

这是参考代码:

import UIKit

class TextView: UITextView {

    var border: UIView
    var originalBorderFrame: CGRect
    var originalInsetBottom: CGFloat

    deinit {
        removeObserver(self, forKeyPath: "contentOffset")
    }

    override var frame: CGRect {
        didSet {
            border.frame = CGRectMake(0, frame.height+contentOffset.y-border.frame.height, frame.width, border.frame.height)
            originalBorderFrame  = CGRectMake(0, frame.height-border.frame.height, frame.width, border.frame.height);
        }
    }

    override func observeValueForKeyPath(keyPath: String?, ofObject object: AnyObject?, change: [String : AnyObject]?, context: UnsafeMutablePointer<Void>) {
        if keyPath == "contentOffset" {
            border.frame = CGRectOffset(originalBorderFrame, 0, contentOffset.y)
        }
    }

    func addBottomBorderWithColor(color: UIColor, width: CGFloat) {
        border.backgroundColor = color
        border.frame = CGRectMake(0, frame.height+contentOffset.y-width, self.frame.width, width)
        originalBorderFrame = CGRectMake(0, frame.height-width, self.frame.width, width)
        textContainerInset.bottom = originalInsetBottom+width
    }
}
Run Code Online (Sandbox Code Playgroud)

注意:由于我以前用 Objective-C 编写代码,所以对 Swift 不太熟悉。上面的代码仅供大家参考(虽然我已经测试了相应的 Objective-C 代码,并且它按预期工作):

  • 如您所见,没有初始化代码。我尝试编写这样的代码,但它总是显示错误,我仍然不知道。只需确保将以下代码添加到您的TextView初始化代码中:

    border = UIView()
    addSubview(border)
    originalInsetBottom = textContainerInset.bottom
    addObserver(self, forKeyPath: "contentOffset", options: .New, context: nil)
    
    Run Code Online (Sandbox Code Playgroud)
  • 我不熟悉可选值、包装、展开...的概念,因此如果需要?,您应该!在代码中添加 , 。

原答案:

self你的代码中的意思是什么TextView

如果是这样,当您添加border为 的子图层时TextViewborder当用户滚动 的文本时, 将会上下移动TextView

尝试添加为的超级视图border的子层而不是其本身。TextViewTextView

这是代码(请注意,我border从更改CALayerUIView):

func addBottomBorderWithColor(color: UIColor, width: CGFloat) {
     let border = UIView()
     border.frame = CGRectMake(self.frame.origin.x, self.frame.origin.y+self.frame.height-width, textView.frame.width, width)
     border.backgroundColor = color
     self.superview!.insertSubview(border, aboveSubview: textView)
 }
Run Code Online (Sandbox Code Playgroud)

这是捕获的内容:

在此输入图像描述

附言。我建议您将第二个参数名称从宽度更改为高度,因为宽度在这种情况下不明确。