Help us understand the problem. What is going on with this article?

UILabel 複数行のフォントサイズ自動調整

More than 3 years have passed since last update.

1行の場合に限り以下のコードでフォントサイズが収縮されます。

label.minimumScaleFactor = 0.3
label.adjustsFontSizeToFitWidth = true
label.numberOfLines = 1

複数行だったらどうでしょう:question:

demo

アニメーションGIFのように文字が長くなるに連れてフォントサイズが小さくなり、フレームにフィットしているのがわかるかと思います。

後々使うかもしれないのでUILabelの拡張(extension)もしくはカテゴリ(category)として書いておきました。Swift版、Objective-C版あり。iOS 7.0以降対応。

:octocat: ShingoFukuyama / UILabel-FontSizeToFit

使い方

Swift

label.fontSizeToFit()

Objective-C

上記2つをプロジェクトに入れます。

ヘッダーファイルを読み込みます。
#import "UILabel+FontSizeToFit.h"

UILabelのframeやfont設定後に以下を実行します。

[label fontSizeToFit];
ShingoFukuyama
[GitHub: https://goo.gl/gDtSXW] [Twitter: https://goo.gl/B6S8Zc]
Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Comments
No comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
ユーザーは見つかりませんでした