Change background color with a variable iOS - objective-c

I want to change the background color of my label with a variable how can I do that ?
This is my code but I'd like to have a variable instead of 'redColor'
[publisherLabel setBackgroundColor:[UIColor redColor]];

Variable with color from an RGBA value.
UIColor *myColor = [UIColor colorWithRed:100.0/255.0 green:101.0/255.0 blue:102.0/255.0 alpha:1.0]];
You can also use HEX if you want:
#define HEXCOLOR(c) [UIColor colorWithRed:((c>>24)&0xFF)/255.0
green:((c>>16)&0xFF)/255.0
blue:((c>>8)&0xFF)/255.0
alpha:((c)&0xFF)/255.0];
// usage:
UIColor* c = HEXCOLOR(0xff00ffff);

Related

How to set BarChartView barColor?

This is the UI Picture
I don't know how to set a lightgray color
I try to use
self.chartView.drawGridBackgroundEnabled = YES;
self.chartView.gridBackgroundColor = [UIColor grayColor];
But the effect is not what I want
You have to enable BarShadow Property as mention below:
_barChartView.drawBarShadowEnabled = YES;
By default its showing GrayColor for customize Shadow Color you have to set shadow color in BarChartDataSet like below :
BarChartDataSet *set1 = [[BarChartDataSet alloc] initWithValues:Values];
set1.barShadowColor = [UIColor lightGrayColor]; // Set your custom color here
Hope this will help you to show your Custom color in bar shadow.
You can use
UIColor *color = ...;
color = [color colorWithAlphaComponent:0.5f];
OR You can try with RGB values.
[UIColor colorWithRed:(160/255.0) green:(97/255.0) blue:(5/255.0) alpha:1]

Issue with using RGB colors in IOS

I want to use RGB color to set set background color of UIButton.
I have lots of UIButtons so I decided to make a custom class.But when I try to use colorWithRed I am getting error.
First Code:
[self.layer setBackgroundColor:[UIColor colorWithRed:60/255.0f green:146/255.0f blue:180/255.0f alpha:1.0f]];
Error
Second Code
self.layer.backgroundColor = [UIColor colorWithRed:60/255.0f green:146/255.0f blue:180/255.0f alpha:1.0f];
Error
try to use this code
self.layer.backgroundColor = [UIColor colorWithRed:60/255.0f green:146/255.0f blue:180/255.0f alpha:1.0f].CGColor;
You need to convert UIColor to CGColor which can be done by above code.
Hope this will solve your problem
layer backgroundcolor is of cgcolorref type, so cant use uicolor. we need to use cgcolor or convert uicolor to cgcolor and use it.
example:
[[UIColor whiteColor] CGColor];
or
[UIColor whiteColor].CGColor;

UIButton with different color text in each line

I want to have a UIButton with two lines of text, with each line in different color. Is it possible to have like that ?
There are 2 Approaches I can think of :
Approach 1 (easy) : Make it as an image button
Approach 2 (hard) : Make a custom UIButton, with 2 separate UILabel, such that you can configure different colors for them
To achieve Approach 2, you first create a class with UIButton as superclass. Then, override - (void)drawRect method. In order not to repeat answer in SO, please read this: How to override -drawrect in UIButton subclass?
Hi this answer only applicable in above ios 6...
Use ios 6 's attributed text to achieve this...
NSString* infoString=#"This is an example of Attributed String";
NSMutableAttributedString *attString=[[NSMutableAttributedString alloc] initWithString:infoString];
NSInteger _stringLength=[infoString length];
UIColor *_red=[UIColor redColor];
UIFont *font=[UIFont fontWithName:#"Helvetica-Bold" size:20.0f];
[attString addAttribute:NSFontAttributeName value:font range:NSMakeRange(0, _stringLength)];
[attString addAttribute:NSForegroundColorAttributeName value:_red range:NSMakeRange(0, _stringLength/2)];
[self.attribButton setAttributedTitle:attString forState:UIControlStateNormal];
self.attribButton.titleLabel.numberOfLines=2;
Try like this...
In Swift
var main_string = "Hello World"
var string_to_color = "World"
var range = (main_string as NSString).rangeOfString(string_to_color)
var attributedString = NSMutableAttributedString(string:main_string)
attributedString.addAttribute(NSForegroundColorAttributeName, value: appSingleton.appRedColor , range: range)
self.celciusButton.setAttributedTitle(attributedString, forState: UIControlState.Normal)
yes sure..!!! make an image what you exactly want to show then set the image as an background of that button..! its simple...
First, make your button of "custom" type. If you need to do that programmatically:
UIButton* myButton = [UIButton buttonWithType: UIButtonTypeCustom];
myButton.frame = CGRectMake (x,y,width,height);
Now make your labels:
UILabel* line1 = [[UILabel alloc] initWithFrame:CGRectMake(0,0,buttonWidth,buttonHeight/2)];
UILabel* line2 = [[UILabel alloc] initWithFrame:CGRectMake(0,buttonHeight/2,buttonWidth,buttonHeight/2)];
And assign the text colors you want:
line1.textColor = [UIColor blueColor];
line2.textColor = [UIColor redColor];
Then add the labels to the button, and the button to your view:
[myButton addSubview:line1];
[myButton addSubview:line2];
[self.view addSubview:myButton]; // only if you've created the button programmatically

Objective c pagecontrol background color

I have a scrollview with a pagecontrol, when i change her color, my pagecontrol dissapear.
UIColor *col = [UIColor colorWithRed:14.5 green:35.6 blue:72.9 alpha:1];
self.pageControl.backgroundColor = col;
The RGB values are supposed to be between 0 and 1. (See the docs). So you're setting the background color to fully opaque white here.
UIColor *col = [UIColor colorWithRed:14.5 green:35.6 blue:72.9 alpha:1];
If you have the RGB ASCII values 0 to 255
you can specify it as
UIColor *col = [UIColor colorWithRed:(255/255) green:(0/255) blue:(0/255) alpha:1]; // This represent Red color

UILabel sizeWithFont: problem. Clipping italic text

I have created a UILabel that displays a single large character. Even with clipsToBounds = NO; I still get clipping.
See link: http://img341.imageshack.us/img341/5310/screenshot20100814at243.png
I used the following code:
CGSize fBounds = [myLabel.text sizeWithFont:cFont];
To get what should be the bounding rectangle of the font. And the label is drawn with:
myLabel = [[UILabel alloc] initWithFrame:CGRectMake(20, 20, 280, 300)];
myLabel.clipsToBounds = NO;
myLabel.numberOfLines = 1;
myLabel.textAlignment = UITextAlignmentCenter;
myLabel.adjustsFontSizeToFitWidth = YES;
myLabel.minimumFontSize = 10;
myLabel.text = #"A";
myLabel.font = [UIFont fontWithName:#"CourierNewPSMT" size:300];
myLabel.textColor = [UIColor blackColor];
myLabel.backgroundColor = [UIColor colorWithRed:1 green:.5 blue:0 alpha:.5];
In the image below, the size returned from sizeWithFont is rendered by the semi-transparent blue rectangle overlay. As you can see, with an italic font (in this case Verdana-BoldItalic), the character extends past what sizeWithFont returns. Further, the UILabel's frame (the orange color) also clips the character. Thoughts? Maybe I could override some text drawing routine. Also, not sure if this is the same problem as here:
UIButton.titleLabel clipping text problem
Use attributed text + indent...
Looks like this is an apple problem. Ended up doing custom drawing with CoreText.