Assigning a background color to a UITextField - iphone

I have a main controller header with a color declaration
MainViewController.h
#interface MainViewController : UIViewController <FlipsideViewControllerDelegate> {
UIColor *myColor;
}
#property (nonatomic, retain) UIColor *myColor;
#end
and an implementation with an initialization
MainViewController.m
#implementation MainViewController
#synthesize myColor;
- (void)viewDidLoad
{
[super viewDidLoad];
myColor = [UIColor colorWithRed:1.0 green:0.98 blue:0.8 alpha:1]; //initialization
}
Now I want to use this color in a method in MainViewController like this
- (void) myMethod {
myTextField.backgroundColor = myColor;
}
but but all my attempts are in vain. What is the correct syntax?

You must send a retain to the color object you initialize :
myColor = [[UIColor colorWithRed:1.0 green:0.98 blue:0.8 alpha:1] retain];
or set the property like so :
self.myColor = [UIColor colorWithRed:1.0
green:0.98
blue:0.8 alpha:1];
which, according to the property you declared, should retain it. The way you set the backgroundColor is ok.

if u not synthesize the myTextfield then
[myTextField setBackgroundColor:myColor];
if synthesize the myTextfield then
self.myTextField.backgroundColor = myColor;

Related

Setting up a subclass for UIButton to handle look-and-feel

I have a AuthButtons.h header:
#import <UIKit/UIKit.h>
#interface AuthButtons : UIButton
#property (nonatomic, weak) UIColor *backgroundColor;
#end
The implementation AuthButtons.m class is as follows:
#import "AuthButtons.h"
#implementation AuthButtons
- (void)setBackgroundColor:(UIColor *)backgroundColor
{
backgroundColor = [UIColor blueColor];
}
- (UIColor *)backgroundColor
{
return [UIColor whiteColor];
}
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
// Initialization code
}
return self;
}
Then in my viewcontroller.m class I have:
#interface ViewController ()
#property (nonatomic, weak) AuthButtons *registerButton;
#end
...
#implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
_registerButton = [AuthButtons buttonWithType:UIButtonTypeSystem];
[_registerButton setTitle:#"Register" forState:UIControlStateNormal];
[_registerButton setBackgroundColor:[UIColor whiteColor]];
_registerButton.frame = CGRectMake(0.0, 0.0, self.screenWidth / 2.0, 100.0);
[_registerButton addTarget:self action:#selector(buttonPressed:) forControlEvents:UIControlEventTouchUpInside];
}
It keeps crashing when I try to create the button. What am I missing?
Your button is weak and not added as a subview so its deallocated right away
Use strong instead of weak(i always use strong type when using buttons in ARC enabled class,not sure whether its a bad habbit :-))
#property (nonatomic, strong) AuthButtons *registerButton;
and add it as your subview([self.view addSubview:_registerButton]).Hope you have implemeted the function called -(void)buttonPressed:(id )sender in your class.

initWithCoder doesn't set UILabel property (UIFont and text color)

I have the following code for my UICollectionViewCell
#property (strong, nonatomic) IBOutlet UIImageView *storyImage;
#property (strong, nonatomic) IBOutlet UILabel *titleLabel;
#property (strong, nonatomic) IBOutlet UILabel *descriptionLabel;
#end
#implementation Story
#synthesize storyImage;
#synthesize titleLabel;
#synthesize descriptionLabel;
- (id)initWithCoder:(NSCoder *)aDecoder
{
self = [super initWithCoder:aDecoder];
if (self)
{
[self.contentView addSubview:self.titleLabel];
[self.contentView addSubview:self.descriptionLabel];
[self.contentView addSubview:self.storyImage];
[self.titleLabel setFont:[UIFont fontWithName:kProximaNovaBold size:15]];
[self.descriptionLabel setFont:[UIFont fontWithName:kProximaNova size:13]];
[self.descriptionLabel setTextColor:[UIColor colorWithWhite:140/255.f alpha:1.0]];
self.descriptionLabel.frame = CGRectIntegral(self.descriptionLabel.frame);
self.descriptionLabel.center = roundedCenterPoint(self.descriptionLabel.center);
}
return self;
}
but for some reason it's not setting up the property. I'd have to move it out of the init method and put it inside a separate method and call this after calling:
dequeueReusableCellWithReuseIdentifier:kCellID forIndexPath:indexPath
and then it would work. Any idea why?
You want to be doing this stuff in awakeFromNib: when everything from the nib has actually been loaded and connected
From the docs for NSObject UIKit Additions
Discussion
The nib-loading infrastructure sends an awakeFromNib message to each object recreated from a nib archive, but only after all the objects in the archive have been loaded and initialized.

Subclassed UIButton doesn't appear to initialize

This started as a simple problem, but I can't solve it. I have subclassed a UIButton (I know, controversial) but I can't seem to get it to work properly. My UIButton subclass looks like this:
.h
#interface SocialButton : UIButton
#property (nonatomic, strong) CustomClass *cust;
- (id)initWithObject:(CustomClass *)custom andFrame:(CGRect)frame;
#end
.m
#implementation SocialButton
#synthesize custom=_custom;
- (id)initWithObject:(CustomClass *)custom andFrame:(CGRect)frame;
{
self = [self initWithFrame:frame];
if (self)
{
[self addTarget:self action:#selector(buttonTouchUpInside:) forControlEvents:UIControlEventTouchUpInside];
self.backgroundColor = [UIColor blueColor];
self.cust = custom;
}
return self;
}
#end
Later, when I try to create a button from this class, like so:
SocialButton *social = [[SocialButton alloc] initWithObject:object andFrame:frame];
it doesn't appear to work. Any suggestions?
Seems like you forgot to call super. Change:
self = [self initWithFrame:frame];
to:
self = [super initWithFrame:frame];

highlighted shadow color

How to set the shadowColor of a highlighted Text?
titleLabel.textColor = [UIColor colorWithRed:20.0f/255.0f green:50.0f/255.0f blue:90.0f/255.0f alpha:1.0f];
titleLabel.highlightedTextColor = [UIColor whiteColor];
titleLabel.shadowColor = [UIColor whiteColor];
titleLabel.shadowOffset = CGSizeMake(0, 1.0);
I'm looking for something like: titleLabel.highlightedTextColor.shadowColor = [UIColor blackColor];
I was annoyed by this oversight from Apple, too, and created a subclass of UILabel that adds the missing highlightedShadowColor and highlightedShadowOffset properties.
By default it just disables the shadow in highlighted mode and sets the offset to the reverse of the standard offset.
#interface FTLabel : UILabel
#property(nonatomic, strong) UIColor *highlightedShadowColor;
#property(nonatomic) CGSize highlightedShadowOffset;
#end
#interface FTLabel ()
#property(nonatomic, strong) UIColor *savedShadowColor;
#property(nonatomic) CGSize savedShadowOffset;
#end
#implementation FTLabel
- (id)initWithCoder:(NSCoder *)aDecoder {
if ((self = [super initWithCoder:aDecoder])) {
[self configure];
}
return self;
}
- (id)initWithFrame:(CGRect)frame {
if ((self = [super initWithFrame:frame])) {
[self configure];
}
return self;
}
- (void)configure {
self.highlightedShadowColor = nil;
self.highlightedShadowOffset = CGSizeMake(self.shadowOffset.width * (-1), self.shadowOffset.height * (-1));
self.savedShadowColor = self.shadowColor;
self.savedShadowOffset = self.shadowOffset;
}
- (void)setHighlighted:(BOOL)highlighted {
[super setHighlighted:highlighted];
self.shadowColor = self.highlighted ? self.highlightedShadowColor : self.savedShadowColor;
self.shadowOffset = self.highlighted ? self.highlightedShadowOffset : self.savedShadowOffset;
}
#end
This should work:
if(!yourlabel.highlighed)
{
[yourLabel setShadowColor:[UIColor greenColor]];
}
else
{
[yourLabel setShadowColor:[UIColor blueColor]];
}
EDIT:
A UIlabel has a property called highlighted and is a BOOL. You can check if it's true or not and change the shadow color at the moment you want it done.
I had the same issue.
What I did in the end is to create a second UILabel underneath the primary UILabel to act as a shadow.
Don't set shadows on your primary and shadow labels. For the shadow label, set the 'Normal Color' to what you wanted your shadow color to be and set the highlighted color to 'Clear Color'.
Obviously you have to update the shadow label each time you update the primary label. Not a big price to pay in many cases.
Hope that helps!

1 out of 4 colors return errors

I have 4 Colors defined.
#interface Global : NSObject {
UIColor *_EnemyColor;
UIColor *_EnemyColor2;
UIColor *_TeamColor;
UIColor *_TeamColor2;
}
#property (nonatomic, retain) UIColor *EnemyColor;
#property (nonatomic, retain) UIColor *EnemyColor2;
#property (nonatomic, retain) UIColor *TeamColor;
#property (nonatomic, retain) UIColor *TeamColor2;
#end
and
#synthesize EnemyColor = _EnemyColor;
#synthesize EnemyColor2 = _EnemyColor2;
#synthesize TeamColor = _TeamColor;
#synthesize TeamColor2 = _TeamColor2;
Then in the init method, I try setting some colors to the variables.
- (id)init {
if (self = [super init]) {
//*******************************************************************
_TeamColor = [UIColor colorWithRed:0.70196078 green:0.70196078 blue:0.70196078 alpha:1.0];
//Everything works, if this line is commented out
_TeamColor2 = [UIColor colorWithRed:0.82352941 green:0.81960784 blue:0.83921569 alpha:1.0];
switch (arc4random() % 4) {
case 0:
_EnemyColor = [UIColor colorWithRed:0.50196078 green:0.47843137 blue:0.41568627 alpha:1];
_EnemyColor2 = [UIColor colorWithRed:0.63529412 green:0.57647059 blue:0.44705882 alpha:1];
break;
case 1:
_EnemyColor = [UIColor colorWithRed:0.72156863 green:0.59607843 blue:0.37254902 alpha:1];
_EnemyColor2 = [UIColor colorWithRed:0.81568627 green:0.73333333 blue:0.51764706 alpha:1];
break;
case 2:
_EnemyColor = [UIColor colorWithRed:0.75686275 green:0.47843137 blue:0.23529412 alpha:1];
_EnemyColor2 = [UIColor colorWithRed:0.85098039 green:0.56470588 blue:0.35686275 alpha:1];
break;
case 3:
_EnemyColor = [UIColor colorWithRed:0.45882353 green:0.6 blue:0.70196078 alpha:1];
_EnemyColor2 = [UIColor colorWithRed:0.57254902 green:0.65882353 blue:0.74117647 alpha:1];
break;
}
The problem I am having is that the program only fails if _teamColor2 is set using colorWithRed: Green: Blue: Alpha:. The error I am getting is
-[UIDeviceRGBColor set]: message sent to deallocated instance 0x5f4af80
. I can use redColor instead, and it will work. Is there a reason that only one out of four isn't working properly?
None of those should work, because none of them are being retained ([UIColor colorWithRed:green:blue:alpha:] returns an autoreleased instance). Try assigning to self.TeamColor, self.TeamColor2, and so on instead.