I want to set a Dotted / Dashed border for my UITextField
and UITextView
.
我想为UITextField和UITextView设置一个点/虚线的边界。
How can I do that? I know that, I can set border with this line of code:
我怎么做呢?我知道,我可以用这行代码设置边框:
[self.textFieldCardTitle.layer setBorderWidth:1.0];
[self.textFieldCardTitle.layer setBorderColor:[[UIColor whiteColor] CGColor]];
Notice: I already have the idea to add a UIImageView
behind the UITextView
and set there an image with dashed border. But I don't want to solve it that way.
注意:我已经有了在UITextView后面添加UIImageView并设置一个带虚线边框的图像的想法。但我不想用这种方法求解。
3 个解决方案
#1
8
You could try, for example, next approach:
你可以尝试下一个方法:
1) Create image that will represent your border (for example: one dot and space)
1)创建表示边框的图像(例如:一个点和一个空格)
2) Add image to project.
2)将图像添加到项目中。
3) Set border (as in code in your question) and set color with pattern:
3)设置边框(如您问题中的代码),用图案设置颜色:
[self.textFieldCardTitle.layer setBorderWidth:6.0];
[self.textFieldCardTitle.layer setBorderColor:[[UIColor colorWithPatternImage:[UIImage imageNamed:@"dashed_white.png"]] CGColor]];
As border is drawn along 4 sides (left, right, bottom, top) you should use square image: for example, pixel in middle is black and pixels around it are transparent. So copies of that image will be placed around the view.
当边框沿着4条边(左、右、下、上)绘制时,您应该使用方形图像:例如,中间的像素是黑色的,周围的像素是透明的。所以图像的拷贝会被放置在视图的周围。
#2
4
Just use following code for Dotted / Dashed Border around UIView
or UITextField
or any other view:-
只需使用以下代码为点/虚线边界围绕UIView或UITextField或任何其他视图:-
CAShapeLayer * _border = [CAShapeLayer layer];
_border.strokeColor = [UIColor redColor].CGColor;
_border.fillColor = nil;
_border.lineDashPattern = @[@4, @2];
[YOURVIEW.layer addSublayer:_border];
//for a square effect
_border.path = [UIBezierPath bezierPathWithRect:YOURVIEW.bounds].CGPath;
//for a rounded effect
//_border.path = [UIBezierPath bezierPathWithRoundedRect:YOURVIEW.bounds cornerRadius:txtUserName.frame.size.height / 2].CGPath;
_border.frame = YOURVIEW.bounds;
For more details, Refer this Answer.
有关更多细节,请参考以下答案。
Hope, this is what you're looking for. Any concern get back to me. :)
希望,这就是你要找的。任何关心的事都可以告诉我。:)
#3
3
Here's what I did with Swift:
以下是我对斯威夫特做的:
self.textFieldCardTitle.layer.borderWidth = 3
self.textFieldCardTitle.layer.borderColor = (UIColor(patternImage: UIImage(named: "dot")!)).CGColor
Free bonus, I attached the pics below. Unless * changes its background, you probably won't see them as they are white squares on a white background, so you'll find the urls below as well.
免费的奖金,我附上下面的图片。除非*改变它的背景,否则你可能不会看到它们,因为它们是白色背景上的白色方块,所以你也会找到下面的url。
- http://i.stack.imgur.com/X7PfM.png -> rename it to dot.png
- http://i.stack.imgur.com/X7PfM.png ->重命名为dot.png
- http://i.stack.imgur.com/IemhF.png -> rename it to dot@2x.png
- http://i.stack.imgur.com/IemhF.png ->重命名为dot@2x.png
- http://i.stack.imgur.com/CSjZT.png -> rename it to dot@3x.png
- http://i.stack.imgur.com/CSjZT.png ->将其重命名为dot@3x.png。
#1
8
You could try, for example, next approach:
你可以尝试下一个方法:
1) Create image that will represent your border (for example: one dot and space)
1)创建表示边框的图像(例如:一个点和一个空格)
2) Add image to project.
2)将图像添加到项目中。
3) Set border (as in code in your question) and set color with pattern:
3)设置边框(如您问题中的代码),用图案设置颜色:
[self.textFieldCardTitle.layer setBorderWidth:6.0];
[self.textFieldCardTitle.layer setBorderColor:[[UIColor colorWithPatternImage:[UIImage imageNamed:@"dashed_white.png"]] CGColor]];
As border is drawn along 4 sides (left, right, bottom, top) you should use square image: for example, pixel in middle is black and pixels around it are transparent. So copies of that image will be placed around the view.
当边框沿着4条边(左、右、下、上)绘制时,您应该使用方形图像:例如,中间的像素是黑色的,周围的像素是透明的。所以图像的拷贝会被放置在视图的周围。
#2
4
Just use following code for Dotted / Dashed Border around UIView
or UITextField
or any other view:-
只需使用以下代码为点/虚线边界围绕UIView或UITextField或任何其他视图:-
CAShapeLayer * _border = [CAShapeLayer layer];
_border.strokeColor = [UIColor redColor].CGColor;
_border.fillColor = nil;
_border.lineDashPattern = @[@4, @2];
[YOURVIEW.layer addSublayer:_border];
//for a square effect
_border.path = [UIBezierPath bezierPathWithRect:YOURVIEW.bounds].CGPath;
//for a rounded effect
//_border.path = [UIBezierPath bezierPathWithRoundedRect:YOURVIEW.bounds cornerRadius:txtUserName.frame.size.height / 2].CGPath;
_border.frame = YOURVIEW.bounds;
For more details, Refer this Answer.
有关更多细节,请参考以下答案。
Hope, this is what you're looking for. Any concern get back to me. :)
希望,这就是你要找的。任何关心的事都可以告诉我。:)
#3
3
Here's what I did with Swift:
以下是我对斯威夫特做的:
self.textFieldCardTitle.layer.borderWidth = 3
self.textFieldCardTitle.layer.borderColor = (UIColor(patternImage: UIImage(named: "dot")!)).CGColor
Free bonus, I attached the pics below. Unless * changes its background, you probably won't see them as they are white squares on a white background, so you'll find the urls below as well.
免费的奖金,我附上下面的图片。除非*改变它的背景,否则你可能不会看到它们,因为它们是白色背景上的白色方块,所以你也会找到下面的url。
- http://i.stack.imgur.com/X7PfM.png -> rename it to dot.png
- http://i.stack.imgur.com/X7PfM.png ->重命名为dot.png
- http://i.stack.imgur.com/IemhF.png -> rename it to dot@2x.png
- http://i.stack.imgur.com/IemhF.png ->重命名为dot@2x.png
- http://i.stack.imgur.com/CSjZT.png -> rename it to dot@3x.png
- http://i.stack.imgur.com/CSjZT.png ->将其重命名为dot@3x.png。