换行模式和adjustsFontSizeToFitWidth不工作obj c

时间:2015-11-21 06:49:52

标签: ios objective-c

我使用多个按钮填充视图(按钮数量不同。按钮和按钮标题的大小不同)。我需要根据标题中的单词数设置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];

`

但这并不是我需要的。这有什么问题?

2 个答案:

答案 0 :(得分:0)

试,

//change scale factor accordingly

    [LABEL setMinimumScaleFactor:12.0/[UIFont labelFontSize]];

答案 1 :(得分:0)

通过计算文字中的字数来解决此问题,并相应地设置numberoflinesbutton1.titleLabel.numberOfLines=[self wordCount:[alertNameArray objectAtIndex:0]];