How to draw Signature on UIView - objective-c

I am new in ios.And I need to create a textview or label in which i can sign.
Like this image.

You can draw signature on UIView for that first subclass UIView and your subclass of UIView should be something like,
SignatureView.h
#import <UIKit/UIKit.h>
#interface SignatureView : UIView{
UIBezierPath *_path;
}
- (void)erase;
#end
SignatureView.m
#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!

This my solution.
First I created the SignatureDrawView class.In side the SignatureDrawView class I wrote the function for the draw the signature.
SignatureDrawView.h
#import <UIKit/UIKit.h>
#interface SignatureDrawView : UIView
#property (nonatomic, retain) UIGestureRecognizer *theSwipeGesture;
#property (nonatomic, retain) UIImageView *drawImage;
#property (nonatomic, assign) CGPoint lastPoint;
#property (nonatomic, assign) BOOL mouseSwiped;
#property (nonatomic, assign) NSInteger mouseMoved;
- (void)erase;
- (void)setSignature:(NSData *)theLastData;
- (BOOL)isSignatureWrite;
#end
SignatureDrawView.m
#import "SignatureDrawView.h"
#implementation SignatureDrawView
#synthesize theSwipeGesture;
#synthesize drawImage;
#synthesize lastPoint;
#synthesize mouseSwiped;
#synthesize mouseMoved;
#pragma mark - View lifecycle
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
// Initialization code
}
return self;
}
- (id)initWithCoder:(NSCoder*)coder
{
if ((self = [super initWithCoder:coder]))
{
drawImage = [[UIImageView alloc] initWithImage:nil];
drawImage.frame = CGRectMake(0, 0, self.frame.size.width, self.frame.size.height);
[self addSubview:drawImage];
self.backgroundColor = [UIColor whiteColor];
mouseMoved = 0;
}
return self;
}
#pragma mark touch handling
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
for (UITouch *touch in touches)
{
NSArray *array = touch.gestureRecognizers;
for (UIGestureRecognizer *gesture in array)
{
if (gesture.enabled & [gesture isMemberOfClass:[UISwipeGestureRecognizer class]])
{
gesture.enabled = NO;
self.theSwipeGesture = gesture;
}
}
}
mouseSwiped = NO;
UITouch *touch = [touches anyObject];
lastPoint = [touch locationInView:self];
}
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
mouseSwiped = YES;
UITouch *touch = [touches anyObject];
CGPoint currentPoint = [touch locationInView:self];
UIGraphicsBeginImageContext(self.frame.size);
[drawImage.image drawInRect:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)];
CGContextSetLineCap(UIGraphicsGetCurrentContext(), kCGLineCapRound);
CGContextSetLineWidth(UIGraphicsGetCurrentContext(), 3.0);
CGContextSetRGBStrokeColor(UIGraphicsGetCurrentContext(), 0.0, 0.0, 0.0, 1.0);
CGContextBeginPath(UIGraphicsGetCurrentContext());
CGContextMoveToPoint(UIGraphicsGetCurrentContext(), lastPoint.x, lastPoint.y);
CGContextAddLineToPoint(UIGraphicsGetCurrentContext(), currentPoint.x, currentPoint.y);
CGContextStrokePath(UIGraphicsGetCurrentContext());
drawImage.image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
lastPoint = currentPoint;
mouseMoved++;
if (mouseMoved == 10) {
mouseMoved = 0;
}
}
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
if(!mouseSwiped)
{
UIGraphicsBeginImageContext(self.frame.size);
[drawImage.image drawInRect:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)];
CGContextSetLineCap(UIGraphicsGetCurrentContext(), kCGLineCapRound);
CGContextSetLineWidth(UIGraphicsGetCurrentContext(), 3.0);
CGContextSetRGBStrokeColor(UIGraphicsGetCurrentContext(), 0.0, 0.0, 0.0, 1.0);
CGContextMoveToPoint(UIGraphicsGetCurrentContext(), lastPoint.x, lastPoint.y);
CGContextAddLineToPoint(UIGraphicsGetCurrentContext(), lastPoint.x, lastPoint.y);
CGContextStrokePath(UIGraphicsGetCurrentContext());
CGContextFlush(UIGraphicsGetCurrentContext());
drawImage.image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
}
self.theSwipeGesture.enabled = YES;
mouseSwiped = YES;
}
#pragma mark Methods
- (void)erase
{
mouseSwiped = NO;
drawImage.image = nil;
}
- (void)setSignature:(NSData *)theLastData
{
UIImage *image = [UIImage imageWithData:theLastData];
if (image != nil)
{
drawImage.image = [UIImage imageWithData:theLastData];
mouseSwiped = YES;
}
}
- (BOOL)isSignatureWrite
{
return mouseSwiped;
}
#end
Next in ViewController I created the UIImageView with UIView.
ViewController.h
#import <UIKit/UIKit.h>
#import "SignatureDrawView.h"
#interface ViewController : UIViewController
#property (strong, nonatomic) IBOutlet SignatureDrawView *drawSignView;
#end
ViewController.m
#import "ViewController.h"
#interface ViewController ()
#end
#implementation ViewController
#synthesize drawSignView;
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (IBAction)actionSave:(id)sender
{
// code for save the signature
UIGraphicsBeginImageContext(self.drawSignView.bounds.size);
[[self.drawSignView.layer presentationLayer] renderInContext:UIGraphicsGetCurrentContext()];
UIImage *viewImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
NSData *postData = UIImageJPEGRepresentation(viewImage, 1.0);
....Then do your stuff to save this in DB or server
}
- (IBAction)actionCancel:(id)sender
{
//code for cancel the signature
[self.drawSignView erase];
}
- (IBAction)actionClear:(id)sender
{
//code for clear the signature
[self.drawSignView erase];
}
#end
NOTE:When you set the view in xib first identity inspector(it is left
> side in utilities).Then click the drop down box of class(it is part of
Custom Class).Select or choose SignatureDrawView.After that hook up
the view from xib or storyboard to ViewController.h
Below is the output screenshots
Also

Related

Creating a draggable image view

I have been searching the net for a long time and I have not found a concrete way of making an image view draggable. Here is what I have so far:
tempViewController.h
#import <UIKit/UIKit.h>
#import "MyRect.h"
#class UIView;
#interface tempViewController : UIViewController
#property (nonatomic, strong) MyRect *rect1;
#end
tempViewController.m
#import "tempViewController.h"
#interface tempViewController ()
#end
#implementation tempViewController
#synthesize rect1 = _rect1;
- (void)viewDidLoad
{
[super viewDidLoad];
_rect1 = [[MyRect alloc]initWithFrame:CGRectMake(150.0, 100.0, 80, 80)];
[_rect1 setImage:[UIImage imageNamed:#"cloud1.png"]];
[_rect1 setUserInteractionEnabled:YES];
[self.view addSubview:_rect1];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
-(void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *touch = [[event allTouches]anyObject];
if([touch view] == _rect1)
{
CGPoint pt = [[touches anyObject] locationInView:_rect1];
NSLog(#"%#",NSStringFromCGPoint(pt));
_rect1.center = pt;
}
}
-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *touch = [[event allTouches]anyObject];
if([touch view] == _rect1)
{
CGPoint pt = [[touches anyObject] locationInView:_rect1];
NSLog(#"%#",NSStringFromCGPoint(pt));
_rect1.center = pt;
}
}
#end
MyRect right now is an empty UIImageView Class.
Dragging the image from a point such as [532,589] on micron moves it to a totally different part of the screen such as [144, 139]
Just attach a UIPanGestureRecognizer to your view. In the recognizer's action, update your view's center based on the “translation” (offset) of the recognizer, then reset the recognizer's translation to zero. Here's an example:
- (void)viewDidLoad {
[super viewDidLoad];
UIView *draggableView = [[UIView alloc] initWithFrame:CGRectMake(150, 100, 80, 80)];
draggableView.userInteractionEnabled = YES;
draggableView.backgroundColor = [UIColor redColor];
[self.view addSubview:draggableView];
UIPanGestureRecognizer *panner = [[UIPanGestureRecognizer alloc]
initWithTarget:self action:#selector(panWasRecognized:)];
[draggableView addGestureRecognizer:panner];
}
- (void)panWasRecognized:(UIPanGestureRecognizer *)panner {
UIView *draggedView = panner.view;
CGPoint offset = [panner translationInView:draggedView.superview];
CGPoint center = draggedView.center;
draggedView.center = CGPointMake(center.x + offset.x, center.y + offset.y);
// Reset translation to zero so on the next `panWasRecognized:` message, the
// translation will just be the additional movement of the touch since now.
[panner setTranslation:CGPointZero inView:draggedView.superview];
}
Don't use -touchesBegan:withEvent: etc.
What you want to use for such "high-level" things are UIGestureRecognizers.
You would add a pan gesture recognizer to your view set a delegate (possibly the view itself), and in the callback move the view by the distance the recognizer moved.
You either remember the initial position and move by the -translationInView each time, or simply move by the translation and then use -setTranslation:inView: to reset the recognizers translation to zero, so on the next call of the delegate method, you will again get the movement since the last call.

How to access a class instance from another method

Here's my code:
#import "ViewController.h"
#implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
UIView *view1 = [[UIView alloc] initWithFrame:CGRectMake(50.0, 50.0, 0, 0)];
[self.view addSubview:view1];
}
-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *touch = [[event allTouches] anyObject];
if([touch.view isEqual:self]) {
CGPoint point = [[touches anyObject] locationInView:self];
NSLog(#"%#",NSStringFromCGPoint(point));
self.view1.center = point;
}
}
I want to be able to access the instance "view1" of the class "UIView" from the touchesMoved method.
Thanks in advance!
You can declare local variables as stated in the other answer. However, in my opinion its better to declare private properties in the anonymous category as below:
#import "ViewController.h"
#interface ViewController ()
#property (nonatomic, strong) UIView *view1; // declares view1 as a private property
#end
#implementation ViewController
#synthesize view1 = _view1;
- (void)viewDidLoad
{
[super viewDidLoad];
self.view1 = [[UIView alloc] initWithFrame:CGRectMake(50.0, 50.0, 0, 0)];
[self.view addSubview:self.view1];
}
-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *touch = [[event allTouches] anyObject];
if([touch.view isEqual:self])
{
CGPoint point = [[touches anyObject] locationInView:self];
NSLog(#"%#",NSStringFromCGPoint(point));
self.view1.center = point;
}
}
You can make rect1 an instance variable by placing it in your .h file. You can do something like:
#interface tempViewController : UIViewController
{
myRect *rect1;
}
Then in your .m file you will not have to declare it again.

how to build an undo stack in iOS/core graphics

I'm trying to add an undo / redo capability to a set of touches..
I have this code for touchesBegan and moved:
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
NSLog(#"%s", __FUNCTION__);
mouseSwiped = NO;
UITouch *touch = [touches anyObject];
if ([touch tapCount] == 2) {
[self eraseButtonTapped:self];
return;
}
lastPoint = [touch locationInView:self.view];
lastPoint.y -= 20;
[self.undoPath addObject:WHATGOESHERE];
// Remove all paths from redo stack
[self.redoStack removeAllObjects];
}
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
//NSLog(#"%s", __FUNCTION__);
mouseSwiped = YES;
UITouch *touch = [touches anyObject];
CGPoint currentPoint = [touch locationInView:self.view];
currentPoint.y -= 20;
UIGraphicsBeginImageContext(self.view.frame.size);
[drawImage.image drawInRect:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetLineCap(context, kCGLineCapRound);
CGContextSetLineWidth(context, brush);
CGContextSetRGBStrokeColor(context, red, green, blue, 1.0);
CGContextBeginPath(context);
CGContextMoveToPoint(context, lastPoint.x, lastPoint.y);
CGContextAddLineToPoint(context, currentPoint.x, currentPoint.y);
CGContextStrokePath(context);
drawImage.image = UIGraphicsGetImageFromCurrentImageContext();
[self.undoPath addObject:WHATGOESHERE];
UIGraphicsEndImageContext();
NSLog(#"Touches Moved undoPath contains %i objects", [self.undoPath count]);
// Remove all paths from redo stack
[self.redoPath removeAllObjects];
lastPoint = currentPoint;
}
I think that if I can figure how to populate the undo stack, that I can iterate through the stack to undo redo touches.. Maybe I'm all wet. I sure would appreciate some help...
Thanks
..I have asked a similar question before, but I've restarted the project in a different form as the last way was not satisfactory.
I finally solved this by managing arrays.
For each stroke, there is an addition to a buffer array:
[self.currentColoredPath.path moveToPoint:[touch locationInView:self]];
[self.currentArray addObject:self.currentColoredPath];
// Remove all paths from redo stack
[self.redoStack removeAllObjects];
Then the Undo and Redo methods look like this:
-(void)undoButtonClicked
{
//NSLog(#"%s", __FUNCTION__);
if ([self.currentArray count] == 0) {
//nothing to undo
return;
}
DrawingPath *undonePath = [self.currentArray lastObject];
[self.currentArray removeLastObject];
[self.redoStack addObject:undonePath];
[self setNeedsDisplay];
}
-(void)redoButtonClicked
{
//NSLog(#"%s", __FUNCTION__);
if ([self.redoStack count] == 0) {
// nothing to redo
return;
}
DrawingPath *redonePath = [self.redoStack lastObject];
[self.redoStack removeLastObject];
[self.currentArray addObject:redonePath];
[self setNeedsDisplay];
}
I hope this helps others.
UPDATE - This is in response to the question below: "What is currentColoredPath?"
#property (strong,nonatomic) DrawingPath *currentColoredPath;
This refers to a class DrawingPath, which I wrote as follows:
.h
#import <Foundation/Foundation.h>
#interface DrawingPath : NSObject {
NSString *brushSize;
}
#property (strong, nonatomic) NSString *brushSize;
#property (strong,nonatomic) UIColor *color;
#property (strong,nonatomic) UIBezierPath *path;
- (void)draw;
- (void)brushChange;
.m
#import "DrawingPath.h"
#implementation DrawingPath
#synthesize path = _path;
#synthesize color = _color;
#synthesize brushSize = _brushSize;
float brush = 12;
- (id)init {
//NSLog(#"%s", __FUNCTION__);
if (!(self = [super init] ))
return nil;
brushSize = [[NSUserDefaults standardUserDefaults] objectForKey:#"brushKey"];
[self brushChange];
_path = [[UIBezierPath alloc] init];
_path.lineCapStyle=kCGLineCapRound;
_path.lineJoinStyle=kCGLineJoinRound;
[_path setLineWidth:brush];
return self;
}
- (void)draw {
//NSLog(#"%s", __FUNCTION__);
[self.color setStroke];
[self.path stroke];
}
- (void)brushChange { //from notification
//NSLog(#"%s", __FUNCTION__);
brushSize = [[NSUserDefaults standardUserDefaults] objectForKey:#"brushKey"];
//NSLog(#"DrawingPath - brushSize is %#: ", brushSize );
//NSLog(#"DrawingPath - brush is %f: ", brush );
}

Tint a UIView with all its subviews

Any way you can tint a UIView? Not the background color, but the entire UIView with all its subviews.
e.g - A UIView with an animation of a star rotating , i.e The UIView shape is constantly changing.
Eventually I created a UIView category that enables tinting of a UIView,
without filling the UIView rectangle, here it is :
Takes an image representation of the UIView, then colors it in the
given UIColor, this category was created to replicate a UIButton
default highlight behavior, so it also comes with a method that can activate that
behavior and let the category handle all touch methods.
//------------------ .h Interface file ------- //
//
// UIView UIView_Tint.h
// BabyQA
//
// Created by yogev shelly on 8/10/12.
// Copyright (c) 2012 __MyCompanyName__. All rights reserved.
//
#import <UIKit/UIKit.h>
#define tintColorClassicUIButton [UIColor colorWithWhite:0.0 alpha:0.5]
#interface UIView(Tint)
//proprties should not be used, use the methods decalred bellow
#property (nonatomic,retain) UIColor* tintColor;
#property(nonatomic,retain) UIImageView* tintImageView;
#property(nonatomic,assign) BOOL tintOnTouchActive;
-(void)tintToColor:(UIColor*)color;
-(void)clearTint;
-(void)enableTintOnTouchWithColor:(UIColor*)color;
-(void)disableTintOnTouch;
-(UIImage *)imageRepresentation;
-(UIImage*)imageRepresentationWithTintColor:(UIColor*)color;
#end
//------------------ .m Implementation file ------- //
// UIView UIView_Tint.m
// BabyQA
//
// Created by yogev shelly on 8/10/12.
// Copyright (c) 2012 __MyCompanyName__. All rights not reserved - go wild!
//
#import "UIView Tint.h"
#import <objc/runtime.h>
#import <QuartzCore/QuartzCore.h>
static char const * const tintImageViewKey = "tintImageView";
static char const * const tintColorKey = "tintColorKey";
static char const * const tintOnTouchActiveKey = "tintOnTouchActive";
#implementation UIView (Tint)
#dynamic tintImageView;
#dynamic tintColor;
#dynamic tintOnTouchActive;
-(void)enableTintOnTouchWithColor:(UIColor*)color
{
self.tintColor = color;
self.tintOnTouchActive = TRUE;
}
-(void)disableTintOnTouch
{
self.tintOnTouchActive = FALSE;
}
-(void)tintToColor:(UIColor*)color
{
if(![self.tintColor isEqual:color] || !self.tintImageView)
{
self.tintColor = color;
UIImage* tintImage = [self imageRepresentationWithTintColor:self.tintColor];
self.tintImageView = [[[UIImageView alloc] initWithImage:tintImage] autorelease];
}
[self addSubview:self.tintImageView];
}
-(void)clearTint
{
[self.tintImageView removeFromSuperview];
}
-(void)clearTintWithSecondsDelay:(float)delay
{
[self performSelector:#selector(clearTint) withObject:self afterDelay:delay];
}
#pragma mark - TouchToggling
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
[super touchesBegan:touches withEvent:event];
if(self.tintOnTouchActive)
[self tintToColor:self.tintColor];
}
-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
[super touchesEnded:touches withEvent:event];
if(self.tintOnTouchActive)
[self clearTintWithSecondsDelay:0.05];
}
-(void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event
{
[super touchesCancelled:touches withEvent:event];
if(self.tintOnTouchActive)
[self clearTintWithSecondsDelay:0.05];
}
#pragma mark - TintingPart
-(UIImage *)imageRepresentation
{
UIGraphicsBeginImageContext(self.bounds.size);
[self.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage * img = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return img;
}
-(UIImage*)imageRepresentationWithTintColor:(UIColor*)color
{
UIImage* viewImage = [self imageRepresentation];
viewImage = [self tintedImage:viewImage UsingColor:color];
return viewImage;
}
-(UIImage *)tintedImage:(UIImage*)image UsingColor:(UIColor *)tintColor {
UIGraphicsBeginImageContextWithOptions(image.size, NO, [[UIScreen mainScreen] scale]);
CGRect drawRect = CGRectMake(0, 0, image.size.width, image.size.height);
[image drawInRect:drawRect];
[tintColor set];
UIRectFillUsingBlendMode(drawRect, kCGBlendModeSourceAtop);
UIImage *tintedImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return tintedImage;
}
#pragma mark - Dynamic setters/getters for Associative References
-(void)setTintImageView:(UIImageView *)tintImageView
{
objc_setAssociatedObject(self, tintImageViewKey, tintImageView, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}
-(UIImageView*)tintImageView
{
return objc_getAssociatedObject(self , tintImageViewKey);
}
-(UIColor*)tintColor
{
return objc_getAssociatedObject(self , tintColorKey);
}
-(void)setTintColor:(UIColor *)tintColor
{
objc_setAssociatedObject(self, tintColorKey, tintColor, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}
-(BOOL)tintOnTouchActive
{
return [objc_getAssociatedObject(self, tintOnTouchActiveKey) boolValue];
}
-(void)setTintOnTouchActive:(BOOL)tintOnTouchActive
{
objc_setAssociatedObject(self, tintOnTouchActiveKey, [NSNumber numberWithBool:tintOnTouchActive], OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}
#end
To tint a UIView, add another subview on top of it with the same frame and an alpha transparency set.
For example let's say your container type view is called containerView, you'd do as follows:
CGRect overlayFrame = containerView.frame;
UIView *overlayView = [UIView alloc] initWithFrame:overlayFrame];
overlayView.alpha = 0.5f;
overlayView.color = [UIColor blackColor];
[containerView addSubview:overlayView];
This gives you a semi transparent (0.5f) black tint that will give a tint like appearance to all the subviews underneath.

Custom UILabel does not show text

I've made an custom UILabel class in which i draw a red line at the bottom of the frame.
The red line is showing but i cant get the text to show.
#import <UIKit/UIKit.h>
#interface LetterLabel : UILabel {
}
#end
#import "LetterLabel.h"
#implementation LetterLabel
- (id)initWithFrame:(CGRect)frame {
if (self = [super initWithFrame:frame]) {
// Initialization code
}
return self;
}
- (void)drawRect:(CGRect)rect {
CGContextSetLineWidth(UIGraphicsGetCurrentContext(), 2.0);
CGContextSetRGBStrokeColor(UIGraphicsGetCurrentContext(), 1.0, 0.0, 0.0, 1.0);
CGContextBeginPath(UIGraphicsGetCurrentContext());
CGContextMoveToPoint(UIGraphicsGetCurrentContext(), 0.0, self.frame.size.height);
CGContextAddLineToPoint(UIGraphicsGetCurrentContext(), self.frame.size.width, self.frame.size.height);
CGContextStrokePath(UIGraphicsGetCurrentContext());
}
- (void)dealloc {
[super dealloc];
}
#end
#import <UIKit/UIKit.h>
#import "Word.h"
#interface WordView : UIView {
Word *gameWord;
}
#property (nonatomic, retain) Word *gameWord;
#end
#implementation WordView
#synthesize gameWord;
- (id)initWithFrame:(CGRect)frame {
if (self = [super initWithFrame:frame]) {
self.backgroundColor = [UIColor whiteColor];
LetterLabel *label = [[LetterLabel alloc] initWithFrame:CGRectMake(0, 0, 20, 30)];
label.backgroundColor = [UIColor cyanColor];
label.textAlignment = UITextAlignmentCenter;
label.textColor = [UIColor blackColor];
[label setText:#"t"];
[self addSubview:label];
}
return self;
}
- (void)drawRect:(CGRect)rect {
// Drawing code
}
- (void)dealloc {
[gameWord release];
[super dealloc];
}
#end
Surely your LetterLabel needs to call the UILabel's drawRect: at some point?
-(void)drawRect:(CGRect)rect {
// Your stuff goes here
[super drawRect: rect];
}