How can I programmatically set the background color of a UILabel? - cocoa-touch

I'm diving into iOS development and I'm programmatically creating some labels, but I can't seem to set their background color to black. From what I read, it seems simple enough, here's my code...
UILabel *lbl = [[[UILabel alloc] initWithFrame:CGRectMake(x, y, width, height] autorelease];
[[lbl layer] setBorderColor:[[UIColor blackColor] CGColor]];
[[lbl layer] setBorderWidth:1.0];
[[lbl layer] setBackgroundColor:[[UIColor blackColor] CGColor]];
[[self view] addSubview:lbl];
When I do this, the border color and width work as expected, but the background color remains white. Am I forgetting to do something?
Thanks so much for your help!

A UILabel already has a .backgroundColor property, you don't need to tweak its layer...
lbl.backgroundColor = [UIColor blackColor];

setting background color of UILabel
[lbl.backgroundColor:[UIColor colorWithPatternImage:[UIImage imageNamed:#"color_lightblue.png"]]];
setting TextColor of UILabel with image color
[label_Description setTextColor:[UIColor colorWithPatternImage:[UIImage imageNamed:#"color_lightgray.png"]]];
simple background color
lbl.backgroundColor = [UIColor blackColor];

swift 2
label.backgroundColor = UIColor.whiteColor()
other formula:
let label = UILabel(frame: CGRectMake(0, 0, view.frame.width, view.frame.height))
label.backgroundColor = UIColor.whiteColor()
label.textAlignment = NSTextAlignment.Center
label.text = message
label.numberOfLines = 2
label.font = UIFont.systemFontOfSize(12)

lbl.backgroundColor = [UIColor blackColor];

Use this
with one code you can set color
LabelObj.backgroundColor = [UIColor blackColor];

Related

self.navigationItem.titleView cutting off UIBarButtonItem after rotation

I use a self.navigationItem.titleView and I set two UIButtons as the self.navigationItem.rightBarButtonItems in the UINavigationBar. I have achieved this in my navigationBar like so
Whenever I rotate the view controller landscape and then back, the uibarbutton item gets cut off like so
The code I use to set the titleView is this:
UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 0, 44)];
titleLabel.backgroundColor = [UIColor clearColor];
titleLabel.numberOfLines = 2;
titleLabel.font = [UIFont boldSystemFontOfSize:13.0f];
titleLabel.shadowColor = [UIColor colorWithWhite:0.0 alpha:0.5];
titleLabel.textAlignment = UITextAlignmentCenter;
titleLabel.textColor = [UIColor whiteColor];
titleLabel.text = self.tabTitle;
self.navigationItem.titleView = titleLabel;
I have no idea why this is happening and I wonder if if any of you know how to fix this.

UITextView Shadow Not Working

I am trying to get a UITextView's layer's shadow to work in a UITableView header. I have a UIView that I am formatting everything in, and then setting the headerview equal to it.
UIView * view = [[UIView alloc] initWithFrame: CGRectMake(0, 0, self.tableView.frame.size.width, 450);
UIColor * baseColor = [[UIColor alloc] initWithRed: 45/255.0
green: 100/255.0
blue: 150/255.0
alpha: 1.0];
view.backgroundColor = baseColor;
...
UITextView * newCommentField = [[UITextView alloc] initWithFrame:CGRectMake(20, 230, 270, 120)];
newCommentField.text = #"New Comment";
newCommentField.tag = 3;
newCommentField.layer.shadowOffset = CGSizeMake(0, 3);
newCommentField.layer.shadowRadius = 5.0;
newCommentField.layer.shadowColor = [UIColor blackColor].CGColor;
newCommentField.layer.shadowOpacity = 0.8;
[view addSubview:newCommentField];
...
self.tableView.tableHeaderView = view;
Everything shows up properly in the view. However, the shadow is not appearing. I don't know what is going wrong here. I have even tried modifying the layer's frame and making it the size of the comment field, bigger and the same size.
You're not setting background color for newCommentField.
Try:
newCommentField.backgroundColor = [UIColor whiteColor];
You can do it this way also... Create a view (eg. bgView) of same size as your textView and add that textView as a subView of bgView, which will be added as subView of you header view. Apply the shadow effect to the bgView instead.
UIView *bgView=[[UIView alloc]initWithFrame:CGRectMake(20, 230, 270, 120)];
bgView.backgroundColor=[UIColor whiteColor];
bgView.layer.shadowOffset = CGSizeMake(0, 3);
bgView.layer.shadowRadius = 5.0;
bgView.layer.shadowColor = [UIColor blackColor].CGColor;
bgView.layer.shadowOpacity = 0.8;
[bgView addSubview:newCommentField];
[view addSubview:bgView];

UIButton has gap

Making a UIButton. for some reason, it has a white gap around it (the grey area is the UIView the button is in):
code:
UIView *photoView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, [[UIScreen mainScreen] bounds].size.width, (THUMBNAIL_SIZE) + (PHOTO_VIEWER_OFFSET_COLUM_1 * 2))];
photoView.backgroundColor = [UIColor lightGrayColor];
UIScrollView *myScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, [[UIScreen mainScreen] bounds].size.width, photoView.frame.size.height)];
myScrollView.backgroundColor = [UIColor lightGrayColor];
UIButton *myButton = [UIButton buttonWithType:UIButtonTypeCustom];
myButton.frame = CGRectMake(0, 0, 50, 50);
myButton.titleLabel.font = [UIFont fontWithName:labelFont size:16];
[myButton setTitle:#"X" forState:UIControlStateNormal];
[myButton setBackgroundImage:[UIImage imageNamed:#"add-report.png"] forState:UIControlStateNormal];
myButton.center = CGPointMake(((photoView.frame.size.width - myScrollView.frame.size.width) / 2) + myScrollView.frame.size.width, photoView.frame.size.height / 2);
[photoView addSubview:myButton];
[self.view addSubview:photoView];
I dont' want the white buffers space there, but i can't figure out how to get rid of it.
any ideas?
EDIT: if i try to change the background color:
myButton.backgroundColor = [UIColor orangeColor];
i get this effect:
It might be the text label that has a white background be default?
Try setting every color to [UIColor clearColor] such as button.background and button.titleLabel.background.
Also try to modify the image view. Maybe it does not scale automatically as expected:
button.imageView.contentMode = UIViewContentModeScaleToFill;

ios "Data Loading" Notification with transparent background

I am trying to create a view in iOS to let the user know their data is loading... it should have about a 200x200 Rounded-corner box in the middle with a spinner and the words "Data Loading..." and a transparent background.
This all is working except my 200x200 rounded-corner box is also transparent.
Here is the code I am using:
UIView *loadingDataView = [[UIView alloc] initWithFrame:CGRectMake(0, 44, 320, 367)];
loadingDataView.alpha = 0.4;
loadingDataView.backgroundColor = [UIColor clearColor];
UIView *viewWithSpinner = [[UIView alloc] initWithFrame:CGRectMake(110, 106, 100, 100)];
[viewWithSpinner.layer setCornerRadius:15.0f];
viewWithSpinner.backgroundColor = [UIColor blackColor];
UILabel *msg = [[UILabel alloc] initWithFrame:CGRectMake(5, 75, 90, 20)];
UIActivityIndicatorView *spinner = [[UIActivityIndicatorView alloc] initWithFrame:CGRectMake(5, 5, 90, 70)];
spinner.activityIndicatorViewStyle = UIActivityIndicatorViewStyleWhite;
[spinner startAnimating];
msg.text = #"Data Loading";
msg.font = [UIFont systemFontOfSize:14];
msg.textAlignment = UITextAlignmentCenter;
msg.textColor = [UIColor whiteColor];
msg.backgroundColor = [UIColor clearColor];
viewWithSpinner.opaque = NO;
viewWithSpinner.backgroundColor = [UIColor blackColor];
[viewWithSpinner addSubview:spinner];
[viewWithSpinner addSubview:msg];
[loadingDataView addSubview:viewWithSpinner];
[self.view addSubview:loadingDataView];
Thanks.
No exactly an answer to your question, but check out the open source MBProgressHUD. It aims to be an open source replacement for the private UIProgressHUD class, and is exactly what you're trying to do.
The solution for your problem is that you should remove the alpha attribute of 0.4, that's what's turning your round view transparent, if your view is set to clearColor (this is not really a color, it just makes the view transparent) it makes no sense to add an alpha of 0.4. If what you want is a semi-transparent view surrounding your black rounded view, you should do the following:
[loadingDataView setBackgroundColor:[UIColor colorWithRed:1 green:1 blue:1 andAlpha:0.4]];
That will give you something whiteish kinda grayish, that should work.
However, I would recommend you to use the GIDAAlertView Class I developed, you can get the source and an example app on my GitHub:
It takes about 3 lines to get it working:
GIDAAlertView *spinnerAlert=[[GIDAAlertView alloc] initAlertWithSpinnerAndMessage:#"GIDAAlertView Spinner"];
//Show it ...
[spinnerAlert presentAlertWithSpinner];
//Later in your code, hide it
[spinnerAlert hideAlertWithSpinner];
This is how it looks like.
you are telling it to be clear...
loadingDataView.backgroundColor = [UIColor clearColor];
What do you want it to be black?

Drawing border in UILabel

Well, I have some problems with border drawing in UILabel. Here is my snippet of code
CGRect frame = CGRectMake(10, 10, 320, 120);
UILabel *label = [[UILabel alloc] initWithFrame:frame];
label.textColor = [UIColor greenColor];
label.font = [UIFont fontWithName:#"Verdana" size:12];
label.textAlignment = UITextAlignmentCenter;
label.text = #"Some text to display";
label.layer.backgroundColor = [UIColor cyanColor].CGColor;
label.layer.borderColor = [UIColor redColor].CGColor;
[self.window addSubview:label];
[label release];label=nil;
I have QuartzCore included and I use iOS4.3 When I launch the app in sumulator text is displayed but not the border and background color.
What is wrong here ?
Looking in CALayer.h, we see that the default value for borderWidth is 0.0.
/* The width of the layer's border, inset from the layer bounds. The
* border is composited above the layer's content and sublayers and
* includes the effects of the `cornerRadius' property. Defaults to
* zero. Animatable. */
#property CGFloat borderWidth;
In order for the border to appear, you must set the borderWidth to something greater than zero.
You can set the background color on the label directly as so:
label.backgroundColor = [UIColor cyanColor];
To set a border, you need to set the width, which can be done like so:
label.layer.borderWidth = 2.0f;
Once you have set a border width, to set the color of the border on the label, you're setting the view's layer's border, which uses a CGColor, so you'll have to do this:
label.layer.borderColor = [UIColor redColor].CGColor;
And if you want to round the corners, you can add this:
label.layer.cornerRadius = 10.0f;
You don't need to set the background color. You really need to only set the borderWidth and borderColor.
label.layer.borderColor = [UIColor darkGrayColor].CGColor;
label.layer.borderWidth = 1.0;