简体   繁体   中英

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM