Muju Muju - 3 months ago 43
Objective-C Question

How to draw Signature on UIView

I am new in ios.And I need to create a textview or label in which i can sign.enter image description here

Like this Image. thanks in advanced.


You can draw signature on UIView for that first subclass UIView and your subclass of UIView should be something like,


 #import <UIKit/UIKit.h>

@interface SignatureView : UIView{

UIBezierPath *_path;
- (void)erase;


 #import "SignatureView.h"

@implementation SignatureView

- (void)drawRect:(CGRect)rect {

_path.lineCapStyle = kCGLineCapRound;
[_path stroke];
- (id)initWithFrame:(CGRect)frame{

self = [super initWithFrame: frame];

if (self) {

    [self setMultipleTouchEnabled: NO];
    _path = [UIBezierPath bezierPath];
    [_path setLineWidth:2.0];

return self;

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {

UITouch *mytouch=[[touches allObjects] objectAtIndex:0];
[_path moveToPoint:[mytouch locationInView:self]];
[_path addLineToPoint:[mytouch locationInView:self]];
[self setNeedsDisplay];


- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {

UITouch *mytouch=[[touches allObjects] objectAtIndex:0];
[_path addLineToPoint:[mytouch locationInView:self]];
[self setNeedsDisplay];


 - (void)erase {

_path   = nil;  //Set current path nil

_path   = [UIBezierPath bezierPath]; //Create new path
[_path setLineWidth:2.0];
[self setNeedsDisplay];


Then you can import SignatureView.h in any your view controller and can instantiate signature view something like,

   SignatureView *signView= [[ SignatureView alloc] initWithFrame: CGRectMake(10, 10, self.view.frame.size.width-40, 200)];
[signView setBackgroundColor:[UIColor whiteColor]];
signView.layer.borderColor = [[UIColor lightGrayColor]CGColor];
signView.layer.borderWidth = 1.0;
[self.view addSubview:signView];

And on that view you can draw your signature!

And you can call erase method to erase the signature!