user3599431
user3599431

Reputation: 43

how to allow the user to change font Swift

I am building an application that display details and i want to allow user to change font of these details I have done some researches but all of them were to go to device's settings and change the font from there so, is there any way to have similar settings inside my application

Thank you in advance

Update

i used the following slider but it didnt works

@IBAction func FontSlider(sender: AnyObject) {
    var currentValue  = Int(Slider.value) as Int
    println(currentValue)
    ingredients.font = UIFont(name: ingredients.font.fontName, size: currentValue)

      }

Upvotes: 0

Views: 1675

Answers (2)

Rizwan Shaikh
Rizwan Shaikh

Reputation: 2904

Better approach is used UIPickerView in which u give the different UIfont option to user.

Once user select any option use didSelectRow function and change the font with respect to user selected font.

Edit: As u used slider than used as

@IBAction func FontSlider(sender: UISlider) {
var currentValue  = Int(Slider.value) as Int
println(currentValue)
ingredients.font = UIFont(name: ingredients.font.fontName, size: currentValue)

  } 

Upvotes: 0

ZeMoon
ZeMoon

Reputation: 20274

You can make use of the appearance proxies for UILabel and UITextView, and save the font in NSUserDefaults.

When the user selects a font (You will have to create an interface, Rizwan's answer about using a UIPickerView is a good idea), you can save the font and set it for all labels and textViews.

func setDefaultFont (font: UIFont)
{
    NSUserDefaults.standardUserDefaults().setObject(font, forKey: "defaultFont")

    UILabel.appearance().font = font;

    UITextView.appearance().font = font;
}

You might want to check for the saved font and set it. This can be called in the -viewDidLoad method of your root viewController or even in the app delegate.

func checkAndSetDefaultFont ()
{
    let font: UIFont = (NSUserDefaults.standardUserDefaults().objectForKey("defaultFont") as? UIFont)!

    UILabel.appearance().font = font;

    UILabel.appearance().font = font;
}

Upvotes: 2

Related Questions