Reputation: 9355
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
's layer
property, I have the following code:
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;
I have kind of got it to work by making my UITextField extra long, so that the user does not see the left and right borders, but I was just wondering if there was a better, less hackish way of doing this?
I have checked the docs, and changing a UITextField
's borderStyle
does not have this option.
From,
An iOS First Timer
Upvotes: 26
Views: 42247
Reputation: 67
Updated for Swift 5
var bottomBorder = CALayer()
bottomBorder.frame = CGRect(x: xVal, y: yVal, width: w, height: h)
bottomBorder.backgroundColor = UIColor.black.cgColor
searchBar.layer.addSublayer(bottomBorder)
Upvotes: 0
Reputation: 1828
Swift 3: Clean with AutoLayout (I'm using here PureLayout but you can also do it with common NSLayoutConstraint:
func makeUnderline(for textField: UITextField) {
let borderLine = UIView()
borderLine.backgroundColor = UIColor.black
borderLine.autoSetDimension(.height, toSize: 1)
textField.addSubview(borderLine)
borderLine.autoPinEdgesToSuperviewEdges(with: UIEdgeInsets.zero, excludingEdge: .top)
}
Upvotes: 2
Reputation: 24205
Thanks to user3075378. my answer is based on his. adding right and left small lines.
- (void)styleSearchTextField {
CALayer *bottomBorder = [CALayer layer], *leftBorder = [CALayer layer], *rightBorder = [CALayer layer];
CGFloat thickness = 1.0f;
CGFloat side_height = 6.0f;
bottomBorder.frame = CGRectMake(0.0f, searchTextField.frame.size.height - 1, searchTextField.frame.size.width, thickness);
leftBorder.frame = CGRectMake(0.0f, searchTextField.frame.size.height - side_height, thickness, searchTextField.frame.size.height - 1);
rightBorder.frame = CGRectMake(searchTextField.frame.size.width - 1, searchTextField.frame.size.height - side_height, thickness, searchTextField.frame.size.height - 1);
bottomBorder.backgroundColor = [self.stylingDetails themeGrayColor].CGColor;
leftBorder.backgroundColor = [self.stylingDetails themeGrayColor].CGColor;
rightBorder.backgroundColor = [self.stylingDetails themeGrayColor].CGColor;
[searchTextField.layer addSublayer:bottomBorder];
[searchTextField.layer addSublayer:leftBorder];
[searchTextField.layer addSublayer:rightBorder];
}
Upvotes: 0
Reputation: 1374
Hope this helps, put this inside a textfield override class
UIView *view = [[UIView alloc] init];
view.translatesAutoresizingMaskIntoConstraints = NO;
view.layer.borderWidth = 1;
view.backgroundColor = [UIColor blackColor];
[self addSubview:view];
[self addConstraint:[NSLayoutConstraint constraintWithItem:view attribute:NSLayoutAttributeBottom relatedBy:NSLayoutRelationEqual toItem:self attribute:NSLayoutAttributeBottom multiplier:1.0 constant:0.0]];
[self addConstraint:[NSLayoutConstraint constraintWithItem:view attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem:self attribute:NSLayoutAttributeWidth multiplier:1.0 constant:0.0]];
[self addConstraint:[NSLayoutConstraint constraintWithItem:view attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeHeight multiplier:1.0 constant:1.0]];
[self addConstraint:[NSLayoutConstraint constraintWithItem:view attribute:NSLayoutAttributeCenterX relatedBy:NSLayoutRelationEqual toItem:self attribute:NSLayoutAttributeCenterX multiplier:1.0 constant:1.0]];
Upvotes: 1
Reputation: 4315
@user3075378's great & simple example in 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)
Upvotes: 42
Reputation: 9965
@Sebyddd why stop there? (;
EDIT: There is an issue with lines being drawn before auto layout sets the right frame for the view, I edited my answer with a fix: it basically involves calling drawLines()
in 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()
}
}
Upvotes: 23
Reputation: 786
One approach I have found works good is using layers. Here's a snippet:
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];
Hope this helps someone.
Upvotes: 77
Reputation: 151
You can also add views to the top and bottom to use as borders.
// 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];
Upvotes: 5
Reputation: 2789
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];
Upvotes: 5
Reputation: 97
I will suggest you put one view on the left side of the textfield and one view on the right side of the textfield to cover the left/right border.
UIView *v1 = [[UIView all] initWithFrame:CGRectMake(textfield.frame.origin.x - 5, textfield.frame.origin.y, 10, textifield.frame.size.height)];
UIView *v2 = [[UIView all] initWithFrame:CGRectMake(textfield.frame.origin.x + textfield.frame.size.with - 5, textfield.frame.origin.y, 10, textifield.frame.size.height)];
Upvotes: 1
Reputation: 10959
you can create one image that with top and bottom border and set it to the background of your UITextField :
yourTextField.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"yourBorderedImageName"]];
Upvotes: 9