Reduce button text size - ios

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]];

Related

Image and Title to UIbutton is not being set

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];

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]];

Adding an Image to my UIButton subview

I have a UIButton that is added on my super view as a subview, It is just a big block, how do I add an image to my button.
- (void)viewDidLoad
{
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
button.frame = CGRectMake(890, 345 , 100, 100);
button.backgroundColor = [UIColor purpleColor];
[self.collectionView.superview addSubview:button];
[button addTarget:self action:#selector(changeContentOffset:) forControlEvents:UIControlEventTouchUpInside];
}
you can add image in UIButton by following two ways.
1) With help of setting Background Image.
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
button.frame = CGRectMake(890, 345 , 100, 100);
button.backgroundColor = [UIColor purpleColor];
[button setBackgroundImage:[UIImage imageNamed:#""] forState:UIControlStateNormal];
[self.collectionView.superview addSubview:button];
[button addTarget:self action:#selector(changeContentOffset:) forControlEvents:UIControlEventTouchUpInside];
2) Or you can add UIImageView as subview.
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
button.frame = CGRectMake(890, 345 , 100, 100);
button.backgroundColor = [UIColor purpleColor];
[button setClipsToBounds:YES];
[button setBackgroundImage:[UIImage imageNamed:#""] forState:UIControlStateNormal];
UIImageView * imgView = [[UIImageView alloc]init];
[imgView setImage:[UIImage imageNamed:#""]];
[imgView setFrame:CGRectMake(0, 0, 100, 100)];
[button addSubview:imgView];
[self.collectionView.superview addSubview:button];
[button addTarget:self action:#selector(changeContentOffset:) forControlEvents:UIControlEventTouchUpInside];
Use this:
-setBackgroundImage:forState:
you may looking for something like this, if you want to have an "image as button" and not only a default button with an image inside
BOOL ipad = UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad;
UIButton* btn = [UIButton buttonWithType:UIButtonTypeCustom];;
CGRect frame;
//universal app
if (ipad)
{
btn.frame = frame = CGRectMake( x, y, w, h );
}
else
{
btn.frame = frame = CGRectMake( x, y, w, h );
}
btn.titleLabel.backgroundColor = [UIColor clearColor];
forState:UIControlStateNormal];
[btn setBackgroundImage:[UIImage imageNamed:#"buttonAsImage.png"] forState:UIControlStateNormal && UIControlStateHighlighted];
[btn setTitle:title forState:UIControlStateNormal];
There are different ways to do it.
First one is you can add an imageview to your superview and set imageview's image whatever you want. Then, at the same position add your button with size of imageview and set button color to clearColor. But you need to add imageView first otherwise your button will be under the image.
Second one is default image to button, use setImage:image forState:UIControlStateNormal or setBackgroundImage:image forState:UIControlStateNormal.
Got it, here is how:
[button setBackgroundImage:[UIImage imageNamed:#"image.png"] forState:UIControlStateNormal && UIControlStateHighlighted];
Use this method.
- (void)setBackgroundImage:(UIImage *)image forState:(UIControlState)state

UIButton text of label not visible

Problem: If the user goes on the iPhone/iPad to "Settings - General - Accessibility - Increase Contrast" and switches on "Darken Colors" the text from my buttons disappears.
Here is my code for creating the buttons:
buttonCurrent = [UIButton buttonWithType:UIButtonTypeSystem];
buttonCurrent.translatesAutoresizingMaskIntoConstraints = NO;
buttonCurrent.titleLabel.font = [UIFont preferredFontForTextStyle:UIFontTextStyleBody];
buttonCurrent.titleLabel.textAlignment = NSTextAlignmentCenter;
buttonCurrent.titleLabel.lineBreakMode = NSLineBreakByWordWrapping;
buttonCurrent.titleLabel.numberOfLines = 2;
buttonCurrent.titleEdgeInsets = UIEdgeInsetsMake(0, 5, 0, 5);
buttonCurrent.backgroundColor = [UIColor whiteColor];
[buttonCurrent setTitle:NSLocalizedString(#"Add the reading with the current date", nil) forState:UIControlStateNormal];
[buttonCurrent addTarget:self action:#selector(buttonPressed:) forControlEvents:UIControlEventTouchUpInside];
Thanks for the help.
I changed the code to:
buttonCurrent = [UIButton buttonWithType:UIButtonTypeCustom];
buttonCurrent.translatesAutoresizingMaskIntoConstraints = NO;
buttonCurrent.titleLabel.font = [UIFont preferredFontForTextStyle:UIFontTextStyleBody];
[buttonCurrent setTitleColor:self.view.tintColor forState:UIControlStateNormal];
[buttonCurrent setTitleColor:[UIColor lightGrayColor] forState:UIControlStateHighlighted];
[buttonCurrent setTitleColor:[UIColor lightGrayColor] forState:UIControlStateDisabled];
buttonCurrent.titleLabel.textAlignment = NSTextAlignmentCenter;
buttonCurrent.titleLabel.lineBreakMode = NSLineBreakByWordWrapping;
buttonCurrent.titleLabel.numberOfLines = 2;
buttonCurrent.titleEdgeInsets = UIEdgeInsetsMake(0, 5, 0, 5);
buttonCurrent.backgroundColor = [UIColor grayColor];
[buttonCurrent setTitle:NSLocalizedString(#"Add the reading with the current date", nil) forState:UIControlStateNormal];
It looks now like the UIButtonTypeSystem and it works also with system setting "Darken Colors".
all code is fine except first line.
buttonCurrent = [UIButton buttonWithType:UIButtonTypeCustom];
UIButtonTypeSystem have default blue color as a back ground in greater than iOS7. You should always use UIButtonTypeCustom, so that it does not shows the effect as you are experience.
And also please give frame. I checked your code and you have not given frame for button. Please do that first.
UIButton *buttonCurrent = [UIButton buttonWithType:UIButtonTypeCustom];
buttonCurrent.translatesAutoresizingMaskIntoConstraints = NO;
buttonCurrent.titleLabel.font = [UIFont preferredFontForTextStyle:UIFontTextStyleBody];
buttonCurrent.titleLabel.textAlignment = NSTextAlignmentCenter;
buttonCurrent.titleLabel.lineBreakMode = NSLineBreakByWordWrapping;
buttonCurrent.titleLabel.numberOfLines = 2;
buttonCurrent.titleEdgeInsets = UIEdgeInsetsMake(0, 5, 0, 5);
buttonCurrent.backgroundColor = [UIColor whiteColor];
buttonCurrent.frame = CGRectMake(0, 50, 320, 80);
[buttonCurrent setTitleColor:[UIColor colorWithRed:0.0 green:0.5 blue:1.0 alpha:1.0] forState:UIControlStateNormal];
[buttonCurrent setTitle:#"Add the reading with the current date" forState:UIControlStateNormal];
[buttonCurrent addTarget:self action:#selector(back:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:buttonCurrent];
All code is fine except first line
buttonCurrent = [UIButton buttonWithType:UIButtonTypeCustom];
UIButtonTypeSystem have default blue color as a back ground in greater than iOS7. You should always use UIButtonTypeCustom, so that it does not shows the effect as you are experience.

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);

Resources