Change UILabel background color - iphone

my code is
UILabel *pTeamAlreadybl=[[UILabel alloc]initWithFrame:CGRectMake(110, 275,180,30)];
pTeamAlreadybl.text=#" Team already?";
pTeamAlreadybl.font=[UIFont boldSystemFontOfSize:16];
pTeamAlreadybl.textAlignment=UITextAlignmentLeft;
pTeamAlreadybl.textColor=[UIColor colorWithRed:61.0/255.0 green:61.0/255.0 blue:61.0/255.0 alpha:0.6];
pTeamAlreadybl.shadowColor = [UIColor colorWithWhite:1.0 alpha:0.7];
How can I change label's background color?

You miss
pTeamAlreadybl.backgroundColor=[UIColor anycolor];
write this code

To change the background color of a UILabel, you need to set the backgroundColor property of the UILabel object. You can do so by using the following line of code:
[pTeamAlreadybl setBackgroundColor:[UIColor colorOfYourChoice]];
OR
pTeamAlreadybl.backgroundColor = [UIColor colorOfYourChoice];

pTeamAlreadybl.backgroundColor = [UIColor ...];
Doesn't the above code work?

[pTeamAlreadybl setBackgroundColor:[UIColor YourColor]];
YourColor as mentioned below
blackColor;
darkGrayColor;
lightGrayColor
whiteColor;
grayColor;
redColor;
greenColor;
blueColor;
cyanColor;
yellowColor;
magentaColor;
orangeColor;
purpleColor;
brownColor;
clearColor;
you can use customized color as well like this
[UIColor colorWithRed:61.0/255.0 green:61.0/255.0 blue:61.0/255.0 alpha:0.6]

Related

Background transparancy of a textfield

I have an application that contains a textfield.
Is there a way to change background transparency of this textfield in code?
Yes you can do it.
You can set transparency by setting its alpha. You can pic color code with digital color meter ( its a inbuilt application in mac).
if you set alpha 1 then there will be no transparency. set transparency according to your need (set alpha less then 1).
Try this:
textfield.backgroundColor=[[UIColor colorWithRed:208.0/255.0 green:15.0/255.0 blue:202.0/255.0 alpha:0.6] CGColor];
You can set with below mentioned code in Xamarin
txtNumber.BackgroundColor = UIColor.White;
txtNumber.Alpha = 0.5f;
Reference 1: transparent UITextView
Reference 2: How do I create a transparent UITextField?
Similary:
txtNumber.BackgroundColor = UIColor.Clear;
use Custom UITextField
UITextField *yourTextField= [[UITextField alloc] initWithFrame:frame];
yourTextField.borderStyle = UITextBorderStyleNone;
[yourTextField setBackgroundColor:[UIColor clearColor]];
Just put bellow code for white background
[yourTextField setBackgroundColor:[UIColor whiteColor]];
or if want to clear Color then bellow code
[yourTextField setBackgroundColor:[UIColor clearColor]];
so simple
:)

Table Cell background and text color cannot be set accurately.

I need to set the background of table cells to a specific color.
(#222222 or RGB(32,32,32) respectively)
The background of the table view in IB is set properly. The correct gray appears in the back of the table header and in section headers etc.
But I struggle with the cells.
To customize the cell's apperance I subclass UITableCell and implement the layoutSubviews method.
This works fine:
- (void)layoutSubviews {
[super layoutSubviews];
self.selectionStyle = UITableViewCellSelectionStyleGray;
self.backgroundColor = [UIColor darkGrayColor];
self.contentView.backgroundColor = [UIColor darkGrayColor];
self.textLabel.textColor = [UIColor whiteColor];
self.detailTextLabel.textColor = [UIColor grayColor];
}
However, grayColor and darkGrayColor simply do not match the colour that I need.
Naturally I tried the colorWithRed:green:blue:alpha method of UIColor.
- (void)layoutSubviews {
[super layoutSubviews];
self.selectionStyle = UITableViewCellSelectionStyleGray;
self.backgroundColor = [UIColor colorWithRed:(32/256) green:(32/256) blue:(32/256) alpha:1];
self.contentView.backgroundColor = [UIColor colorWithRed:(32/256) green:(32/256) blue:(32/256) alpha:1];
self.textLabel.textColor = [UIColor whiteColor];
self.detailTextLabel.textColor = [UIColor colorWithRed:(32/256) green:(32/256) blue:(32/256) alpha:1];
}
That one results in black background and black color of the detailTextLable.
(Of course it is senseless using the same color for a background and for a text label. I am just trying to work out what colorWithRed:green:blue:alpha does and does not.)
With plain style tables I am fine. Those' cells do not have a background color at all. When I just omit setting the backgroundColor and the contentView's background Color propierties then the background of the cells is displayed as defined as the Table's background color in IB.
But with grouped tables the standard background is some light gray which I want to change to some more decent color that matches my client's style guide.
What am I doing wrong? Do I use colorWithRed:green:blue:alpha properly?
Any suggestion is much appreciated.
I would try an other methods of calucaluting the color float:
[UIColor colorWithRed:(32.0f/255.0f) green:(32.0f/255.0f) blue:(32.0f/255.0f) alpha:1.0f];
since 0 is also include you have 0 to 255 values not 1 to 256 values.
If you want the cell to be transparent use [UIColor clearColor]
32/256 = 0 but 32/256.0 = 0.125.
Thanks for putting your finger to the error.
However, this does not exactly answer the question related to the difference between plain and grouped table style.
Just in case somebody finds the question interesting:
The grouped style comes with a background view. I set that to nil.
The gropued style always has a cell background color set to something. Therefore omitting the self.backgroundColor=... statement was not sufficient.
finally
self.backgroundColor = [UIColor clearColor];
did the trick. It was just not remotely connected to the objective-c mistake that I made too.

iphone : How to draw line on Label?

I want to make my label as shown in the image
I know I can get this effect by putting image view on it.
but is there any other method to do ?
How can I put line on label ?
Try this,
UILabel *blabel = [[UILabel alloc] initWithFrame:CGRectMake(XX, 6, 271, 26)];
blabel.text = #"Hellooooooo";
blabel.textAlignment = UITextAlignmentCenter;
blabel.backgroundColor = [UIColor clearColor];
blabel.textColor = [UIColor blackColor];
blabel.font = [UIFont systemFontOfSize:14];
[scrollDemo addSubview:blabel];
//underline code
CGSize expectedLabelSize = [#"Hellooooooo" sizeWithFont:blabel.font constrainedToSize:blabel.frame.size lineBreakMode:UILineBreakModeWordWrap];
UIView *viewUnderline=[[UIView alloc] init];
viewUnderline.frame=CGRectMake((blabel.frame.size.width - expectedLabelSize.width)/2, expectedLabelSize.height + (blabel.frame.size.height - expectedLabelSize.height)/2, expectedLabelSize.width, 1);
viewUnderline.backgroundColor=[UIColor blackColor];
[scrollDemo addSubview:viewUnderline];
[viewUnderline release];
The line above will appear below the text. You just need to change Y for UIView and it'll do wonders :)
put another label with "_" over it
transparent background.
you can create UIView with line's height and width and give background color to it. Put UIView over your UILabel .
For one of my projects I've created an UILabel subclass, which supports multiline text, underline, strikeout, underline/strikeout line offset, different text alignment and different font sizes.
Please see provided link for more info and usage example.
https://github.com/GuntisTreulands/UnderLineLabel
Place a UIImageView with line image on your label so when you run application it will fit.

Setting the background of a UILabel (transparency problem with .png files)

I'm trying to set the background of a UILabel by setting its property backgroundColor.
But I have a problem: the backgroundColor is created from a .png file (with transparency) and the result is that the transparent part appear in black.
Any solution without the need of custom subclasses?
Update
I tried setting the UILabel property opaque to NO but the problem is still there.
It sounds like you might have forgotten to change the opaque property of the UILabel:
label.opaque = NO;
Alternatively, you can set this property in the InterfaceBuilder view of Xcode.
I had the same problem, and the solution is:
[yourLabel setBackgroundColor:[UIColor clearColor]];
Changing opaque or alpha value didn't work.
Maybe you can try this one:
[your_label setBackgroundColor:[UIColor colorWithPatternImage:[UIImage imageNamed:#"yourImage.png"]]];
You'll also need to save it as a png24 or 24bit png file - at least that's the case when saving from PhotoShop
[footer setBackgroundColor:[UIColor colorWithPatternImage:[UIImage imageNamed:#"Favourite_商品文字背景.png"]]];
[footer setOpaque:NO];
it must be work,goodLuck!
Change its opaque property to NO.
If it doesn't help, you can add imageView behing your label:
[yourLabel setOpaque:NO];
[yourLabel setBackgroundColor:[UIColor clearColor]];
UIImageView *labelBkg = [[UIImageView alloc] initWithFrame:yourLabel.frame];
[labelBkg setImage:[UIImage imageNamed:#"yourImageName.png"]];
[self.view insertSubview:labelBkg belowSubview:yourLabel];
[labelBkg release];

Setting Cell Text Color

Apparently this is deprecated :
cell.textColor = [UIColor whiteColor];
Does anyone know what the best way to change the color of a Cell Text ?
Edit after comment :
I'm now using this :
cell.textLabel.textColor = [UIColor whiteColor];
but the color is still not changing, see the IB parameters :
cell.textLabel.textColor = [UIColor whiteColor];
Like says Apple on the UITableViewCell page http://developer.apple.com/library/ios/#documentation/uikit/reference/UITableViewCell_Class/Reference/Reference.html
The color of the title text. (Deprecated in iOS 3.0. Instead set the text color attribute of the UILabel objects assigned to the textLabel and detailTextLabel properties.)
You must use the textLabel instead textColor ;-)
Edit : you must add your OWN textLabel.
You must forget cell.text