how to allow the user to change font Swift

血红的双手。 提交于 2021-02-07 10:58:23

问题


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)

      }

回答1:


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;
}



回答2:


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)

  } 


来源:https://stackoverflow.com/questions/30638133/how-to-allow-the-user-to-change-font-swift

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!