How to disable Customized UIButton - ios

I subclassed UIButton and did some customized drawing in drawRect method such as drawing NSAttributedString and UIImage.
However, after I did this, the customized UIButton doesn't gray out when enabled is set to NO. I think my customized drawing happens on top of its state. How do I deal with this?
Sharing my drawing code here:
- (void)drawRect:(CGRect)rect
{
// Drawing code
if (self.faceUp) {
[self drawCardText:self.card.contents inRect:self.bounds];
} else {
[self drawCardImage:[UIImage imageNamed:CardBackImageName] inRect:self.bounds];
}
}
- (void)drawCardText:(NSString *)text inRect:(CGRect)rect
{
// set background color to white so text can be shown
[[UIColor whiteColor] setFill];
[[UIBezierPath bezierPathWithRect:rect] fill];
UIFont *preferedFont = [UIFont preferredFontForTextStyle:UIFontTextStyleBody];
UIFont *actualFont = [UIFont fontWithName:preferedFont.fontName
size:hypotf(rect.size.width, rect.size.height) / 6.0];
NSDictionary *attributes = #{NSFontAttributeName: actualFont};
NSAttributedString *attributedText = [[NSAttributedString alloc] initWithString:text attributes:attributes];
[self setAttributedTitle:attributedText forState:UIControlStateNormal];
}
- (void)drawCardImage:(UIImage *)image inRect:(CGRect)rect
{
UIGraphicsBeginImageContextWithOptions(rect.size, NO, 1.0f);
[image drawInRect:rect];
UIImage *actualImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
[[UIColor colorWithPatternImage:actualImage] setFill];
[[UIBezierPath bezierPathWithRect:rect] fill];
}

You might have to add an overlay view with grey color and some alpha yourself. Remove the overlay when button is enabled again.

You can add this method to CustomButton class.
-(void)setEnabled:(BOOL)enabled{
//disableLayer.hidden = !enabled;
if (enabled) {
//self.enabled = YES;
self.alpha = 1.0;
}else{
//self.enabled = NO;
self.alpha = 0.7;
}
[super setEnabled:enabled];
}
For enable or disable call-
[customButtob setEnabled:buttonStatus];
If you want to change color, add a background layer, and toggle its hidden property in setEnabled method.
disableLayer = [CALayer layer];
disableLayer.backgroundColor = [UIColor colorWithRed:20/255.0f green:20/255.0f blue:20/255.0f alpha:1.0f].CGColor;
disableLayer.frame = self.layer.bounds;
disableLayer.hidden = YES;
[self.layer insertSublayer:disableLayer below:otherLayer];

I think you share some more details of your implementation. Moreover I am sure you have called
Super methods in the customized methods at first call.

You have to override the setEnabled: method of your CustomButton
-(void)setEnabled:(BOOL)enabled
{
if (!enabled) {
[self setAlpha:0.2f];
}
}
OR
If you have to change the color of UIButton in its disabled state. Yo can change drawRect: method as
Make a assign property of BOOL type variable (say isEnabled)
-(void)drawRect:(CGRect)rect
{
[[UIColor redColor] setFill];
UIRectFill(rect);
if (!self.isEnabled) {
[[UIColor blackColor] setFill];
UIRectFill(rect);
}
}
-(void)setEnabled:(BOOL)enabled
{
self.isEnabled = enabled;
[self setNeedsDisplay];
}

You can use
btn.userInteractionEnabled = NO;
Or you can use
btn.enabled = NO;
Hope this helps... :)
Edit:
I think you can use this
[btn setTitleColor:[UIColor grayColor] forState:UIControlStateDisabled];
Add this:
-(void)setEnabled:(BOOL)enabled{
if (!enabled) {
self.alpha = 0.3;
}else{
self.alpha = 1.0;
}
[super setEnabled:enabled];
}

add a method which can help u for enable and disable of your button just add this method in custom button class
//in customButton.h file
#import <UIKit/UIKit.h>
#interface CustomButton : UIButton
#property (nonatomic,assign) BOOL faceUp;//your property
- (void)makeButtonDisable:(BOOL)disable;//method to handle disable and enable
#end
//in customButton.m file
#import "CustomButton.h"
#implementation CustomButton
{
CALayer *grayLayer;//layer to handle disable and enable
}
#synthesize faceUp;
// in initilization method
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if(self)
{
grayLayer = [CALayer layer];
grayLayer.frame = frame;
[self.layer addSublayer:grayLayer]; //add the grayLayer during initialisation
}
return self;
}
//your code that put above
- (void)drawRect:(CGRect)rect
{
// Drawing code
if (self.faceUp) {
[self drawCardText:self.card.contents inRect:self.bounds];
} else {
[self drawCardImage:[UIImage imageNamed:CardBackImageName] inRect:self.bounds];
}
}
- (void)drawCardText:(NSString *)text inRect:(CGRect)rect
{
// set background color to white so text can be shown
[[UIColor whiteColor] setFill];
[[UIBezierPath bezierPathWithRect:rect] fill];
UIFont *preferedFont = [UIFont preferredFontForTextStyle:UIFontTextStyleBody];
UIFont *actualFont = [UIFont fontWithName:preferedFont.fontName
size:hypotf(rect.size.width, rect.size.height) / 6.0];
NSDictionary *attributes = #{NSFontAttributeName: actualFont};
NSAttributedString *attributedText = [[NSAttributedString alloc] initWithString:text attributes:attributes];
[self setAttributedTitle:attributedText forState:UIControlStateNormal];
}
- (void)drawCardImage:(UIImage *)image inRect:(CGRect)rect
{
UIGraphicsBeginImageContextWithOptions(rect.size, NO, 1.0f);
[image drawInRect:rect];
UIImage *actualImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
[[UIColor colorWithPatternImage:actualImage] setFill];
[[UIBezierPath bezierPathWithRect:rect] fill];
}
//add this method
- (void)makeButtonDisable:(BOOL)disable
{
if(disable)
{
grayLayer.backgroundColor = [UIColor colorWithRed:201.0f/255.0f green:201.0f/255.0f blue:201.0f/255.0f alpha:5.0f].CGColor;
self.userInteractionEnabled = NO;
grayLayer.opacity = 0.5f;
self.alpha = 0.5f;
}
else
{
grayLayer.backgroundColor = [UIColor clearColor].CGColor;
self.userInteractionEnabled = YES;
grayLayer.opacity = 0.0f;
self.alpha = 1.0f;
}
}
in the controller where u are using this button
//some where in the controller u want to disable the button just do like this
//to disable
[button makeButtonDisable:YES]; //button is an instance of customCell
//to enable
[button makeButtonDisable:NO]; //button is an instance of customCell

Related

Create show Case view In Tabbar xcode

I am trying to create my own custom show case view , In which i have left button , right button and a tab bar view.
I want to highlight the buttons with round circle around it.
I am able to create a single Circle around my button by subclass UIView
But what i do so user swipe from left to right it will change the Circle to right button and the left button hide.
Here is my subclass of UIView
//--------.h class
#import <UIKit/UIKit.h>
#interface IntroView : UIView
- (void)drawRect:(CGRect)rect withBtnRect:(CGRect)btnrect ;
#end
//------. m class
#import "IntroView.h"
#implementation IntroView
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
// Initialization code
}
self.backgroundColor= [[UIColor blackColor] colorWithAlphaComponent:0.3];
return self;
}
- (void)drawRect:(CGRect)rect {
CGRect maskRect= CGRectMake(5, 5, 100, 100);//set and pass this maskRect
CGRect rBounds = self.bounds;
CGContextRef context = UIGraphicsGetCurrentContext();
// Fill background with 40% gray
CGContextSetFillColorWithColor(context, [[[UIColor grayColor] colorWithAlphaComponent:0.4] CGColor]);
CGContextFillRect(context, rBounds);
// Draw the window 'frame'
CGContextSetStrokeColorWithColor(context, [[UIColor whiteColor] CGColor]);
CGContextSetLineWidth(context,2);
CGContextStrokeEllipseInRect(context, maskRect);
// make the window transparent
CGContextSetBlendMode(context, kCGBlendModeClear);
CGContextFillEllipseInRect (context, maskRect);
}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
// Drawing code
}
*/
#end
And inside my tabbar first view i call using
#import "IntroView.h"
then
- (void)viewDidLoad
{
IntroView *vw_intro=[[IntroView alloc]initWithFrame:CGRectMake(0, 0, 320, 480)];
vw_intro.tag=1000;
[self.view addSubview:vw_intro];
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
Here is my output
And expected Output on Slide right to left , and so or in reverse (like we do in showcase android library )
In IntroView.h
#import <UIKit/UIKit.h>
#interface IntroView : UIView
- (id)initWithFrame:(CGRect)frame introElements:(NSArray *)iElements;
- (void)showIntro;
#end
In IntroView.m
#import "IntroView.h"
#implementation IntroView
{
CAShapeLayer *mask;
NSArray *introElements;
NSUInteger currentIndex;
}
- (id)initWithFrame:(CGRect)frame introElements:(NSArray *)iElements
{
self = [super initWithFrame:frame];
if (self) {
// Initialization code
introElements = iElements;
mask = [CAShapeLayer layer];
[mask setFillColor:[[UIColor grayColor] colorWithAlphaComponent:0.8f].CGColor];
[mask setFillRule:kCAFillRuleEvenOdd];
[self.layer addSublayer:mask];
UISwipeGestureRecognizer *swipeGestureRecognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:#selector(userDidSwipe)];
swipeGestureRecognizer.direction = UISwipeGestureRecognizerDirectionRight;
[self addGestureRecognizer:swipeGestureRecognizer];
}
return self;
}
- (void)userDidSwipe
{
[self showIntroAtIndex:currentIndex+1];
}
- (void)showIntro
{
//Initial index by default
[self showIntroAtIndex:0];
}
- (void)showIntroAtIndex:(NSUInteger)index
{
currentIndex = index;
CGRect rect = [[introElements objectAtIndex:index] CGRectValue];
UIBezierPath *maskPath = [UIBezierPath bezierPathWithRect:self.bounds];
UIBezierPath *elementPath = [UIBezierPath bezierPathWithRoundedRect:rect cornerRadius:3.0f];
[maskPath appendPath:elementPath];
// Animate
CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:#"path"];
animation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseOut];
animation.duration = 0.3f;
animation.fromValue = (__bridge id)(mask.path);
animation.toValue = (__bridge id)(maskPath.CGPath);
[mask addAnimation:animation forKey:#"path"];
mask.path = maskPath.CGPath;
}
In ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
NSArray *viewElements = [NSArray arrayWithObjects:[NSValue valueWithCGRect:button1.frame],[NSValue valueWithCGRect:button2.frame], nil];
IntroView *introView = [[IntroView alloc]initWithFrame:self.view.bounds introElements:viewElements];
[self.view addSubview:introView];
[introView showIntro];
}
Note: i made a highlight to the UIControl frame, if you need a circle highlight, update the changes in the CGRect.

Customize UITextField Clear Button Color to White Color [duplicate]

This question already has answers here:
How to change the tint color of the clear button on a UITextField
(25 answers)
Closed 6 years ago.
I want to change Color of the UITextField's Clear Button, As I Know I can achieve the same with accessing the RightView Property of the TextField as Well as also I can use an Image to do the same.
But No, I want to change the colour of that button only.
Can Someone help me with the same?
The Below represents the code which I wrote to access the clear button of the UITextField.
for (UIView *aSubview in self.view.subviews) {
for (UIView *bSubview in aSubview.subviews) {
if ([bSubview isKindOfClass:[UITextField class]]){
for(UIView *v in bSubview.subviews)
{
if([v isKindOfClass:[UIButton class]])
{
}
}
}
}
}
From all your answers, I come to this conclusion,
UIButton *btnClear = [self.txtEmail valueForKey:#"_clearButton"];
UIImage *imageNormal = [btnClear imageForState:UIControlStateNormal];
UIGraphicsBeginImageContextWithOptions(imageNormal.size, NO, 0.0);
CGContextRef context = UIGraphicsGetCurrentContext();
CGRect rect = (CGRect){ CGPointZero, imageNormal.size };
CGContextSetBlendMode(context, kCGBlendModeNormal);
[imageNormal drawInRect:rect];
CGContextSetBlendMode(context, kCGBlendModeSourceIn);
[[UIColor whiteColor] setFill];
CGContextFillRect(context, rect);
UIImage *imageTinted = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
[btnClear setImage:imageTinted forState:UIControlStateNormal];
I am new in iOS app development.But i will try.please refer following code.
.h file:
#import <UIKit/UIKit.h>
#interface TextFieldTint : UITextField
-(void) setColorButtonClearHighlighted:(UIColor *)colorButtonClearHighlighted;
-(void) setColorButtonClearNormal:(UIColor *)colorButtonClearNormal;
#end
.m file:
#import "TextFieldTint.h"
#interface TextFieldTint()
#property (nonatomic,strong) UIColor *colorButtonClearHighlighted;
#property (nonatomic,strong) UIColor *colorButtonClearNormal;
#property (nonatomic,strong) UIImage *imageButtonClearHighlighted;
#property (nonatomic,strong) UIImage *imageButtonClearNormal;
#end
#implementation TextFieldTint
-(void) layoutSubviews
{
[super layoutSubviews];
[self tintButtonClear];
}
-(void) setColorButtonClearHighlighted:(UIColor *)colorButtonClearHighlighted
{
_colorButtonClearHighlighted = colorButtonClearHighlighted;
}
-(void) setColorButtonClearNormal:(UIColor *)colorButtonClearNormal
{
_colorButtonClearNormal = colorButtonClearNormal;
}
-(UIButton *) buttonClear
{
for(UIView *v in self.subviews)
{
if([v isKindOfClass:[UIButton class]])
{
UIButton *buttonClear = (UIButton *) v;
return buttonClear;
}
}
return nil;
}
-(void) tintButtonClear
{
UIButton *buttonClear = [self buttonClear];
if(self.colorButtonClearNormal && self.colorButtonClearHighlighted && buttonClear)
{
if(!self.imageButtonClearHighlighted)
{
UIImage *imageHighlighted = [buttonClear imageForState:UIControlStateHighlighted];
self.imageButtonClearHighlighted = [[self class] imageWithImage:imageHighlighted
tintColor:self.colorButtonClearHighlighted];
}
if(!self.imageButtonClearNormal)
{
UIImage *imageNormal = [buttonClear imageForState:UIControlStateNormal];
self.imageButtonClearNormal = [[self class] imageWithImage:imageNormal
tintColor:self.colorButtonClearNormal];
}
if(self.imageButtonClearHighlighted && self.imageButtonClearNormal)
{
[buttonClear setImage:self.imageButtonClearHighlighted forState:UIControlStateHighlighted];
[buttonClear setImage:self.imageButtonClearNormal forState:UIControlStateNormal];
}
}
}
+ (UIImage *) imageWithImage:(UIImage *)image tintColor:(UIColor *)tintColor
{
UIGraphicsBeginImageContextWithOptions(image.size, NO, 0.0);
CGContextRef context = UIGraphicsGetCurrentContext();
CGRect rect = (CGRect){ CGPointZero, image.size };
CGContextSetBlendMode(context, kCGBlendModeNormal);
[image drawInRect:rect];
CGContextSetBlendMode(context, kCGBlendModeSourceIn);
[tintColor setFill];
CGContextFillRect(context, rect);
UIImage *imageTinted = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return imageTinted;
}
#end

Custom draw rect is overlayed by background color

I created a custom UIView to draw a "hole" ontop of a background
#implementation MaskWithHole
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
self.hole = CGRectZero;
self.holeColor = [UIColor clearColor];
}
return self;
}
-(UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event
{
return nil;
}
-(void)setHole:(CGRect)hole
{
_hole = hole;
[self setNeedsDisplay];
}
-(void)setHoleColor:(UIColor *)holeColor
{
_holeColor = holeColor;
[self setNeedsDisplay];
}
- (void)drawRect:(CGRect)rect
{
[self.backgroundColor setFill];
UIRectFill(rect);
// clear the background in the given rectangles
CGRect holeRect = self.hole;
CGRect holeRectIntersection = CGRectIntersection( holeRect, rect );
[self.holeColor setFill];
UIRectFill(holeRectIntersection);
}
#end
But the custom drawing is always the background color - disregarding the drawRect code
CGRect holeRect = self.hole;
CGRect holeRectIntersection = CGRectIntersection( holeRect, rect );
[self.holeColor setFill];
UIRectFill(holeRectIntersection);
when I change the color of the hole from clear to green i can see the green- so apperantly the method is drawing teh background on everything
The easy way to have a transparent hole in the middle of a solid background is using a png image of that size with transparency in the middle, and the solid color outside.
This seems to work
#interface MaskWithHole : UIView
// default cgrectzero
#property (nonatomic,assign) CGRect hole;
// default [uicolor clearcolor]
#property (nonatomic,strong) UIColor *holeColor;
#end
#interface MaskWithHole ()
#property (nonatomic,strong) UIColor *backGroundColorForMask;
#end
#implementation MaskWithHole
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
self.hole = CGRectZero;
self.holeColor = [UIColor clearColor];
self.backgroundColor = [UIColor clearColor];
self.opaque = NO;
}
return self;
}
-(UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event
{
return nil;
}
-(void)setHole:(CGRect)hole
{
_hole = hole;
[self setNeedsDisplay];
}
-(void)setHoleColor:(UIColor *)holeColor
{
_holeColor = holeColor;
[self setNeedsDisplay];
}
-(void)setBackgroundColor:(UIColor *)backgroundColor
{
[super setBackgroundColor:[UIColor clearColor]];
self.backGroundColorForMask = backgroundColor;
}
- (void)drawRect:(CGRect)rect
{
[self.backGroundColorForMask setFill];
UIRectFill(rect);
CGRect holeRectIntersection = CGRectIntersection( self.hole, rect );
[[UIColor clearColor] setFill];
UIRectFill(holeRectIntersection);
}
#end

Draw text outside of CGRect.

I am using Apple's Sample Code TheElements for this question. How would I go about drawing text outside of the elementSymbolRectangle. For example I would like to display the Elements Name, but I need it to be outside of the elementSymbolRectangle. I am new to programming and would appreciate any help.
Thanks
- (id)initWithFrame:(CGRect)frame {
if (self = [super initWithFrame:frame]) {
[self setAutoresizesSubviews:YES];
[self setupUserInterface];
// set the background color of the view to clearn
self.backgroundColor=[UIColor clearColor];
}
return self;
}
- (void)jumpToWikipedia:(id)sender {
// create the string that points to the correct Wikipedia page for the element name
NSString *wikiPageString = [NSString stringWithFormat:#"http://en.wikipedia.org/wiki/%#", self.element.name];
if (![[UIApplication sharedApplication] openURL:[NSURL URLWithString:wikiPageString]])
{
// there was an error trying to open the URL. for the moment we'll simply ignore it.
}
}
- (void)drawRect:(CGRect)rect {
// get the background image for the state of the element
// position it appropriately and draw the image
//
UIImage *backgroundImage = [self.element stateImageForAtomicElementView];
CGRect elementSymbolRectangle = CGRectMake(0, 0, [backgroundImage size].width, [backgroundImage size].height);
[backgroundImage drawInRect:elementSymbolRectangle];
// all the text is drawn in white
[[UIColor whiteColor] set];
// draw the element number
UIFont *font = [UIFont boldSystemFontOfSize:32];
CGPoint point = CGPointMake(10,5);
[[NSString stringWithFormat:#"%#", self.element.atomicNumber] drawAtPoint:point withFont:font];
// draw the element symbol
CGSize stringSize = [self.element.symbol sizeWithFont:font];
point = CGPointMake((self.bounds.size.width-stringSize.width-10),5);
[self.element.symbol drawAtPoint:point withFont:font];
This solved my problem.
UILabel *scoreLabel = [ [UILabel alloc ] initWithFrame:CGRectMake((self.bounds.size.width / 2), -50.0, 100.0, 100.0) ];
scoreLabel.textAlignment = UITextAlignmentCenter;
scoreLabel.textColor = [UIColor whiteColor];
scoreLabel.backgroundColor = [UIColor blackColor];
scoreLabel.font = [UIFont fontWithName:#"Arial Rounded MT Bold" size:(36.0)];
[self addSubview:scoreLabel];
scoreLabel.text = [NSString stringWithFormat: #"Test"];

Draw a line between two points - How to pass UIView

I am trying to draw a simple line connecting two points. I think i got the right code for that in my drawLine method. No lines appear at all and I am pretty sure it has to do with the View I am calling with and the one I want to draw on. The following code is where I build my next View in code (data coming from a web service). The View gets created just fine and all buttons and objects appear.
I am trying to add lines to the View I'm creating (Next) dynamically and not sure how to get that done. From the Errors I get (invalid context 0x0) it seems I am doing something out of order. Trying to display on a view that is not there. how than can I get this done? Any advice would be appreciated.
I realize that passing on the View might not be the right answer. I am in the midst of trial and error here.
To clarify: I am calling drawLine here:
[nextView.view addSubview:next];
Thanks,
Shai
-(void) drawView
{
CGRect rec;
CGFloat width = [UIScreen mainScreen].bounds.size.width;
CGFloat height = [UIScreen mainScreen].bounds.size.height;
UIView *next = [[UIView alloc]initWithFrame:CGRectMake(0, 0, height, width)];
next.backgroundColor = [UIColor whiteColor];
next.userInteractionEnabled = YES;
next.multipleTouchEnabled = YES;
nextView = [UIViewController alloc];
[nextView setTitle: as_objectDesc];
for (object in viewObjects)
{ // ratio based on Ipad 2 screen size conversion from the Datawindow size - alter to support other device
CGFloat objWidth = object.Width/3.83;
CGFloat objHeight = object.Height/2.78;
CGFloat objXPoint = object.positionX/3.83;
CGFloat objYPoint = object.positionY/2.78;
if ([object.Shape isEqualToString:#"text"])
{
rec = CGRectMake(objXPoint, objYPoint, objWidth, objHeight);
UILabel *myLabel = [[UILabel alloc] initWithFrame:rec];
myLabel.backgroundColor = [UIColor clearColor];
myLabel.text = object.objectTag;
myLabel.lineBreakMode = NSLineBreakByWordWrapping;
if ([object.description isEqualToString:#"Header"])
{
myLabel.font = [UIFont systemFontOfSize:20];
}
else
{
myLabel.font = [UIFont systemFontOfSize:10];
}
[next addSubview:myLabel];
}
else if ([object.Shape isEqualToString:#"line"])
{
**[self drawLine:nextView.view];**
}
else
{
UIButton *myButton = [UIButton buttonWithType:UIButtonTypeCustom];
if ([object.Shape isEqualToString:#"ellipse"])
{
UIImage * buttonImage;
if ([object.Color isEqualToString:#"255"])
{
buttonImage = [UIImage imageNamed:#"redBackground.png"];
}
else if ([object.Color isEqualToString:#"65535"])
{
buttonImage = [UIImage imageNamed:#"yellowBackground.png"];
}
else if ([object.Color isEqualToString:#"GRAY"])
{
buttonImage = [UIImage imageNamed:#"graybground.PNG"];
}
else if ([object.Color isEqualToString:#"BLACK"])
{
buttonImage = [UIImage imageNamed:#"BlackBackground.png"];
}
[myButton setBackgroundImage:buttonImage forState: UIControlStateNormal];
objWidth = objHeight;
rec = CGRectMake(objXPoint, objYPoint, objWidth, objHeight);
myButton.frame = rec;
myButton.clipsToBounds = YES;
myButton.layer.cornerRadius = objWidth/2;
}
else
{
rec = CGRectMake(objXPoint, objYPoint, objWidth, objHeight);
if ([object.Color isEqualToString:#"255"])
myButton.backgroundColor = [UIColor redColor];
else if ([object.Color isEqualToString:#"65535"])
myButton.backgroundColor = [UIColor yellowColor];
else if ([object.Color isEqualToString:#"GRAY"])
myButton.backgroundColor = [UIColor grayColor];
else if ([object.Color isEqualToString:#"BLACK"])
myButton.backgroundColor = [UIColor blackColor];
}
[[myButton layer] setBorderWidth:1.0f];
[[myButton layer] setBorderColor:[UIColor blackColor].CGColor];
myButton.enabled = YES;
myButton.accessibilityHint = object.objectTag;
myButton.titleLabel.lineBreakMode = NSLineBreakByWordWrapping;
myButton.titleLabel.textAlignment = NSTextAlignmentCenter;
myButton.titleLabel.font = [UIFont systemFontOfSize:13];
[myButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
[myButton addTarget:self action:#selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
myButton.frame = rec;
[myButton setTitle:object.description forState:UIControlStateNormal];
// Check if an Empty Rectangle
NSRange test =[myButton.accessibilityHint rangeOfString:#"r_"];
if (test.location == 0)
{
[next sendSubviewToBack:myButton];
[myButton setEnabled:NO];
}
[next addSubview:myButton];
}
UIPinchGestureRecognizer *twoFingerPinch = [[UIPinchGestureRecognizer alloc]
initWithTarget:self
action:#selector(twoFingerPinch:)]
;
[[nextView view] addGestureRecognizer:twoFingerPinch];
[next addSubview:activityIndicator];
[next bringSubviewToFront:activityIndicator];
}
[nextView.view addSubview:next];
[self dismissViewControllerAnimated:NO completion:nil];
[self.navigationController pushViewController:nextView animated:NO];
}
- (void)drawLine:(UIView *) view
{
CGFloat x1 = object.positionX;
CGFloat y1 = object.positionY;
CGFloat x2 = object.positionX2;
CGFloat y2 = object.positionY2;
CGContextRef context = UIGraphicsGetCurrentContext();
if ([object.Color isEqualToString:#"255"])
CGContextSetStrokeColorWithColor(context, [UIColor redColor].CGColor);
else if ([object.Color isEqualToString:#"65535"])
CGContextSetStrokeColorWithColor(context, [UIColor yellowColor].CGColor);
else if ([object.Color isEqualToString:#"GRAY"])
CGContextSetStrokeColorWithColor(context, [UIColor grayColor].CGColor);
else if ([object.Color isEqualToString:#"BLACK"])
CGContextSetStrokeColorWithColor(context, [UIColor blackColor].CGColor);
CGContextSetLineWidth(context, 3.0);
CGContextBeginPath(context);
CGContextMoveToPoint(context, x1, y1);
CGContextAddLineToPoint(context, x2, y2);
CGContextStrokePath(context);
}
I end up figuring this up days a go and thought I would share here. I might have added to much code in my question which did not help to simplify it to others (no comments).
What this came down to is when trying to Draw a line you need to override the DrawRect Method of the UIView subclass and not the UIViewController as I did. There is no Error thrown if you call DrawRect from your UIViewController but it doesn't do anything. I missed that small detail.
What added to the complexity of finding this out ans solving this was that I was creating all of it Programmatically and initially didn't subclass UIView at all. I ended up adding all the Points in an NSMutablearray and redrawing the lines in the following class after the newly View got created:
#import "draw.h"
#implementation draw
#synthesize lines;
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self)
{
// Initialization code
}
return self;
}
-(void)drawRect:(CGRect)rect
{
CGContextRef context = UIGraphicsGetCurrentContext();
for (line *myLine in lines)
{
if ([myLine.color isEqualToString:#"255"])
CGContextSetStrokeColorWithColor(context, [UIColor redColor].CGColor);
else if ([myLine.color isEqualToString:#"65535"])
CGContextSetStrokeColorWithColor(context, [UIColor yellowColor].CGColor);
else if ([myLine.color isEqualToString:#"GRAY"])
CGContextSetStrokeColorWithColor(context, [UIColor grayColor].CGColor);
else if ([myLine.color isEqualToString:#"BLACK"])
CGContextSetStrokeColorWithColor(context, [UIColor blackColor].CGColor);
int setNew = myLine.Width/4;
CGContextSetLineWidth(context, (CGFloat)setNew);
CGContextBeginPath(context);
CGContextMoveToPoint(context, myLine.x1, myLine.y1);
CGContextAddLineToPoint(context, myLine.x2, myLine.y2);
CGContextStrokePath(context);
}
}
#end

Resources