Line break mode and adjustsFontSizeToFitWidth not working obj c - ios

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

Related

I have used this code for group button but it display button in vertical view but i have display in horizontal

NSMutableArray* buttons = [NSMutableArray arrayWithCapacity:5];
CGRect btnRect = CGRectMake(25, 320, 100, 30);
for (NSString* optionTitle in #[#"Red", #"Green", #"Blue", #"Pink" ,#"Black"]){
RadioButton* btn = [[RadioButton alloc] initWithFrame:btnRect];
[btn addTarget:self action:#selector(onRadioButtonValueChanged:) forControlEvents:UIControlEventValueChanged];
btnRect.origin.y += 40;
[btn setTitle:optionTitle forState:UIControlStateNormal];
[btn setTitleColor:[UIColor darkGrayColor] forState:UIControlStateNormal];
btn.titleLabel.font = [UIFont boldSystemFontOfSize:17];
[btn setImage:[UIImage imageNamed:#"unchecked.png"] forState:UIControlStateNormal];
[btn setImage:[UIImage imageNamed:#"checked.png"] forState:UIControlStateSelected];
btn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
btn.titleEdgeInsets = UIEdgeInsetsMake(0, 6, 0, 0);
[self.view addSubview:btn];
[buttons addObject:btn];
}
Change the btnRect value accordingly :
btnRect.origin.y += 40;
Should be changed as :
btnRect.origin.x = (btnRect.size.width + btnRect.origin.x) + 5.0;
// 5.0 value can be changed to what ever you want according to the distance required between two buttons

How to make the UIButton title label font same in size regardless of the text width

I am generating few buttons that should look like a tabbar. This is what I have done.
scrolTab=[[UIScrollView alloc] initWithFrame:CGRectMake(0, h-48, w, 48)];
[scrolTab setContentInset:UIEdgeInsetsMake(0.0, 0.0, 0.0, 180.0)];
[scrolTab setShowsHorizontalScrollIndicator:NO];
[self.view addSubview:scrolTab];
for (int i=1; i<6; i++) {
[scrolTab addSubview:[self GetTabBarItems:i :5]];
}
Then my GetTabBarItems method is like this.
-(UIButton *)GetTabBarItems :(NSInteger)index:(NSInteger)count
{
UIButton *item=[[UIButton alloc] initWithFrame:CGRectMake(tabbuttonX, 0, w/count,49)];
[item.titleLabel setFont:[UIFont fontWithName:#"HelveticaNeue-Light" size:10.0]];
item.titleLabel.numberOfLines = 1;
item.titleLabel.adjustsFontSizeToFitWidth = YES;
item.titleLabel.lineBreakMode = NSLineBreakByClipping;
CGSize imageSize=item.imageView.image.size;
CGFloat space=6.0;
if (index==1) {
[item setTitle:#"Menu" forState:UIControlStateNormal];
[item setImage:[UIImage imageNamed:#"MenuBtn"] forState:UIControlStateNormal];
}
else if (index==2) {
[item setTitle:#"My Profile" forState:UIControlStateNormal];
[item setImage:[UIImage imageNamed:#"ProfTab"] forState:UIControlStateNormal];
}
else if (index==3) {
[item setTitle:#"Notification" forState:UIControlStateNormal];
[item setImage:[UIImage imageNamed:#"Notification"] forState:UIControlStateNormal];
}
else if (index==4)
{
[item setTitle:#"Things-Todo" forState:UIControlStateNormal];
[item setImage:[UIImage imageNamed:#"ThingsTodo"] forState:UIControlStateNormal];
}
else if (index==5)
{
[item setTitle:#"Search" forState:UIControlStateNormal];
[item setImage:[UIImage imageNamed:#"Search"] forState:UIControlStateNormal];
}
[item setTitleEdgeInsets:UIEdgeInsetsMake(-(imageSize.height+(-20)), -10.0, -(imageSize.height+space), 10.0)];
CGSize titleSize=[item.titleLabel.text sizeWithAttributes:#{NSFontAttributeName: item.titleLabel.font}];
CGRect txtLabelframe=item.titleLabel.frame;
item.imageEdgeInsets = UIEdgeInsetsMake(0.0, -(txtLabelframe.size.width+txtLabelframe.origin.x)/2, 10.0, - titleSize.width);
tabbuttonX=tabbuttonX+item.frame.size.width;
return item;
}
I want to make my font is in same size, But if I make those fonts in same size its truncate from the middle and middle of the text shows as dots. How can I make those fonts looks like in same size and show the full text in the button.
Please help me. This is my output for this code.
Thanks
Don't add button title, you have to add another label as subview to the button
UILabel *label2 = [[UILabel alloc]initWithFrame:CGRectMake(-10, button.frame.size.height-20, 100, 30)];//adjust the frame as you required
[label2 setTextColor:[UIColor redColor]];
label2.text = #"lengthy string is here";
[label2 setFont:[UIFont fontWithName:button.titleLabel.font.fontName size:button.titleLabel.font.pointSize]];
[button addSubview:label2];
If the label touches the near button, Use this code
UILabel *label2 = [[UILabel alloc]initWithFrame:CGRectMake(-10, button.frame.size.height-20, 150, 60)]; //adjust the frame as your string length
[label2 setTextColor:[UIColor redColor]];
NSMutableAttributedString *muAtrStr = [[NSMutableAttributedString alloc]initWithString:#"lengthy string\nshould be here"];
label2.numberOfLines = 0;
[label2 setAttributedText:muAtrStr];
[label2 setFont:[UIFont fontWithName:button.titleLabel.font.fontName size:button.titleLabel.font.pointSize]];
[button addSubview:label2];
First try to remove this line
item.titleLabel.adjustsFontSizeToFitWidth = YES;
I guess that the font will now remain equal size but gets clipped ?
Try to use the iOS Simulator debug option "Color Blended Layer" or the Xcode 6 View Debugging Option and see if the label is big enough.
Ah and methods usually starts with a lower case letter ;)

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.

UIScrollView hiding UINavigationBar

I have an a UIControlView with the following hierarchy.
...
UIView
UIScrollview
textfield
button
textfield
button
NavigationBar
...
When this view loads the scrollview pushes the navigationBar off the screen or hides it? What am I doing wrong?
When I drag a srollview onto the UIViewController storyboard, should the scrollview be at 480 or around 430 so its below the navigationbar?
// === HERE IS THE CODE (there is no other custom code besides this) ======
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
DLog(#"%f", self.scrollView.contentOffset.y);
//self.scrollView.contentOffset = CGPointMake(self.view.contentOffset.x,0);
self.codeTextField.delegate = self;
self.mobileTextField.delegate = self;
[self.navBarLabel setTitleColor:[UIColor statDarkGray] forState:UIControlStateNormal];
[self.navBarLabel setTitleColor:[UIColor statDarkGray] forState:UIControlStateSelected];
[self.navBarLabel setTitleColor:[UIColor statLightGray] forState:UIControlStateHighlighted];
UIFont *font = [UIFont fontWithName:#"GothamRounded-Light" size:17];
[self.navBarLabel.titleLabel setFont:font];
//SETUP TEXTFIELDS
// --- Setup textfield designs ---------
self.codeTextField.borderStyle = UITextBorderStyleNone;
UIImage *fieldBGImage = [[UIImage imageNamed:#"code_textfield.png"] stretchableImageWithLeftCapWidth:20 topCapHeight:20];
[self.codeTextField setBackground:fieldBGImage];
// for left padding
self.codeTextField.leftView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 88, 20)];
self.codeTextField.leftViewMode = UITextFieldViewModeAlways;
self.mobileTextField.leftView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 88, 20)];
self.mobileTextField.leftViewMode = UITextFieldViewModeAlways;
// setup submit button
self.submitButton.titleLabel.text = #"Submit";
self.submitButton.titleLabel.font = [UIFont fontWithName:#"GothamRounded-Light" size:18];
self.submitButton.enabled = NO;
[self.submitButton setBackgroundImage:[UIImage imageNamed:#"bluebutton_disabled.png"] forState:UIControlStateDisabled];
[self.submitButton setBackgroundImage:[UIImage imageNamed:#"bluebutton_normal.png"] forState:UIControlStateNormal];
[self.submitButton setBackgroundImage:[UIImage imageNamed:#"bluebutton_selected.png"] forState:UIControlStateSelected];
// setup submit button
self.resendCodeButton.enabled = NO;
self.resendCodeButton.titleLabel.font = [UIFont fontWithName:#"GothamRounded-Light" size:18];
[self.resendCodeButton setBackgroundImage:[UIImage imageNamed:#"redbutton_disabled.png"] forState:UIControlStateDisabled];
[self.resendCodeButton setBackgroundImage:[UIImage imageNamed:#"redbutton_normal.png"] forState:UIControlStateNormal];
[self.resendCodeButton setBackgroundImage:[UIImage imageNamed:#"redbutton_selected.png"] forState:UIControlStateSelected];
// SETUP BUTTONS
// The next arrow button
UIImage *background = [UIImage imageNamed:#"Cancel_normal.png"];
UIImage *backgroundSelected = [UIImage imageNamed:#"Cancel_selected.png"];
self.cancelButton = [UIButton buttonWithType:UIButtonTypeCustom];
[self.cancelButton addTarget:self action:#selector(cancelPressed:) forControlEvents:UIControlEventTouchUpInside]; //adding action
[self.cancelButton setBackgroundImage:background forState:UIControlStateNormal];
[self.cancelButton setBackgroundImage:backgroundSelected forState:UIControlStateSelected];
self.cancelButton.frame = CGRectMake(0 ,0,background.size.width, background.size.height);
self.cancelButtonItem = [[UIBarButtonItem alloc] initWithCustomView:self.cancelButton];
self.navigationItem.leftBarButtonItem = self.cancelButtonItem;
}
The view was shown modally not pushed. And even though I designed a navbar and set one in the settings (translucent bar). It wasn't showing one?
So on the segue I was using show modally and the, no matter if it had a UIView or UIScrollView, never would show the navbar. By switching to a PUSH segue, it now shows the navbar. Fixed.
I just decided to push the view instead of modal to be done with it.

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

Resources