简体   繁体   中英

How to set a “bold” dynamic font type for UILabel?

The Apple documentation says that for iOS an app can use text styles and dynamic font type so text size automatically adjusts to the users reading preferences.

I have a text style Body and set Dynamic Type to true so that it automatically adjust the text size. This works good.

This text I would also like to make appear bold . I haven't found a way yet how to achieve this either by code or in the Interface Builder.

Is there a way to achieve a "bold dynamic font type"?

Ok, it seems the documentation provides a hint. For using custom fonts, I consider a bold font as a custom font now, one can use the UIFontMetrics class.

If I pass a "bold custom font" to the scaledFontForFont method of UIFontMetrics class I get the desired result with the dynamic font type UIFontTextStyleBody .

UIFont* boldSystemFont = [UIFont boldSystemFontOfSize:17];
myLabel.font           = [[UIFontMetrics metricsForTextStyle:UIFontTextStyleBody] scaledFontForFont:boldSystemFont];

So far I only see this way using code. No idea if this could somehow be setup in the Interface Builder as well.

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