UILabel Setting Transparent Background Color? - iphone

I am looking to add a black label with a transparent background to my view (see below)
// ADD LABEL
UILabel *label = [[UILabel alloc] init];
[label setFrame:CGRectMake(124, 312, 72, 35)];
[label setText:#"Yay!"];
[label setTextAlignment:UITextAlignmentCenter];
[[self view] addSubview:label];
[label release];
When I add the label it always comes out black text on a white background. I have looked in the NIB and the only way I can see to make this work is set the background > color > opacity to 0, or in Xcode:
[label setBackgroundColor:[UIColor clearColor]];
Is this the right way to do this?
Cheers Gary

I can confirm that creating a label programatically with background color set using
[label setBackgroundColor:[UIColor clearColor]];
will work. I'm using this in one of my apps.
Also, when you set the background color in Interface Builder, you can set the opacity of the background color to 0. Make sure that you don't set the opacity of the label itself to 0, or the text will also be transparent.

Swift 3 and 4:
yourView.backgroundColor = UIColor.clear

Related

How to set background image for text into UIlabel?

We can set color to text in UILabel. But can we set image to text in UILabel? Can you help me? Thanks all!
UILabel *lbl = [[UILabel alloc]initWithFrame:CGRectMake(20, 220, 200, 30)];
[lbl setText:#"Hello"];
[lbl setTextColor:[UIColor yellowColor]];
[lbl setBackgroundColor:[UIColor redColor]];
if you need to set image for your UILabel, you can try like this:
[lbl setBackgroundColor:[UIColor colorWithPatternImage:[UIImage imageNamed:#""]]];
[self.view addSubview:lbl];
If you want your textColor as Image then please do this
[lbl setTextColor:[UIColor colorWithPatternImage:[UIImage imageNamed:#"button_select.png"]]];
UILabel is only for text. If you want an image, use an UIImageView.
If you want to replicate the image you linked to using a UILabel, you can do that by setting the label's text and backgroundColor. Then you useUIColor colorWithPatternImage:for the orange color pattern. Then use that color for the label'stextColor`.

Moving labels on circle

I have a circle that turns over, how can i put on him some labels that will turn with him also?
my point is that the circle and labels should be on same angel.
Make the label a subview of the UIView you are using to present the circle.
In IB just drag the label onto the the UIView you are using for the circle image or if you are not using IB just add the label to the circle like this:
[circleUIView addSubview:theUILabel];
Download demo source code this url http://www.devedup.com/index.php/2010/03/03/iphone-animate-an-object-along-a-path/
Now changes into Canvas.m file's animateCicleAlongPath method.
Replace to image to label.
UILabel *label = [[UILabel alloc]init];
[label setText:#"test"];
[label setTextColor:[UIColor redColor]];
//Set the frame of the view - which is used to position it when we add it to our current UIView
label.frame = CGRectMake(1, 1, 20, 20);
label.backgroundColor = [UIColor clearColor];
[self addSubview:label];
[label.layer addAnimation:pathAnimation forKey:#"moveTheSquare"];

Navigation title gets hidden by bar button

In my app when I'm setting the title more than 13 characters it is hidden by bar button.So how do i overcome from this problem.Please help me.Thanks in advance.
Use a smaller font or a shorter title, or use a multiline title by customizing the navBar's titleView with a custom UILabel. For example,
// CUSTOMIZE NAVIGATION BAR TITLE LABEL
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0.0, 0.0, 320.0, 44.0)];
[label setBackgroundColor:[UIColor clearColor]];
[label setFont:[UIFont boldSystemFontOfSize:20.0]];
[label setAdjustsFontSizeToFitWidth:YES];
[label setTextAlignment:UITextAlignmentCenter];
[[self navigationItem] setTitleView:label];
[label release];
You can customize it however you like.

Set NavigationBarButton Title Text Color

I just wanted to know if it is possible to set the NavigationBarButton's title's text color, and if so how?
I did the following. In -(id)init add:
// CUSTOMIZE NAVIGATION BAR TITLE COLOR
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0.0, 0.0, 320.0, 44.0)];
[label setBackgroundColor:[UIColor clearColor]];
[label setFont:[UIFont boldSystemFontOfSize:20.0]];
[label setAdjustsFontSizeToFitWidth:YES];
[label setTextAlignment:UITextAlignmentCenter];
[[self navigationItem] setTitleView:label];
[label release];
Then in -(void)viewDidLoad add:
[[[self navigationItem] titleView] setText:#"TITLE"];
[[[self navigationItem] titleView] setTextColor:titleColor];
I put them like this so that I could change the title and titleColor based on how one got to this screen. If you don't need to customize, then put it all in init or viewDidLoad. Doing it like this I get a warning that "UIView may not respond to '-setText'" even though it all works fine.
You can set an image for the UIBarButtonItem and thus fully customize the title text.

How to change the text color on UINavigationBar button items

I've got a UINavigationController and i've changed it to white using the Tint property of the navigation bar in Interface Builder. But the text in buttons and the title is still the default color, white, and so gets lost against the white background. Anyone know how to work around this?
As of iOS 5 this it much easier, using the UIBarButtonItem appearance proxy:
NSDictionary *attributes = [NSDictionary dictionaryWithObjectsAndKeys:
[UIColor blackColor],
UITextAttributeTextColor,
[UIColor clearColor],
UITextAttributeTextShadowColor, nil];
[[UIBarButtonItem appearance] setTitleTextAttributes: attributes
forState: UIControlStateNormal];
Set this in application:didFinishLaunchingWithOptions:
for (id subView in theNavigationBar.subviews) {
if ([subView isKindOfClass:[UIButton class]]) {
[(UIButton *)subView setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
[(UIButton *)subView setTitleShadowColor:[UIColor clearColor] forState:UIControlStateNormal];
}
}
Here's one way:
[[theNavigationBar.subviews objectAtIndex:1] setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
[[theNavigationBar.subviews objectAtIndex:2] setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
However, HUGE, caveat. This is highly likely to break on a future OS release and is not recommended.
At the very least you should perform a lot of testing and make sure you your assumptions of the subview layout of the navigation bar are correct.
Or you use your own button bar item subclass with a setter you specify, lus isn't iPhone os 3 suppose to exposé text color for EVERY button
I did as drunknbass suggested. I resorted to making a series of images for back-button in a few states, and regular buttons in a few states, and put them in a custom UIButton subclass, setting up the appropriate styles.
As a solution it doesn't scale particularly well, but my needs were simple enough. It has the advantage of not breaking if the subview orders in the built in controls change though, which is the obvious downside of that approach. Disadvantage of needing several images. The other tricky thing is handling an orientation change, since the buttons should change size.
On a related note, if you do change the tint color of your navigation bar, it does not play well with the special "More" view controller for customizing the order of a tab bar. There doesn't seem to be any "acceptable" way to change the color of the toolbar in that view.
Can't you iterate the contents of the UINavigationBar view to find the buttons in the awakeFromNib?
Here the clean way (relying on public API) to do this IMHO : you should use the titleView property of the navigationItem to apply it your own UILabel on which you will customize the textColor
UILabel* lbl = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 320, 20)];
lbl.textAlignment=UITextAlignmentCenter;
lbl.backgroundColor=[UIColor clearColor];
lbl.textColor=[UIColor colorWithRed:0.18 green:0.2 blue:0.56 alpha:1];
theControllerOnWhichYouWantToHaveATitleWithYourOwnColor.navigationItem.titleView=lbl;
[lbl release];
you can change title appearance by adding label into the navigation bar like bellow.
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 100, 35)];
label.backgroundColor = [UIColor clearColor];
label.textAlignment = UITextAlignmentCenter;
label.textColor =[UIColor whiteColor];
label.text=self.title;
self.navigationItem.titleView = viewLabel;
[label release];