How to set background image for text into UIlabel? - iphone

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`.

Related

How to highlight only text in UILabel - IOS

I just want to highlight only text in UILabel, I have tried by giving backgroundColor for label, but it is highlighting the empty spaces also looks not good. So Is there any way to highlight text without resizing UILabel.
Please check the image, here labels are bigger than the text (center aligned)
Thanx.
Most of the other solutions don't consider text that spans multiple lines while still only highlighting the text, and they are all pretty hacky involving extra subviews.
An iOS 6 and later solution is to use attributed strings:
NSMutableAttributedString *s =
[[NSMutableAttributedString alloc] initWithString:yourString];
[s addAttribute:NSBackgroundColorAttributeName
value:[UIColor greenColor]
range:NSMakeRange(0, s.length)];
label.attributedText = s;
This will add a subview behind the text, with the correct size:
CGSize size= [[label text] sizeWithFont:[UIFont systemFontOfSize:18.0]];
NSLog(#"%.1f | %.1f", size.width, size.height);
NSLog(#"%.1f | %.1f", label.frame.size.width, label.frame.size.height);
UIView *highlightView=[[UIView alloc] initWithFrame:CGRectMake(0, 0, size.width, size.height)];
[highlightView setBackgroundColor:[UIColor greenColor]];
[self.view insertSubview:highlightView belowSubview:label];
[highlightView setCenter:label.center];
And don't forget: [label setBackgroundColor:[UIColor clearColor]];
try this
MTLabel
MTLabel *label4 = [MTLabel labelWithFrame:CGRectMake(20, 270, 250, 60)
andText:#"This label is highlighted, has a custom line height, and adjusts font size to fit inside the label."];
[label4 setLineHeight:22];
[label4 setFont:[UIFont fontWithName:#"Helvetica" size:30]];
[label4 setAdjustSizeToFit:YES];
[label4 setMinimumFontSize:15];
[label4 setFontHighlightColor:[[UIColor orangeColor] colorWithAlphaComponent:0.5]];
[self.view addSubview:label4];
You can have a UIImageView, set its background color of your choice then place your UIlabel on it.
That will surely solve your problem.
Here is Workaround code :
UIImageView * imgView = [[UIImageView alloc]initWithFrame:CGRectMake(10, 10, 100, 40)];
[imgView setBackgroundColor:[UIColor brownColor]];
[self.view addSubview:imgView];
UILabel * label = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 100, 40)];
[label setBackgroundColor:[UIColor clearColor]];
[label setTextAlignment:UITextAlignmentCenter];
label.text = #"My Label";
[imgView addSubview:label];
[label release];
[imgView release];
You can also achieve the same using Interface Builder.

right align self.navigationItem setTitleView

I need to align the navigationItem TitleView to the far right. (where the navigationItem rightBarButtonItem normally appears)
How can this be done ?
I have tried this but with no luck it still places TitleView in the center:
CGRect titleViewPos = CGRectMake(300, 0, 200, 10);
self.navigationItem.titleView.frame = titleViewPos;
UILabel *lbl = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 320, 44)];
lbl.textAlignment = UITextAlignmentRight;
lbl.backgroundColor = [UIColor clearColor];
lbl.text = #"TITLEVIEW";
self.navigationItem.titleView = lbl;
You will get output as
Just an idea:
UIButton *btn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
btn.frame = CGRectMake(300, 0, 200, 10);
[btn setTitle:#"titleView" forState:UIControlStateNormal];
self.navigationItem.titleView = btn;
You need to pass a UIElement (UIView,UILabel) to navigation item then you can set the position and make other customisation with navigation title or left button or right button.
May this will help you.
try this one, May be use-full
UILabel *lbNavTitle = [[UILabel alloc] initWithFrame:CGRectMake(0,40,320,40)];
lbNavTitle.textAlignment = NSTextAlignmentRight;
lbNavTitle.backgroundColor = [UIColor clearColor];
lbNavTitle.text = #"hello World";
self.navigationItem.titleView = lbNavTitle;
The titleview frame is automatically adjusted, if you want to obtain this result you can try this:
UILabel *titleLabel = [[UILabel alloc] initWithFrame:self.navigationController.navigationBar.frame];
[titleLabel setTextAlignment:UITextAlignmentRight];
[titleLabel setText:#"Your title"];
[titleLabel setBackgroundColor:[UIColor clearColor]];
[titleLabel setTextColor:[UIColor whiteColor]];
self.navigationItem.titleView = titleLabel;
NOTE: be sure to don't set self.title of your view controller otherwise you will overwrite the titleView
Another solution could be put the titleLabel into an UIBarButtonItem (customView) and set it like to self.navigationItem.rightBarButtonItem in this way you don't need to set the text alignement ad the frame of the label can be the proper text size
none of that worked for me. I took Desdanova's advice and just added as a subview. Makes sense because that's all it is! Initialize it with any frame position you want and then
[self.view addSubView:lbl];
and Voila!

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.

how to change the size of UILabel

I have a UILabel coded programmatically. I want to change the size of the label when i pressed a button. how to change the size of that label? this is my code
UILabel *theLabel11 = [[UILabel alloc] initWithFrame:CGRectMake(0,0,100,30)];
[theLabel11 setText:#"US"];
[theLabel11 setTextAlignment:UITextAlignmentCenter];
[theLabel11 setFont: [UIFont fontWithName:#"Arial" size:13.0f]];
[theLabel11 setBackgroundColor:[UIColor orangeColor]];
[theLabel11 setTextColor:[UIColor blackColor]];
[scroll1 addSubview:theLabel11];
You should declare your label as class property, so it can be accessed from other methods
To change the font size use
[theLabel11 setFont: [UIFont fontWithName:#"Arial" size:13.0f]];
To change the frame size of the label us
theLabel11.frame = CGRectMake(x, y, width, height);
A common idiom for adjusting the spatial information on a UIView is as below
label.frame = CGRectMake(
x,
y,
width,
height
);
You can get the old position and height via
label.frame.origin.x
label.frame.origin.y
label.frame.size.width
label.frame.size.height
If there is only one label added to scroll1 then, iterate the scrollview to get the label reference as follows in button action
for(UIView *subView in scroll1.subViews){
if([subView isKindOfClass:[UILabel class]]){
UILabel *lbl=(UILabel*)subView;
//change size of label here
}
}
if there are many labels assign a tag to each label while creating and check that in for loop

UILabel Setting Transparent Background Color?

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