I have created a custom UILabel class and set the default background color.
Here are .h and .m files of my custom class.
#import <UIKit/UIKit.h>
#interface imLabel : UILabel
#end
AND
#import "imLabel.h"
#implementation imLabel
- (void)drawRect:(CGRect)rect {
}
- (void) layoutSubviews {
self.backgroundColor = [UIColor redColor];
}
#end
It works fine, but here is what I need: I want this work only if the backgroundColor is not set in ViewController.
Here is my viewDidLoad
- (void)viewDidLoad {
[super viewDidLoad];
label = [[imLabel alloc] initWithFrame:CGRectMake(50, 50, 300, 300)];
label.backgroundColor = [UIColor blueColor];
[self.view addSubview:label];
}
You control whether set label's backgroundColor or not in ViewController, so the best way to meet your need that I think is check the label had set backgroundColor or not, if not then set it.
/* Somewhere in your ViewController */
if (!self.label.backgroundColor) {
self.label.backgroundColor = [UIColor redColor];
}
Delete all your other code in imLabel.m, then:-
(instancetype)initWithFrame:(CGRect)aRect { self = [super initWithFrame:aRect]; if (self) { self.backgroundColor = [UIColor redColor]; } return self;}
Thanks to #Meiliang Dong
Related
I have 4 screens that have one almost the same view:
And one screen have the same view but with slightly different UI:
So, my question: Can I use one xib and adapt states (active, inactive) and change ui for different screen? How I can do it?
Here is an example of this kind of class
In Your .m file of custom XIB class if you are using objective-c.
- (void)foo:(NSString*)labelText andButtonText:(NSString*)buttonTitle {
//Do your code here for some screen like change labels and button text
}
- (void)bar:(NSString*)labelText andButtonText:(NSString*)buttonTitle {
//Do your code here for some another screen and change labels and button text
}
In Your .h file of custom XIB class if you are using objective-c.
- (void)foo:(NSString*)labelText andButtonText:(NSString*)buttonTitle;
- (void)bar:(NSString*)labelText andButtonText:(NSString*)buttonTitle;
In your view controller where you want to display the custom UI
Create an instance of your xib or add through interfacebuilder
And On your instance of custom class call the method as required.
Below is a class I've used in one of my project to get a clear understanding.
#import <UIKit/UIKit.h>
#import "DYRateView.h"
#interface LevelAndRankDetails : UIView
#property (nonatomic, strong) IBOutlet UIView* contentView;
#property (nonatomic, strong) IBOutlet UIView* viewContainer;
#property (nonatomic, strong) IBOutlet UILabel* lblLevel;
#property (nonatomic, strong) IBOutlet UILabel* lblRanking;
#property (weak, nonatomic) IBOutlet DYRateView *viewRate;
- (void)setLevel:(NSNumber*)level andRanking:(NSNumber*)ranking;
- (void)setupUI;
#end
.m File
#import "LevelAndRankDetails.h"
#import "AppDelegate.h"
#import "Constants.h"
#implementation LevelAndRankDetails
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
// Drawing code
}
*/
- (id)initWithFrame:(CGRect)frame {
if (self = [super initWithFrame:frame]){
[self commonSetup];
}
return self;
}
-(id)initWithCoder:(NSCoder *)aDecoder
{
self = [super initWithCoder:aDecoder];
if (self)
{
[self commonSetup];
}
return self;
}
- (void)layoutSubviews {
[super layoutSubviews];
}
- (void)viewFromNibForClass {
[[NSBundle mainBundle] loadNibNamed:[[self class] description] owner:self options:nil];
[self addSubview:self.contentView];
self.contentView.frame = self.bounds;
}
- (void)commonSetup {
[self viewFromNibForClass];
//For View's Corner Radius
self.contentView.layer.cornerRadius = 12;
self.contentView.layer.masksToBounds = YES;
self.contentView.backgroundColor = kDefaultBackgroundGreyColor;
self.viewContainer.backgroundColor = kDefaultBackgroundGreyColor;//[UIColor clearColor];
self.backgroundColor = kDefaultBackgroundGreyColor;
//self.viewContainer.backgroundColor = UIColorFromRGB(0xBB9657);//[kLearnFromLightColor colorWithAlphaComponent:0.5];
self.viewRate.rate = 0;
self.viewRate.editable = NO;
self.viewRate.delegate = nil;
self.viewRate.alignment = RateViewAlignmentCenter;
self.viewRate.backgroundColor = [UIColor clearColor];
[self.viewRate setEmptyStarImage:[UIImage imageNamed:#"StarEmpty"]];
UIImage* imageFullStar = [[UIImage imageNamed:#"StarFull"] imageTintedWithColor:kSliderDarkYellowColor];
[self.viewRate setFullStarImage:imageFullStar];
self.lblLevel.textColor = [UIColor whiteColor];
self.lblRanking.textColor = [UIColor whiteColor];
//For Teacher label
}
- (void)setupUI {
self.contentView.layer.cornerRadius = 0;
self.contentView.layer.masksToBounds = YES;
self.contentView.backgroundColor = [UIColor clearColor];
self.viewContainer.backgroundColor = [UIColor clearColor];//[UIColor clearColor];
self.backgroundColor = [UIColor clearColor];
}
- (void)setRanking:(CGFloat)ranking {
self.viewRate.rate = ranking;
}
- (void)setLevel:(NSNumber*)level {
self.lblLevel.text = [NSString stringWithFormat:#"Level : %#",level];
}
- (void)setLevel:(NSNumber*)level andRanking:(NSNumber*)ranking {
if (level.integerValue > 0) {
[self setLevel:level];
}
if (ranking.doubleValue > 0) {
CGFloat rankingConverted = ranking.floatValue;
[self setRanking:rankingConverted];
}
}
#end
And this is how you use it in your view controller
LevelAndRankDetails* toolTipCustomView = [[LevelAndRankDetails alloc] initWithFrame:CGRectMake(0, 0, 250, 66)];
toolTipCustomView.backgroundColor = [UIColor blackColor];
[toolTipCustomView setLevel:#(10) andRanking:#(3)];
I've looked at many posts and none seem to help with the current issue I'm experiencing. I am trying to change the UIView background color and UILabel text color programmatically on a button click. They are all connected in the interface builder. What I did was open the ViewController.h file next to the Main.storyboard and clicked and dragged to connect the view (backgroundView) and label (letterR).
If anyone can see what I am doing wrong that would be great thanks!
ViewController.h
#import <UIKit/UIKit.h>
#interface ViewController : UIViewController{
IBOutlet UIView *backgroundView;
IBOutlet UILabel *letterR;
}
#end
ViewController.m
#import "ViewController.h"
#interface ViewController ()
#end
#implementation ViewController
static int flag = 0;
- (void)viewDidLoad {
[super viewDidLoad];
}
- (IBAction)ButtonClick:(id)sender {
if(flag == 0){
flag = 1;
letterR.textColor = [UIColor blackColor];
backgroundView.backgroundColor = [UIColor whiteColor];
}
else if(flag == 1){
flag = 0;
letterR.textColor = [UIColor whiteColor];
backgroundView.backgroundColor = [UIColor colorWithRed:0 green:1 blue:1 alpha:1];
}
}
#end
I've got a UILabel with a background color in a cell. When I select this cell, the cell changes the color (which it should) but it also changes the background of the label. I want the preserve the background color on the UILabel. When I use an image with just a random color in it it is preserved, but isn't there any better way?
Thanks in advance
Code:
_label = [UILabel new];
_label.translatesAutoresizingMaskIntoConstraints = NO;
_label.font = [UIFont systemFontOfSize:10.f];
_label.backgroundColor = HEXCOLOR(0xFFE5E5E5); //Macro just a UIColor
But I use this way to add a different selection color (could have something to do with it)
UIView *selectionColor = [[UIView alloc] init];
selectionColor.backgroundColor = HEXCOLOR(0XFFF1F1F1);
self.selectedBackgroundView = selectionColor;
self.contentView.backgroundColor = [UIColor whiteColor];
Nothing really more to it. Just a simple label added with autolayout to fill with a padding of 5.
Solution:
Create a subclass of UILabel and just not call super
- (instancetype) initWithColor:(UIColor *)color
{
self = [super init];
if (self) {
[super setBackgroundColor:color];
}
return self;
}
- (void) setBackgroundColor:(UIColor *)backgroundColor
{
//do nothing here!
}
The default behavior of UITableView is that when a cell is selected the background color of all the cell's subviews is temporarily removed.
We usually handled this issue by subclassing UILabel, overwrite setBackgroundColor: and simply do not call [super setBackgroundColor:] after we've set our own color.
#interface MyLabel : UILabel
#property(nonatomic) BOOL backgroundColorLocked;
#end
#implementation MyLabel
-(void) setBackgroundColor:(UIColor*)backgroundColor {
if (_backgroundColorLocked) {
return;
}
super.backgroundColor = backgroundColor;
}
#end
Usage:
MyLabel* label = …;
label.backgroundColor = UIColor.redColor;
label.backgroundColorLocked = YES;
As long as backgroundColorLocked is YES no-one, not even UITableView(Cell), can change the label's background color.
I have a button that I am trying to add a border to like this:
.h
#interface MyButton : UIButton
.m
#implementation MyButton
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
}
self.layer.borderWidth=1.0f;
self.layer.cornerRadius = 4;
self.layer.borderColor=[[UIColor blueColor] CGColor];
return self;
}
The border is not set, what am I doing wrong?
UIButton is not meant to be subclassed, I recommend using a category if all you are trying to do is style buttons. Here is an example category to accomplish what you want... note that this has not been run through a compiler, but you get the idea:
UIButton+Styles.h
#import <UIKit/UIKit.h>
#interface UIButton (Styles)
-(void)styleWithBorderColor:(UIColor *)color;
#end
.m
#import "UIButton+Styles.h"
#import <QuartzCore/QuartzCore.h>
#implementation UIButton (CURStyles)
-(void)styleWithBorderColor:(UIColor *)color
{
self.layer.masksToBounds = YES;
self.layer.cornerRadius = 4.0f;
self.layer.borderWidth = 1.0f;
self.layer.borderColor = color.CGColor;
}
#end
Now when you want to style a button simply import the category and call it like so:
[myButton styleWithBorderColor:[UIColor whiteColor]];
EDIT:
Please see comments... these should be prefixed to avoid confusion with framework methods
Add
self.layer.masksToBounds = YES;
Also if you are creating your button via Nib or Storyboard file initWithFrame method will not be called. In such a case do not forget to place your additional code in awakeFromNib method
- (void)awakeFromNib
{
self.layer.masksToBounds = YES;
self.layer.borderWidth=1.0f;
self.layer.cornerRadius = 4;
self.layer.borderColor=[[UIColor blueColor] CGColor];
}
There is nothing wrong with subclassing UIButton at all.. I suggest maybe you need to set
self.backgroundColor = nil; // or [UIColor clearColor]
self.layer.backgroundColor = desiredBackgroundColor.CGColor;
The backgroundcolor in UIView covers the border on the layer, move the background color to the layer instead to put it behind (and clip it to) the border
I am facing problem in custom initialization of my subview class the method somewhat look like this
my sub view .h file lloks like this
#import <UIKit/UIKit.h>
#interface mysubview : UIView
#property(nonatomic,strong)UIButton *mybutton;
#end
ViewController.m file
-(id)initWithbutton:(CGRect)frame color:(UIColor*)mycolor
{
NSLog(#"Into the initializer");
self = [super initWithFrame:frame];
if (self) {
// Initialization code
mybutton = [[UIButton alloc]initWithFrame:CGRectMake(frame.size.width/2,frame.size.height/2,20,20)];
[mybutton setTitle:#"b" forState:UIControlStateNormal];
[mybutton setTitle:#"b" forState:UIControlStateHighlighted];
self.backgroundColor = mycolor;
[self addSubview:mybutton];
}
return self;
}
and in my viewcontroller i am calling it as
p_subview1 = [[mysubview alloc] initWithbutton:CGRectMake(10,10,70,70) color:[UIColor blueColor]];
but during compilation i am getting error no visible interface in my subview class has this method plz consider if i am doing something silly here I am new to iphone development.