IOS provides a text box, buttons, view with dotted border, and ios dotted line

Source: Internet
Author: User

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!

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.