我如何让文字适合UIButton? [英] how do i let text fit to UIButton?

查看:106
本文介绍了我如何让文字适合UIButton?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

按钮大小已足够,但是当我更改标题时,标题文字无法适应按钮宽度。任何SDK功能都可以解决这个问题,或者我需要手动编码来解决它?

Button size is enough, however when I change title, the title text cannot fit the button width. Any SDK function can solve this problem, or I need to manually code to solve it?

请参考下面的图片。

在nib文件中设计。

design in the nib file.

initial在模拟器中显示

initial show in simulator

当我更改标题文本时

when I change the title text


  1. _button.titleLabel.adjustsFontSizeToFitWidth = YES;

    方式会改变我的字体大小。我不能接受这种方式。

  1. _button.titleLabel.adjustsFontSizeToFitWidth = YES;
    the way will change my font size. I cannot accept the way.

[_ button setTitleEdgeInsets:UIEdgeInsetsMake(10.0,10.0,0.0,0.0)];

只改变标签的位置,而不是标签尺寸。

[_button setTitleEdgeInsets:UIEdgeInsetsMake(10.0, 10.0, 0.0,0.0)];
the way change label's position only, not label size.

[_ button.titleLabel sizeToFit];

结果与图片(3)相同。

[_button.titleLabel sizeToFit];
result is same with picture(3).

[_ button sizeToFit];

title移到左上角,标题仍然是相同的结果。

[_button sizeToFit];
title moved to upper left corner, and the title still the same result.

Just困惑,我的按钮大小足够大,为什么标题大小如此之小?

Just confused, my button size is big enough, why title size is so small?

推荐答案

使用它。

Objective-c

Objective-c

button.titleLabel.numberOfLines = 1;
button.titleLabel.adjustsFontSizeToFitWidth = YES;
button.titleLabel.lineBreakMode = NSLineBreakByClipping; 

Swift 2.0

Swift 2.0

button.titleLabel?.numberOfLines = 0
button.titleLabel?.adjustsFontSizeToFitWidth = true
button.titleLabel?.lineBreakMode = NSLineBreakMode.ByWordWrapping

注意:Swift代码礼貌:@Rachel Harvey

NOTE: Swift code courtesy: @Rachel Harvey

这篇关于我如何让文字适合UIButton?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆