UITextField Only Top And Bottom Border

后端 未结 11 1007
温柔的废话
温柔的废话 2020-12-07 22:46

I currently have a regular border. I would like to only have a top and bottom border.

How do I accomplish this?

Using the UITextField<

11条回答
  •  天涯浪人
    2020-12-07 23:50

    You can use layers to add lines / shapes to any UIView subclass. This code draws two lines at the top and bottom of a text field. You can add it to a subclass of a control, or call this directly in a parent view / view controller.

    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];
    

提交回复
热议问题