首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >断线模式和adjustsFontSizeToFitWidth不工作的obj c

断线模式和adjustsFontSizeToFitWidth不工作的obj c
EN

Stack Overflow用户
提问于 2015-11-21 06:49:52
回答 2查看 280关注 0票数 0

我正在用多个按钮填充视图(按钮的数量是不同的)。按钮和按钮标题的大小不同)。我需要设置按钮标题在2行或1行根据字数在标题。此外,字体大小应与按钮框大小兼容。包括我这里的一些代码。`

代码语言:javascript
复制
     // --------------------------
    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];

`

但这不是我所需要的。这是怎么回事?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2015-11-24 06:50:41

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

票数 0
EN

Stack Overflow用户

发布于 2015-11-21 10:13:54

试试看

代码语言:javascript
复制
//change scale factor accordingly

    [LABEL setMinimumScaleFactor:12.0/[UIFont labelFontSize]];
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/33840577

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档