UITextView/UITextField的虚线/虚线边框 [英] Dotted / Dashed border for UITextView / UITextField
问题描述
我想为我的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]];
注意::我已经有了在UITextView
后面添加UIImageView
并在其中设置带有虚线边框的图像的想法.但是我不想那样解决.
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.
推荐答案
例如,您可以尝试以下方法:
You could try, for example, next approach:
1)创建代表边框的图像(例如:一个点和一个空格)
1) Create image that will represent your border (for example: one dot and space)
2)将图像添加到项目中.
2) Add image to project.
3)设置边框(如您问题中的代码所示),并使用图案设置颜色:
3) Set border (as in code in your question) and set color with pattern:
[self.textFieldCardTitle.layer setBorderWidth:6.0];
[self.textFieldCardTitle.layer setBorderColor:[[UIColor colorWithPatternImage:[UIImage imageNamed:@"dashed_white.png"]] CGColor]];
当边框沿4个侧面(左侧,右侧,底部,顶部)绘制时,您应该使用正方形图像:例如,中间的像素是黑色的,周围的像素是透明的.因此,该图像的副本将放置在视图周围.
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.
这篇关于UITextView/UITextField的虚线/虚线边框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!