如何允许用户更改字体Swift [英] how to allow the user to change font Swift

查看:46
本文介绍了如何允许用户更改字体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

提前谢谢

更新

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)

      }

推荐答案

您可以使用

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

当用户选择字体时(您必须创建一个界面,Rizwan关于使用UIPickerView的答案是一个好主意),您可以保存字体并为所有标签和textViews设置字体.

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

您可能要检查保存的字体并进行设置.可以在根viewController的 -viewDidLoad 方法中调用它,甚至可以在应用程序委托中调用它.

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

这篇关于如何允许用户更改字体Swift的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆