UITextfield borderColor and width on one side of t

2019-04-29 23:12发布

I have a text field setup like so:

textField.borderStyle = UITextBorderStyleLine;
textField.layer.borderColor = [[UIColor greenColor] CGColor];
textField.layer.borderWidth= 10.0f;'

But is it possible to just have a larger border on the left side and it be a different color? Or do I have to position a drawRect there with the color I want and position?

5条回答
唯我独甜
2楼-- · 2019-04-29 23:52

try with this code might be help's you add Border left side of custom TextFiled like Bellow:-

- (void)viewDidLoad
{

    [super viewDidLoad];
    UIView *bottomBorder = [[UIView alloc]
                            initWithFrame:CGRectMake(0,0,4,txtField.frame.size.height)];
    bottomBorder.backgroundColor = [UIColor redColor];
    [txtField addSubview:bottomBorder];

}

This Bellow delegate Code for start after some space in textfield

- (void)textFieldDidBeginEditing:(UITextField *)textField {
    UIView *paddingView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 5, 20)];
    textField.leftView = paddingView;
    textField.leftViewMode = UITextFieldViewModeAlways;
}

OUTPUT look like this:-

enter image description here

查看更多
兄弟一词,经得起流年.
3楼-- · 2019-04-29 23:52

Nope, i font think its possible you can Set the borderStyle of your text view to UITextBorderStyleNone and then you can place them on top of whatever you want. In Facebook's case, it looks like they're on top of a table view, but you could put any image behind them.

查看更多
趁早两清
4楼-- · 2019-04-29 23:55

I just wrote this small category to easily add a border of a specified color and thickness on one side of a view. It depends on the excellent PureLayout, since the border resizes automatically with the view and I didn't want to write all this using Auto Layout's native API.

Just call

[myView addBorder:ALEdgeLeft withColor:[UIColor blueColor] width:2.0f];

You can call it several times to add borders on different edges.

查看更多
干净又极端
5楼-- · 2019-04-29 23:55

you can create one image with one sided border and set it to the background of your UITextField :

yourTextField.backgroundColor = 
 [UIColor colorWithPatternImage:[UIImage imageNamed:@"yourBorderedImageName"]];
查看更多
Emotional °昔
6楼-- · 2019-04-29 23:55

I think the best way is adding a category on UIView, but addings borders as views instead of as CALayers so they can resize correctly using AutoresizingMasks.

Check this response: https://stackoverflow.com/a/29033559/1658831

查看更多
登录 后发表回答