Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I change the font of a UILabel in swift without changing the font size?

Tags:

I have programatically created a label which I have set the adjustsToFitWidth to true. Now I want to change its font, but I can't do that without making the font size of the label constant. Does anybody know how I could change only the font and not the font size of a UILabel in swift?

like image 990
Matt Spoon Avatar asked Jun 27 '15 16:06

Matt Spoon


People also ask

How do I change the font in SwiftUI?

Hold the command key and click the text to bring up a pop-over menu. Choose Show SwiftUI Inspector and then you can edit the text/font properties.


2 Answers

Why not get the font size, and specify a new font with this value ?

let fontSize = self.label.font.pointSize;
self.label.font = UIFont(name: "HelveticaNeue", size: fontSize)
like image 137
Aeradriel Avatar answered Oct 23 '22 16:10

Aeradriel


You can do it like this:

label.font = UIFont(name: "Arial", size: label.font.pointSize)

This will use the same font size, "Arial" can be whatever family you want to choose.

like image 39
iYoung Avatar answered Oct 23 '22 17:10

iYoung