UITextField只有顶部和底部边框

Gan*_*ham 26 cocoa-touch objective-c uitextfield ios

我目前有一个常规的边界.我想只有一个顶部和底部边框.

我该如何做到这一点?

使用UITextFieldlayer属性,我有以下代码:

    self.layer.borderColor = [[UIColor colorWithRed:160/255.0f green:160/255.0f blue:160/255.0f alpha:1.0f] CGColor];
    self.layer.borderWidth = 4.0f;
Run Code Online (Sandbox Code Playgroud)

通过让我的UITextField超长,让用户看不到左右边框,我有点工作,但我只是想知道是否有更好的,更少的hackish方式这样做?

检查了文档,并且更改了UITextField's' borderStyle没有这个选项.

从,

iOS第一计时器

小智 76

我发现一种方法很好用的是使用图层.这是一个片段:

CALayer *bottomBorder = [CALayer layer];
bottomBorder.frame = CGRectMake(0.0f, self.frame.size.height - 1, self.frame.size.width, 1.0f);
bottomBorder.backgroundColor = [UIColor blackColor].CGColor;
[myTextField.layer addSublayer:bottomBorder];
Run Code Online (Sandbox Code Playgroud)

希望这有助于某人.


Seb*_*ddd 40

@ user3075378在Swift中非常简单的例子

var bottomBorder = CALayer()
bottomBorder.frame = CGRectMake(0.0, textField.frame.size.height - 1, textField.frame.size.width, 1.0);
bottomBorder.backgroundColor = UIColor.blackColor().CGColor
textField.layer.addSublayer(bottomBorder)
Run Code Online (Sandbox Code Playgroud)

  • 因为可能有更多人使用Swift而不是Objective-C.我很高兴他把它发布在Swift中 (14认同)
  • @kareem为什么?问题是标记为objective-c,它也用在问题中. (11认同)

Avi*_*oss 22

@Sebyddd为何停在那里?(;

编辑:有一个与线的问题之前,自动布局设置视图右侧框架正在制定,我编辑我的答案与修复:它主要涉及调用drawLines()layoutSubviews():

class FramedTextField: UITextField {

    @IBInspectable var linesWidth: CGFloat = 1.0 { didSet{ drawLines() } }

    @IBInspectable var linesColor: UIColor = UIColor.blackColor() { didSet{ drawLines() } }

    @IBInspectable var leftLine: Bool = false { didSet{ drawLines() } }
    @IBInspectable var rightLine: Bool = false { didSet{ drawLines() } }
    @IBInspectable var bottomLine: Bool = false { didSet{ drawLines() } }
    @IBInspectable var topLine: Bool = false { didSet{ drawLines() } }



    func drawLines() {

        if bottomLine {
            add(CGRectMake(0.0, frame.size.height - linesWidth, frame.size.width, linesWidth))
        }

        if topLine {
            add(CGRectMake(0.0, 0.0, frame.size.width, linesWidth))
        }

        if rightLine {
            add(CGRectMake(frame.size.width - linesWidth, 0.0, linesWidth, frame.size.height))
        }

        if leftLine {
            add(CGRectMake(0.0, 0.0, linesWidth, frame.size.height))
        }

    }

    typealias Line = CGRect
    private func add(line: Line) {
        let border = CALayer()
        border.frame = line
        border.backgroundColor = linesColor.CGColor
        layer.addSublayer(border)
    }

    override func layoutSubviews() {
        super.layoutSubviews()
        drawLines()
    }

}
Run Code Online (Sandbox Code Playgroud)


KDe*_*kar 9

您可以创建一个具有顶部和底部边框的图像,并将其设置为UITextField的背景:

yourTextField.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"yourBorderedImageName"]];
Run Code Online (Sandbox Code Playgroud)


小智 5

您还可以在顶部和底部添加视图以用作边框.

// Top border
UIView *topBorder = [[UIView alloc]
    initWithFrame:CGRectMake(0,
                             0,
                             textfield.frame.size.width,
                             4.0f)];
topBorder.backgroundColor = [UIColor colorWithRed:160/255.0f
                                            green:160/255.0f
                                             blue:160/255.0f
                                            alpha:1.0f];
[textfield addSubview:topBorder];

// Bottom border
UIView *bottomBorder = [[UIView alloc]
    initWithFrame:CGRectMake(0,
                             textfield.frame.origin.y +
                                 textfield.frame.size.height - 4.0f,
                             textfield.frame.size.width,
                             4.0f)];
bottomBorder.backgroundColor = [UIColor colorWithRed:160/255.0f
                                               green:160/255.0f
                                                blue:160/255.0f
                                               alpha:1.0f];
[textfield addSubview:bottomBorder];
Run Code Online (Sandbox Code Playgroud)

  • 非常好,谢谢你的想法.请注意:由于您将边框添加为UITextField本身的子视图,因此您可能不希望根据文本字段的原点定位边框,因为它指的是其父级的坐标空间.您希望顶部边框从(0,0)开始,底部开始于:(textfield.frame.size.height - 4.0f) (2认同)

Eli*_*rke 5

您可以使用图层将线条/形状添加到任何UIView子类。此代码在文本字段的顶部和底部绘制两行。您可以将其添加到控件的子类中,或直接在父视图/视图控制器中调用它。

CGRect layerFrame = CGRectMake(0, 0, _usernameField.frame.size.width, _usernameField.frame.size.height);
CGMutablePathRef path = CGPathCreateMutable();
CGPathMoveToPoint(path, NULL, 0, 0);
CGPathAddLineToPoint(path, NULL, layerFrame.size.width, 0); // top line
CGPathMoveToPoint(path, NULL, 0, layerFrame.size.height);
CGPathAddLineToPoint(path, NULL, layerFrame.size.width, layerFrame.size.height); // bottom line
CAShapeLayer * line = [CAShapeLayer layer];
line.path = path;
line.lineWidth = 2;
line.frame = layerFrame;
line.strokeColor = [UIColor blackColor].CGColor;
[_usernameField.layer addSublayer:line];
Run Code Online (Sandbox Code Playgroud)