IOS provides a text box, buttons, view with dotted border, and ios dotted line
How can I add a dotted border to the IOS view control or set it based on the control size?
Application Scenario: the size of a text textField must be changed based on the text size. The border is a dotted border.
In this case, you may consider creating a dotted background and stretching it as the border of textField.
The idea is good, but it always appears that there is always at the stretch point, right when it is white or black. This is also true for the white strip after the stretch.
At this time, we are considering whether we can draw a dotted background image in real time to set the textField background.
The answer is yes.
I also found some relevant information on the Internet and found that nothing was scattered, but no one posted a complete and available solution.
Therefore, I wrote a classification for UIImage. You can call it directly.
Code:
+ (UIImage*)imageWithSize:(CGSize)size borderColor:(UIColor *)color borderWidth:(CGFloat)borderWidth{ UIGraphicsBeginImageContextWithOptions(size, NO, 0.0); [[UIColor clearColor] set]; CGContextRef context = UIGraphicsGetCurrentContext(); CGContextBeginPath(context); CGContextSetLineWidth(context, borderWidth); CGContextSetStrokeColorWithColor(context, color.CGColor); CGFloat lengths[] = { 3, 1 }; CGContextSetLineDash(context, 0, lengths, 1); CGContextMoveToPoint(context, 0.0, 0.0); CGContextAddLineToPoint(context, size.width, 0.0); CGContextAddLineToPoint(context, size.width, size.height); CGContextAddLineToPoint(context, 0, size.height); CGContextAddLineToPoint(context, 0.0, 0.0); CGContextStrokePath(context); UIImage* image = UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext(); return image;}
This is the implementation of the method. The first parameter is the size of the dotted border view, the second parameter is the border color, and the third parameter is the Border width.
You can use UIImage to call the classification created as a UIImage.
Original article. For more information, see the source!