Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

change font of the label without changing the size

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)

    }
like image 524
Motivation gym5 Avatar asked Aug 05 '15 04:08

Motivation gym5


People also ask

Can I change font on Word labels?

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.

How do I change the font of a data label in Excel?

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.


2 Answers

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)
like image 147
Dharmesh Kheni Avatar answered Nov 06 '22 19:11

Dharmesh Kheni


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()
  }
like image 44
Hamza Ansari Avatar answered Nov 06 '22 17:11

Hamza Ansari