UIButton title clipped - ios

I have a custom button with the font.
customButtonSynchronize = [UIButton buttonWithType:UIButtonTypeCustom];
customButtonSynchronize.frame=CGRectMake(578, 27.5, 91, 29) ;
[customButtonSynchronize setBackgroundImage:[UIImage imageNamed:#"synchronize.png"]
forState:UIControlStateNormal];
customButtonSynchronize.titleLabel.font = [UIFont fontWithName:#"HelveticaLTStd-Roman" size:14.0f];
[customButtonSynchronize setTitle:#"Sincronizzare" forState:UIControlStateNormal];
/////HERE//////////
customButtonSynchronize.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;
customButtonSynchronize.contentVerticalAlignment = UIControlContentVerticalAlignmentTop;
customButtonSynchronize.contentEdgeInsets = UIEdgeInsetsMake(8, 0, 0, 0);
customButtonSynchronize.backgroundColor=[UIColor clearColor];
[customButtonSynchronize addTarget:self action:#selector(synchronizeDB:) forControlEvents:UIControlEventTouchUpInside];
But the text above seems to be "Cut off" a little.See the top of "S".
I dont want to change frame of button,size of the font.Changing the inset DOESNOT avoid the problem. Is it a simulator bug or problem with helvetica font? Any work around?

set uibutton title to nil then create label with text and add subview to uibutton thats all, now it will display text correctly as you required

Related

UIButton text and image

I have one UIButton. In that UIButton i have to show text and image. Image should be displayed after the text. Based on text i want to adjust the button frame.
See the image. After the text "I am a Button", i want to add a image like arrow.
UIButton *titleButton = [UIButton buttonWithType:UIButtonTypeCustom];
NSString *name = #"I am a button";
titleButton.titleLabel.text = [NSString stringWithFormat:#"%#", name];
titleButton.titleLabel.textColor = [UIColor whiteColor];
CGSize stringSize = [name sizeWithAttributes:#{NSFontAttributeName: [UIFont fontWithName:#"MyriadPro-Cond" size:24]}];
CGRect frame = CGRectMake(300, 30, stringSize.width, 30);
//frame.size.width = stringSize.width;
[titleButton setFrame:frame];
UIImageView *image = [[UIImageView alloc]initWithImage:[UIImage imageNamed:#"home.png"]];
image.frame = CGRectMake(stringSize.width, 14, 20, 13);
titleButton.imageEdgeInsets = UIEdgeInsetsMake(0, 0, stringSize.width+20, 0);
[titleButton addSubview:image];
I written code like this. But it is not working. Anyone can make it correct. Thanks in advance
You dont have to add a UIImageView over the UIButton to get this behaviour. The UIButton has itself has some properties. Set the orange image as the background image of the button, set the arrow image as the image of the button. Then adjust the insets of the title and the image of the button to move the title to the left and the arrow image to the right.
[btn setBackgroundImage:[UIImage imageNamed:#"orange_bg"] forState:UIControlStateNormal];
[btn setImage:[UIImage imageNamed:#"arrow"] forState:UIControlStateNormal];
[btn setTitleEdgeInsets:UIEdgeInsetsMake(0, 0, -60, 0)];
[btn setImageEdgeInsets:UIEdgeInsetsMake(0, 0, 125, 0)];
You have to adjust the insets according to your image and button sizes.

Center a UIButton title in its frame

I want to center (both x and y axis) this + sign within my UIButton CGRect but it keeps getting pushed down the y-axis.
UIButton *aButton = [UIButton buttonWithType:UIButtonTypeSystem];
[aButton setTitle:#"+" forState:UIControlStateNormal];
[aButton.titleLabel setFont:[UIFont fontWithName:#"HelveticaNeue-Bold" size:(40.0)]];
[aButton setBackgroundColor:[UIColor grayColor]];
[aButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
[aButton setTitleColor:[UIColor blueColor] forState:UIControlStateHighlighted];
aButton.layer.cornerRadius = 25.0;
aButton.frame = CGRectMake(75.0, 100.0, 50.0, 50.0);
This would look more correct to you if you had ascenders and descenders. There's a label inside the button and it is sized to accommodate the height of the full range of possible characters of this font. If you don't like the position of the label, you're free to move it. There are a lot of ways to do this; you might try playing with the button's titleEdgeInsets, for example.
Add this line after setting the title and font:
aButton.titleEdgeInsets = UIEdgeInsetsMake(0, 0, 9, 0);

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

Add text to a custom Back Button iOS

I have a backbutton with an image I'm using in my navigation controller, and I'd like to set the text over top of the image. I tried this but it doesn't seem to show up:
UIImage *normalBackImage = [UIImage imageNamed:#"button_tl.png"];
UIButton *backButton = [UIButton buttonWithType:UIButtonTypeCustom]; backButton.bounds = CGRectMake(0, 0, normalBackImage.size.width, normalBackImage.size.height );
[backButton setImage:normalBackImage forState:UIControlStateNormal];
backButton.titleLabel.text = #"Back";
Does anyone know how to accomplish this? Thanks!
set the title's color, else you'll not see anything :)
setTitleColor:forState:
2.
dont try to set the image (which means FOREGROUND image). Instead set the BACKGROUND image :)
– setBackgroundImage:forState:
sample:
UIButton *backButton = [UIButton buttonWithType:UIButtonTypeCustom];
backButton.frame = CGRectMake(10, 10, 100, 50);
[backButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
[backButton setTitle:#"Bla" forState:UIControlStateNormal];
UIImage *normalBackImage = [UIImage imageNamed:#"button_tl.png"];
[backButton setBackgroundImage:normalBackImage forState:UIControlStateNormal];
[self.view addSubview:backButton];
You could create a UILabel and add it as a subview to your UIImage. That would look something like this (writing code outside the compiler, might not be perfect)
UILabel *myLabel = [[UILabel alloc] init];
[myLabel setText:#"My Label Text"];
[image addSubview:myLabel];

UIButton Image + Text IOS

I need a UIButton with image & text. Image should be in the top & text comes under the image both should be clickable.
I see very complicated answers, all of them using code. However, if you are using Interface Builder, there is a very easy way to do this:
Select the button and set a title and an image. Note that if you set the background instead of the image then the image will be resized if it is smaller than the button.
Set the position of both items by changing the edge and insets. You could even control the alignment of both in the Control section.
You could even use the same approach by code, without creating UILabels and UIImages inside as other solutions proposed. Always Keep It Simple!
EDIT: Attached a small example having the 3 things set (title, image and background) with correct insets
I think you are looking for this solution for your problem:
UIButton *_button = [UIButton buttonWithType:UIButtonTypeCustom];
[_button setFrame:CGRectMake(0.f, 0.f, 128.f, 128.f)]; // SET the values for your wishes
[_button setCenter:CGPointMake(128.f, 128.f)]; // SET the values for your wishes
[_button setClipsToBounds:false];
[_button setBackgroundImage:[UIImage imageNamed:#"jquery-mobile-icon.png"] forState:UIControlStateNormal]; // SET the image name for your wishes
[_button setTitle:#"Button" forState:UIControlStateNormal];
[_button.titleLabel setFont:[UIFont systemFontOfSize:24.f]];
[_button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal]; // SET the colour for your wishes
[_button setTitleColor:[UIColor redColor] forState:UIControlStateHighlighted]; // SET the colour for your wishes
[_button setTitleEdgeInsets:UIEdgeInsetsMake(0.f, 0.f, -110.f, 0.f)]; // SET the values for your wishes
[_button addTarget:self action:#selector(buttonTouchedUpInside:) forControlEvents:UIControlEventTouchUpInside]; // you can ADD the action to the button as well like
...the rest of the customisation of the button is your duty now, and don't forget to add the button to your view.
UPDATE #1 and UPDATE #2
or, if you don't need a dynamic button you could add your button to your view in the Interface Builder and you could set the same values at there as well. it is pretty same, but here is this version as well in one simple picture.
you can also see the final result in the Interface Builder as it is on the screenshot.
Xcode-9 and Xcode-10 Apple done few changes regarding Edge Inset now, you can change it under size-inspector.
Please follow below steps:
Step-1:
Input text and select image which you want to show:
Step-2:
Select button control as per your requirement as shown in below image:
Step-3:
Now go-to size inspector and add value as per your requirement:
swift version:
var button = UIButton()
newGameButton.setTitle("Новая игра", for: .normal)
newGameButton.setImage(UIImage(named: "energi"), for: .normal)
newGameButton.backgroundColor = .blue
newGameButton.imageEdgeInsets.left = -50
In my case, I wanted to add UIImage to the right and UILabel to the left. Maybe I can achieve that by writing code (like the above mentioned), but I prefer not to write code and get it done by using the storyboard as much as possible. So this is how did it:
First, write down something in your label box and select an image that you want to show:
And that will create a button looking like this:
Next, look for Semantic and select Force Right-to-Left (If you don't specify anything, then it will show the image to the left and label to the right like the above image):
Finally, you'll see UIImage to the right and UILabel to the left:
To add space between a label and an image, go to the Size inspector and change those values depending on your requirement:
That's it!
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
button.imageView.image = [UIImage imageNamed:#"your image name here"];
button.titleLabel.text = #"your text here";
but following code will show label above and image in background
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
button.background.image = [UIImage imageNamed:#"your image name here"];
button.titleLabel.text = #"your text here";
There is no need to use label and button in same control because UIButton has UILabel and UIimageview properties.
Use this code:
UIButton *sampleButton = [UIButton buttonWithType:UIButtonTypeCustom];
[sampleButton setFrame:CGRectMake(0, 10, 200, 52)];
[sampleButton setTitle:#"Button Title" forState:UIControlStateNormal];
[sampleButton setFont:[UIFont boldSystemFontOfSize:20]];
[sampleButton setBackgroundImage:[[UIImage imageNamed:#"redButton.png"]
stretchableImageWithLeftCapWidth:10.0 topCapHeight:0.0] forState:UIControlStateNormal];
[sampleButton addTarget:self action:#selector(buttonPressed)
forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:sampleButton]
You should create custom imageview for image and custom label for text and you add to your button as subviews. That's it.
UIButton *yourButton = [UIButton buttonWithType:UIButtonTypeCustom];
yourButton.backgroundColor = [UIColor greenColor];
yourButton.frame = CGRectMake(140, 40, 175, 30);
[yourButton addTarget:self action:#selector(yourButtonSelected:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:yourButton];
UIImageView *imageView1 = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, yourButton.frame.size.width, yourButton.frame.size.height/2)];
imageView1.image =[UIImage imageNamed:#"images.jpg"];
[yourButton addSubview:imageView1];
UILabel *label=[[UILabel alloc] initWithFrame:CGRectMake(0, yourButton.frame.size.height/2, yourButton.frame.size.width, yourButton.frame.size.height/2)];
label.backgroundColor = [UIColor greenColor];
label.textAlignment= UITextAlignmentCenter;
label.text = #"ButtonTitle";
[yourButton addSubview:label];
For testing purpose, use yourButtonSelected: method
-(void)yourButtonSelected:(id)sender{
NSLog(#"Your Button Selected");
}
I think it will be helpful to you.
Use this code:
UIButton *button=[UIButton buttonWithType:UIButtonTypeRoundedRect];
button.imageView.frame=CGRectMake(0.0f, 0.0f, 50.0f, 44.0f);///You can replace it with your own dimensions.
UILabel *label=[[UILabel alloc] initWithFrame:CGRectMake(0.0f, 35.0f, 50.0f, 44.0f)];///You can replace it with your own dimensions.
[button addSubview:label];
I encountered the same problem, and I fix it by creating a new subclass of UIButton and overriding the layoutSubviews: method as below :
-(void)layoutSubviews {
[super layoutSubviews];
// Center image
CGPoint center = self.imageView.center;
center.x = self.frame.size.width/2;
center.y = self.imageView.frame.size.height/2;
self.imageView.center = center;
//Center text
CGRect newFrame = [self titleLabel].frame;
newFrame.origin.x = 0;
newFrame.origin.y = self.imageView.frame.size.height + 5;
newFrame.size.width = self.frame.size.width;
self.titleLabel.frame = newFrame;
self.titleLabel.textAlignment = UITextAlignmentCenter;
}
I think that the Angel García Olloqui's answer is another good solution, if you place all of them manually with interface builder but I'll keep my solution since I don't have to modify the content insets for each of my button.
Make UIImageView and UILabel, and set image and text to both of this....then Place a custom button over imageView and Label....
UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"search.png"]];
imageView.frame = CGRectMake(x, y, imageView.frame.size.width, imageView.frame.size.height);
[self.view addSubview:imageView];
UILabel *yourLabel = [[UILabel alloc] initWithFrame:CGRectMake(x, y,a,b)];
yourLabel.text = #"raj";
[self.view addSubview:yourLabel];
UIButton * yourBtn=[UIButton buttonWithType:UIButtonTypeCustom];
[yourBtn setFrame:CGRectMake(x, y,c,d)];
[yourBtn addTarget:self action:#selector(#"Your Action") forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:yourBtn];
It's really simple,just add image to background of you button and give text to titlelabel of button for uicontrolstatenormal.
That's it.
[btn setBackgroundImage:[UIImage imageNamed:#"img.png"] forState:UIControlStateNormal];
[btn setContentVerticalAlignment:UIControlContentVerticalAlignmentBottom];
[btn setTitle:#"Click Me" forState:UIControlStateNormal];

Resources