简体   繁体   中英

Adjust font size to uilabel width

I got a label in my app it´s constrained to margins |[myLabel]|. I want to ,whatever the device is(iPhone4, iPhone5, iPhone6 and iPhone6Plus), have 15 characters and adjust font size to fit in.

IPhone6Plus:

iPhone6Plus

IPhone6:

iPhone 6

I want in iPhone6Plus the font bigger to fit as in iPhone6, and in iPhone4 and 5 to be smaller. Is this possible?

label.numberOfLines = 1
label.adjustsFontSizeToFitWidth = true

In the attributes inspector of your label :

在此处输入图片说明

An alternative approach (if nothing else works) would be to start with assigning the text to a ui label at run time, using the max font size you use on the largest target device. Then (pseudo code):

Obtain temp label intrinsic length

If intrinsic length > display label.width then reduce size by 1.

Repeat until intrinsic length <= display label.width

Hope it helps!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM