我正在用多个按钮填充视图(按钮的数量是不同的)。按钮和按钮标题的大小不同)。我需要设置按钮标题在2行或1行根据字数在标题。此外,字体大小应与按钮框大小兼容。包括我这里的一些代码。`
// --------------------------
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];`
但这不是我所需要的。这是怎么回事?
发布于 2015-11-24 06:50:41
通过计算文本中的单词数来解决这一问题,并相应地设置numberoflines。button1.titleLabel.numberOfLines=[self wordCount:[alertNameArray objectAtIndex:0]];。
发布于 2015-11-21 10:13:54
试试看
//change scale factor accordingly
[LABEL setMinimumScaleFactor:12.0/[UIFont labelFontSize]];https://stackoverflow.com/questions/33840577
复制相似问题