Swift - Change the font size inside the label

I have a label where there is text that should be bold and with a different font size. Is it possible to do this as a line break ("Hello \ n World!") With the command or do I need to make another shortcut for it?

Thanks!

+5
source share
1 answer

Take a look at the API on NSAttributedString - it allows you to create a string that indicates the parts of the string that should be written using specific text styles and / or fonts. The resulting object can be used instead of a simple string with UILabel (and other user interface elements) by setting the label attributedText property instead of the usual text property.

To make only the word "bold" appear in 18 dot bold , try the following:

 var label = UILabel() let bigBoldFont = UIFont.boldSystemFontOfSize(18.0) var attrString = NSMutableAttributedString(string: "This text is bold.") attrString.addAttribute(kCTFontAttributeName, value: bigBoldFont, range: NSMakeRange(13, 4)) label.attributedText = attrString 

The specified range defines the portion of the string to which the assigned name should apply (in this case, the font). And note that the parameters for NSMakeRange are the starting position of the character and the length of the range.

+6
source

Source: https://habr.com/ru/post/1205643/


All Articles