Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Swift - Changing font size inside label

Tags:

ios

swift

I have a label where there is text which should be bold and with another font size. Is there any possibility to do it like the line break ("Hello \n World!") with a command or do I have to make another label for this?

Thanks!

like image 839
Michael Avatar asked Oct 27 '14 20:10

Michael


People also ask

How do I change font size on labels in Swift?

Change Font And Size Of UILabel In Storyboard XIB file, open it in the interface builder. Select the label and then open up the Attribute Inspector (CMD + Option + 5). Select the button on the font box and then you can change your text size or font.

How do I change font size in labels?

To change the font size in HTML, use the style attribute. The style attribute specifies an inline style for an element. The attribute is used with the HTML <p> tag, with the CSS property font-size. HTML5 do not support the <font> tag, so the CSS style is used to add font size.

How do I change the size of labels in Xcode?

You can set the Minimum Font Scale or size in Storyboard/Xib when you set it up in IB under the Attributes inspector. I prefer scale, as it is better at fitting longer text on iPhone 4/5/iPod touches. If you set the size, you can get cut off earlier than with scale.


1 Answers

Look at the API for NSAttributedString -- it allows you to create a string that specifies portions of the string that should be styled with specific text styles and/or fonts. The resulting object can be used instead of a plain string with UILabel (and other UI elements) by setting the label's attributedText property instead of the usual text property.

To make just the word "bold" appear in 18 point bold, try something like 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 range specified determines the portion of the string to which the named attributed (in this case, the font) should be applied. And note that the parameters to NSMakeRange are the starting character position and the length of the range.

like image 70
Todd Agulnick Avatar answered Oct 06 '22 03:10

Todd Agulnick