将底线边框添加到TextView-iOS [英] Adding Bottom Line Border To A TextView - iOS

查看:40
本文介绍了将底线边框添加到TextView-iOS的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

what is the best way to add a bottom line border to a TextView? i have tried this but it seems it doesn't work. It is affected by the scrolling ability of the textview and the line is being drawn in the middle of the 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
 }

TextView具有动态高度.

The TextView has a dynamic height.

推荐答案

更新:

我对我的原始解决方案考虑了很多.

I thought a lot about my original solution.

如果要对 UITextView 进行子类化以添加底线,则底线最好是文本视图本身的子视图,而不是其超级视图的子视图.

If you are subclassing UITextView to add a bottom line, then the bottom line had better be a subview of the text view itself, rather than its super view's.

最后,我想出了一种可以将底行添加为 TextView 本身的子视图的解决方案,并且当用户滚动 TextView .在视图控制器中,您还可以动态更改 TextView 的框架,并且底线也将停留在底部.

And finally, I figure out one solution that can add bottom line as a subview of TextView itself and the bottom line will not move when the user scrolls the text of TextView. In your view controller, you can also change the frame of TextView dynamically, and the bottom line will also stick to the the bottom.

这是参考代码:

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
    }
}

注意:由于我以前是用Objective-C编写代码的,所以我对Swift并不熟悉.上面的代码仅供您参考(尽管我已经测试了相应的Objective-C代码,并且可以按预期工作):

Note: Since I used to write code in Objective-C, I am not familiar with Swift. The code above is only for your reference (though I have tested the corresponding Objective-C code, and it works as expected):

  • 如您所见,没有初始化代码.我试图编写这样的代码,但是它始终显示错误,对此我仍然一无所知.只需确保将以下代码添加到您的 TextView 初始化代码中即可:

border = UIView()
addSubview(border)
originalInsetBottom = textContainerInset.bottom
addObserver(self, forKeyPath: "contentOffset", options: .New, context: nil)

  • 我不熟悉可选值的概念,包括包装,展开...,因此,如果需要,您应该在代码中添加?.

    原始答案:

    您代码中的 self 是否表示 TextView ?

    如果是这样,当您将 border 添加为 TextView 的子层时,当用户滚动浏览器时, border 将上下移动. TextView 的文本.

    If so, when you add border as a sublayer of the TextView, the border will move up and down when the user scrolls the text of TextView.

    尝试添加 border 作为 TextView 的超级视图的子层,而不是 TextView 本身.

    Try to add border as a sublayer of TextView's super view rather than TextView itself.

    这是代码(请注意,我将 border CALayer 更改为 UIView ):

    Here is the code (Note that I change border from CALayer to UIView):

    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)
     }
    

    以下是捕获内容:

    PS.我建议您将第二个参数名从width更改为height,因为在这种情况下width是不明确的.

    PS. I suggest you to change the second paramter name from width to height since width is ambiguous in this context.

    这篇关于将底线边框添加到TextView-iOS的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

  • 查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆