hey i want to change the font of the label without changing the size as i have different sizes for the different devices See code below:-
this code works but as i said i don't want to change the size
QuotesLabel.font = UIFont(name: "optima", size: CGFloat(30)
here is the code for different sizes for different devices
if UIScreen.mainScreen().bounds.size.height == 480 {
// iPhone 4
QuotesLabel.font = QuotesLabel.font.fontWithSize(30)
} else if UIScreen.mainScreen().bounds.size.height == 568 {
// IPhone 5
QuotesLabel.font = QuotesLabel.font.fontWithSize(30)
} else if UIScreen.mainScreen().bounds.size.width == 375 {
// iPhone 6
QuotesLabel.font = QuotesLabel.font.fontWithSize(35)
} else if UIScreen.mainScreen().bounds.size.width == 414 {
// iPhone 6+
QuotesLabel.font = QuotesLabel.font.fontWithSize(40)
}
To change font properties, highlight the address, right-click, and click Font. Make your changes, and click OK. Still on the Labels window, click the New Document button at the bottom. A Word document will appear containing the labels.
To change the text font for any chart element, such as a title or axis, right–click the element, and then click Font. When the Font box appears make the changes you want.
You can assign its own font size this way:
QuotesLabel.font = UIFont(name: "optima", size: QuotesLabel.font.pointSize)
UPDATE:
You can create and use font array this way:
let fontArr = ["helvetica", "optima", "arial"] //Change this array as per your need
let int = Int(arc4random_uniform(UInt32(fontArr.count)))
QuotesLabel.font = UIFont(name: fontArr[int], size: QuotesLabel.font.pointSize)
Use this UIFont Extension:
extension UIFont{
func fontWithName(name:String)->UIFont{
return UIFont(name: name, size: self.pointSize)!
}
Then use it like this:
QuotesLabel.font = QuotesLabel.font.fontWithName("Verdana"
UPDATE : Random Font from an array
Define an array of fonts you want:
let fonts = ["Verdana", "HoeflerText-Black", "Menlo-BoldItalic"]
This function will return random Font:
func getRandomFont()->UIFont{
let int = Int(arc4random_uniform(UInt32(fonts.count)))
let font = UIFont(name: fonts[int], size: 30)
return font
}
On button Tap action call random Funtion and change TextField Font:
@IBAction func testTapped(sender: UIButton) {
QuotesLabel.font = getRandomFont()
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With