我生成的几个按钮应该看起来像一个tabbar。这就是我所做的。
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]];
}
然后我的GetTabBarItems方法是这样的。
-(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;
}
我想让我的字体大小相同,但如果我将这些字体设置为相同的大小,则从文本的中间和中间截取的字体显示为点。如何使这些字体看起来像相同的大小,并在按钮中显示全文。
请帮帮我。这是此代码的输出。
由于
答案 0 :(得分:1)
不要添加按钮标题,您必须将另一个标签作为子视图添加到按钮
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];
如果标签触及near按钮,请使用此代码
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];
答案 1 :(得分:0)
首先尝试删除此行
item.titleLabel.adjustsFontSizeToFitWidth = YES;
我猜这个字体现在会保持相同的大小,但会被修剪?
尝试使用iOS模拟器调试选项“颜色混合图层”或Xcode 6查看调试选项,看看标签是否足够大。
啊,方法通常以小写字母开头;)