Image and Title to UIbutton is not being set - ios

I'm trying to implement an image as well as title on UIButton. Size of UIButton is 86 x 96. I need to embed an image of size 86 x 76 on top and title of size 86 x 20 just below the image.
The code is
UIButton *generalButton = [UIButton buttonWithType:UIButtonTypeCustom];
[generalButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
generalButton.titleLabel.font = [UIFont fontWithName:#"Arial" size:13];
[generalButton setImageEdgeInsets:UIEdgeInsetsMake(0,0,20,0)];
[generalButton setTitleEdgeInsets:UIEdgeInsetsMake(76,0,0,0)];
generalButton.frame = CGRectMake(32,20,86,96);
[generalButton setImage:[UIImage imageNamed:#"flower_icon.png"] forState:UIControlStateNormal];
[generalButton setTitle:#"Flower" forState:UIControlStateNormal];
[self addSubview:generalButton];
But the problem is that only image is being shown, whereas the title is not showing up. And if I hide the image by commenting the code, then title shows up perfectly.
Please Help

Please try this code:
UIButton *generalButton = [UIButton buttonWithType:UIButtonTypeCustom];
[generalButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
generalButton.titleLabel.font = [UIFont fontWithName:#"Arial" size:13];
[generalButton setImageEdgeInsets:UIEdgeInsetsMake(20,22,20,0)];
[generalButton setTitleEdgeInsets:UIEdgeInsetsMake(50,-30,0,0)];
generalButton.frame = CGRectMake(32,20,86,96);
[generalButton setBackgroundColor:[UIColor yellowColor]];
[generalButton setImage:[UIImage imageNamed:#"Facebook"] forState:UIControlStateNormal];
[generalButton setTitle:#"Flower" forState:UIControlStateNormal];
[self.view addSubview:generalButton];
Note:
[generalButton setImageEdgeInsets:UIEdgeInsetsMake(20,22,20,0)];
[generalButton setTitleEdgeInsets:UIEdgeInsetsMake(50,-30,0,0)];
These two line are performing main role to repositioning your title and image so set all parameters as per your image height & width and title length.
In this code I change image name as I have and also changed back colour of button so please change according to your data.
Hope this will help you.

try this code:
UIButton *generalButton = [UIButton buttonWithType:UIButtonTypeCustom];
[generalButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
generalButton.titleLabel.font = [UIFont fontWithName:#"Arial" size:13];
[generalButton setImageEdgeInsets:UIEdgeInsetsMake(0,0,20,0)];
[generalButton setTitleEdgeInsets:UIEdgeInsetsMake(30,-50,0,0)];
generalButton.frame = CGRectMake(32,20,86,96);
[generalButton setImage:[UIImage imageNamed:#"RepresentativeIcon.png"] forState:UIControlStateNormal];
[generalButton setTitle:#"Flower" forState:UIControlStateNormal];
[self.view addSubview:generalButton];

Related

Line break mode and adjustsFontSizeToFitWidth not working obj c

I am populating a view with multiple buttons (number of buttons is varying. Size of buttons and button titles is varying). I need to set button titles in 2 lines or 1 line according to number of words in the title. Also the font size should be compatible with the button frame size. Including some of my code here.
`
// --------------------------
button1.titleLabel.font = [UIFont fontWithName:#"ProximaNovaSoft-Bold" size:35];
button1.titleLabel.numberOfLines=0;
button1.titleLabel.lineBreakMode= NSLineBreakByWordWrapping;
button1.titleLabel.adjustsFontSizeToFitWidth=YES;
[button1.titleLabel setTextAlignment: NSTextAlignmentCenter];
// --------------------------
[button1 setBackgroundImage:[UIImage imageNamed:imgColor1] forState:UIControlStateNormal];
[self.view addSubview:button1];
button2 = [[UIButton alloc] init];
button2 = [UIButton buttonWithType:UIButtonTypeRoundedRect];
button2.tag=2;
[button2 addTarget:self
action:#selector(createAlert:)
forControlEvents:UIControlEventTouchUpInside];
button2.frame = CGRectMake(self.view.bounds.size.width/6-5, button1.frame.origin.y+(button1.frame.size.height/3)+button1.frame.size.height/2+15, self.view.bounds.size.width/3, self.view.bounds.size.width/3);
NSString *imgColor2 = [alertColorArray objectAtIndex:1];
if ([imgColor2 isEqualToString:#"RED"] || [imgColor2 isEqualToString:#"BLUE"] || [imgColor2 isEqualToString:#"YELLOW"]) {
imgColor2 = [imgColor2 stringByAppendingString:#".png"];
}
else
{
imgColor2 = #"YELLOW.png";
}
[button2 setTitle:[alertNameArray objectAtIndex:1] forState:UIControlStateNormal];
[button2 setTintColor:[UIColor whiteColor]];
button2.titleLabel.font = [UIFont fontWithName:#"ProximaNovaSoft-Bold" size:25];
// --------------------------
// button2.titleLabel.lineBreakMode= NSLineBreakByWordWrapping;
button2.titleLabel.numberOfLines=2;
button2.titleLabel.adjustsFontSizeToFitWidth=YES;
// --------------------------
[button2.titleLabel setTextAlignment: NSTextAlignmentCenter];
[button2 setBackgroundImage:[UIImage imageNamed:imgColor2] forState:UIControlStateNormal];
[self.view addSubview:button2];
`
But this is not working as I need. What wrong with this?
try,
//change scale factor accordingly
[LABEL setMinimumScaleFactor:12.0/[UIFont labelFontSize]];
Solved this by counting number of words in the text and numberoflines is set accordingly.button1.titleLabel.numberOfLines=[self wordCount:[alertNameArray objectAtIndex:0]];

Programmatically changing UIButton font color depending on state

I'm trying to programmatically create a UIButton. However by default it's turning up white (which is the default color for my navigation bar, I feel that's relevant). I want it to just be Apple's default blue color in iOS7. I've managed to change the color for it's default state, but the moment I select it the text becomes white again. I cannot figure out how to keep it blue.
Could someone please explain to me how I can programmatically create a UIButton and have it act the same as though I created it in storyboard?
Current code:
UIButton *cancelButton = [UIButton buttonWithType:UIButtonTypeCustom];
cancelButton.frame = CGRectMake(320 - 150, 0, 140, 40);
[cancelButton setTitle:#"Cancel" forState:UIControlStateNormal];
cancelButton.titleLabel.tintColor = [UIColor colorWithRed:0.0 green:122.0/255.0 blue:1.0 alpha:1.0];
cancelButton.titleLabel.textColor = [UIColor colorWithRed:0.0 green:122.0/255.0 blue:1.0 alpha:1.0];
Thank you.
Try this code :
UIButton *cancelButton = [UIButton buttonWithType:UIButtonTypeCustom];
[cancelButton setFrame:CGRectMake(320 - 150, 0, 140, 40)];
[cancelButton setBackgroundColor:[UIColor clearColor]];
[cancelButton setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
[cancelButton setTitleColor:[UIColor blueColor] forState:UIControlStateHighlighted]; // This will helps you during click time title color will be blue color
[cancelButton setTitle:#"Cancel" forState:UIControlStateNormal];
[cancelButton addTarget:self action:#selector(button_Action) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:cancelButton];
Just tried with
[cancelButton setTitleColor:[UIColor colorWithRed:0.0 green:122.0/255.0 blue:1.0/255.0 alpha:1.0] forState:UIControlStateNormal];
For more information read official documentation of UIButton.
Have you tried this
[cancelButton setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
[cancelButton setTitleColor:[UIColor blueColor] forState:UIControlStateSelected];
This will solve your problem,
[cancelButton setTitleColor:YOUR COLOR forState:SPECIFIED STATE];
The state values are :
UIControlStateNormal
UIControlStateHighlighted
UIControlStateDisabled
UIControlStateSelected
Try using
- (void)setTitleColor:(UIColor *)color forState:(UIControlState)state
for specifying colors for all the required states.
Swift:
let button = UIButton (type: UIButtonType.system)
This will create a button that uses the standard tint color just like a button added to a storyboard in interface builder.

Switched from UILabel to UIButton. Text disappears

I just switched from a UILabel to UIButton, intending to use the button's titleLabel property to display the information formerly displayed in the label replaced by the button.
Problem is, the text doesn't show up. I've checked to see if the button itself (normally with a transparent background, changed to red to check) is appearing, and it is, but the text isn't there. Here's a sample of the relevant code, which is identical to the original (working) code from the label, changing only lines like:
UILabel *firstLabel;
(...)
firstLabel.textColor = [UIColor blackColor];
to:
UIButton *firstLabel;
(...)
firstLabel.titleLabel.textColor = [UIColor blackColor];
Here's a full chunk for clarity:
firstLabel.frame = CGRectMake(thisRiser.bounds.origin.x, thisRiser.frame.size.height -focusBarEndHeight - 55, barWidth, 60);
firstLabel.backgroundColor = [UIColor clearColor];
firstLabel.titleLabel.textColor = [UIColor blackColor];
firstLabel.titleLabel.text = [NSString stringWithFormat:#"%#\n%.2f%%\nTime--%#",focusItemName,focusItemPercent,actualDurationFocusItem];
[firstLabel.titleLabel setFont:[UIFont boldSystemFontOfSize:12]];
firstLabel.titleLabel.numberOfLines = 0;
firstLabel.titleLabel.textAlignment = NSTextAlignmentCenter;
[firstLabel setHidden:NO];
What am I missing? Any ideas?
Thanks!
UPDATE --
This may be related to a known bug!
Many thanks to the responders below. I implemented the first recommended fix, which resolved my initial problem, so my text now appears in the label. However, the UIControlStateHighlighted and UIControlStateSelected don't work.
For example, this code produces no discernible effect to the text when clicked:
firstLabel.backgroundColor = [UIColor clearColor];
[firstLabel setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
[firstLabel setTitleColor:[UIColor whiteColor] forState:UIControlStateSelected];
[firstLabel setTitleColor:[UIColor whiteColor] forState:UIControlStateHighlighted];
After searching around SO for a while, I'm coming to the conclusion that there is a bug (at least in iOS 7) that prevents proper functioning of these methods. Unfortunately, I'm not smart enough to provide more detailed information, but plenty of recent posts point to a major problem in this area of UIControl.
I'll be ecstatic to be proven wrong, because I'd like to use this functionality.
UIButton responds to different messages, you can use the button state to change its title.
You can set the title like this;
UIButton *button = [UIButton buttonWithType: UIButtonTypeCustom];
[button setTitle:#"Title goes here" forState:UIControlStateNormal];
See Control State: https://developer.apple.com/library/ios/documentation/UIKit/Reference/UIControl_Class/Reference/Reference.html#//apple_ref/c/tdef/UIControlState
To set a title in an UIButton, you have to init the button:
UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(10, 10, 60, 20)];
and then use:
[button setTitle:#"text" forState:UIControlStateNormal];
where you must specific the control state (highlighted, selected, etc.)
try for example,
UIButton *firstLabel = [UIButton buttonWithType:UIButtonTypeCustom];
firstLabel.frame = CGRectMake(0, 60, 100, 50); //set the frame
[firstLabel setTitle:#"Hello" forState:UIControlStateNormal];
[firstLabel setBackgroundColor:[UIColor redColor]];//for test
[firstLabel setTitleColor:[UIColor greenColor] forState:UIControlStateNormal]; //it will always displays green for normal
[firstLabel setTitleColor:[UIColor whiteColor] forState:UIControlStateSelected];//if u touch it text changed to white instantly
[firstLabel setTitleColor:[UIColor whiteColor] forState:UIControlStateHighlighted];//if touch it and hold it shows white color

Reduce button text size

I have a UIButton with setBackgroundImage: and used setTitle: for text. But the text size is too big. How to reduce the text size.
prevBtn = [UIButton buttonWithType:UIButtonTypeCustom];
[prevBtn setBackgroundImage:[UIImage imageNamed:#"previous_iphone.png"] forState:UIControlStateNormal];
[prevBtn addTarget:self
action:#selector(goPrev:)
forControlEvents:UIControlEventTouchDown];
[prevBtn setTitle:#"Previous" forState:UIControlStateNormal];
prevBtn.frame = CGRectMake(2, 5, 85, 30);
Try setting the font of the button, like this:
float size = 14.0; //assign size any value you wish for the font size.
prevBtn.titleLabel.font = [UIFont systemFontOfSize:size];
Change your code to this. It will automatically reduce the text size when it does not fit with minimum font size 10:
prevBtn = [UIButton buttonWithType:UIButtonTypeCustom];
[prevBtn setBackgroundImage:[UIImage imageNamed:#"previous_iphone.png"] forState:UIControlStateNormal];
[prevBtn addTarget:self
action:#selector(goPrev:)
forControlEvents:UIControlEventTouchDown];
[prevBtn setTitle:#"Previous" forState:UIControlStateNormal];
prevBtn.titleLabel.adjustsFontSizeToFitWidth = TRUE;
prevBtn.titleLabel.minimumFontSize = 10;
prevBtn.frame = CGRectMake(2, 5, 85, 30);
[prevButton.titleLabel setFont: [UIFont systemFontOfSize: 13.0]];
[prevBtn.titleLabel setFont:[UIFont systemFontOfSize:12]];

UIButton label not visible

I'm making a custom UIButton with 2 images, but for some reason the title is just not showing up, the button is showing fine, but without any title.
_button = [UIButton buttonWithType:UIButtonTypeCustom];
_button.frame = CGRectMake(10, 250, 300, 43);
[_button addTarget:self action:#selector(loginClicked:) forControlEvents:UIControlEventTouchUpInside];
[_button setImage:[UIImage imageWithContentsOfFile:normalButtonImg] forState:UIControlStateNormal];
[_button setImage:[UIImage imageWithContentsOfFile:hoverButtonImg] forState:UIControlStateSelected];
[_button setTitle:#"Login" forState:UIControlStateNormal];
[_button setTitle:#"Login" forState:UIControlStateHighlighted];
[_button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
[_button setTitleColor:[UIColor whiteColor] forState:UIControlStateHighlighted];
[self.view addSubview:_button];
These two lines:
[_button setImage:[UIImage imageWithContentsOfFile:normalButtonImg] forState:UIControlStateNormal];
[_button setImage:[UIImage imageWithContentsOfFile:hoverButtonImg] forState:UIControlStateSelected];
should be
[_button setBackgroundImage:[UIImage imageWithContentsOfFile:normalButtonImg] forState:UIControlStateNormal];
[_button setBackgroundImage:[UIImage imageWithContentsOfFile:hoverButtonImg] forState:UIControlStateSelected];
Explanation: if a foreground image is set for UIButton, the image is placed above the title, so it might hide it. If you want an image and a title to be displayed, you have to set a background image.
When you set the image, that overrides the title. If you can make your image a background image, that will work. Otherwise you will need to create a composite image for the button by essentially creating a new image which is your desired image with the title drawn over it.

Resources