A small small changes left better impact on UI. In this post we will work on to enhance UI implementation with dashed pattern border on UIView.
Draw dashed pattern border on UIView is very simple and we can use UIBezierPath for draw dashed pattern border.
Lets start, first create subclass of UIView(named DashedBorderView).
#import <UIKit/UIKit.h>
@interface DashedPatternView : UIView
@end
#import "DashedPatternView.h"
@implementation DashedPatternView
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
// Drawing code
}
*/
@end
Go to DashedPatternView.m and uncomment drawRect: method
Now add following code for draw a rectangle
- (void)drawRect:(CGRect)rect {
// Drawing code
[[UIColor blackColor] setStroke];
UIBezierPath *path = [UIBezierPath bezierPath];
[path moveToPoint:CGPointMake(10, 10)];
[path addLineToPoint:CGPointMake(rect.size.width, 10)];
[path addLineToPoint:CGPointMake(rect.size.width, rect.size.height - 10)];
[path addLineToPoint:CGPointMake(10, rect.size.height - 10)];
[path closePath];
path.lineWidth = 2.0;
[path stroke];
}
Go to your controller class and add DashedPatternView as a subview on main view.
Here code
-(void) addBorderView{
DashedPatternView *dashedView = [[DashedPatternView alloc] init];
dashedView.frame = CGRectMake(100, 300, self.view.frame.size.width-200, 60);
dashedView.backgroundColor = [UIColor clearColor];
[self.view addSubview:dashedView];
}
Call addBorderView() method from viewDidLoad() method.
Run the app and you can see a rectangle with solid black border
Now we are going to add dashed pattern border instead of solid border
Add following code just before to call stroke method
CGFloat dashes[] = { 6,6,6};
[path setLineDash:dashes count:3 phase:0];
above two lines of code enough for draw dashed pattern border.
first we have initialize an array for the dashed border style pattern(you can change the pattern and can see result)
second line of code set pattern for draw with 3 count(Count of pattern array).
Now run the app and you can see a rectangle with dashed pattern border.
Draw dashed pattern border on UIView is very simple and we can use UIBezierPath for draw dashed pattern border.
Lets start, first create subclass of UIView(named DashedBorderView).
#import <UIKit/UIKit.h>
@interface DashedPatternView : UIView
@end
#import "DashedPatternView.h"
@implementation DashedPatternView
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
// Drawing code
}
*/
@end
Go to DashedPatternView.m and uncomment drawRect: method
Now add following code for draw a rectangle
- (void)drawRect:(CGRect)rect {
// Drawing code
[[UIColor blackColor] setStroke];
UIBezierPath *path = [UIBezierPath bezierPath];
[path moveToPoint:CGPointMake(10, 10)];
[path addLineToPoint:CGPointMake(rect.size.width, 10)];
[path addLineToPoint:CGPointMake(rect.size.width, rect.size.height - 10)];
[path addLineToPoint:CGPointMake(10, rect.size.height - 10)];
[path closePath];
path.lineWidth = 2.0;
[path stroke];
}
Go to your controller class and add DashedPatternView as a subview on main view.
Here code
-(void) addBorderView{
DashedPatternView *dashedView = [[DashedPatternView alloc] init];
dashedView.frame = CGRectMake(100, 300, self.view.frame.size.width-200, 60);
dashedView.backgroundColor = [UIColor clearColor];
[self.view addSubview:dashedView];
}
Call addBorderView() method from viewDidLoad() method.
Run the app and you can see a rectangle with solid black border
Now we are going to add dashed pattern border instead of solid border
Add following code just before to call stroke method
CGFloat dashes[] = { 6,6,6};
[path setLineDash:dashes count:3 phase:0];
above two lines of code enough for draw dashed pattern border.
first we have initialize an array for the dashed border style pattern(you can change the pattern and can see result)
second line of code set pattern for draw with 3 count(Count of pattern array).
Now run the app and you can see a rectangle with dashed pattern border.