swift如何更改两个视图之间动态字体?

标签: UITextView ios Swift
发布时间: 2016/3/31 18:54:41
注意事项: 本文中文内容可能为机器翻译,如要查看英文原文请点击上面连接.

我有两个 UIViewControllers (ViewController 和 SettingViewController)。

在我的 SettingViewController 3 按钮,当按下时应更改的字体,在主要的 ViewController UITextView。

我使用 NSUserDefaults。然而,它仅在第一次或根本不起作用。

我觉得问题是,当我回到我主视图设置视图视图不会重新加载。

我如何做这件事?

SettingViewController.swift

class SettingsViewController: UIViewController {

 var handWritingOption: String?
    var zapfinoOption: String?
    var systemOption: String?


@IBAction func handWritingPressed(sender: AnyObject) {

        handWritingOption = "1"
        zapfinoOption = "0"
        systemOption = "0"

        NSNotificationCenter.defaultCenter().postNotificationName("load", object: nil)
        NSUserDefaults.standardUserDefaults().setObject(handWritingOption, forKey: "handWritingBool")
        NSUserDefaults.standardUserDefaults().setObject(zapfinoOption, forKey: "zapfinoBool")
        NSUserDefaults.standardUserDefaults().setObject(systemOption, forKey: "systemBool")
        NSUserDefaults.standardUserDefaults().synchronize()

    }

    @IBAction func zapfinoPressed(sender: AnyObject) {

        handWritingOption = "0"
        zapfinoOption = "1"
        systemOption = "0"

        NSNotificationCenter.defaultCenter().postNotificationName("load", object: nil)
        NSUserDefaults.standardUserDefaults().setObject(handWritingOption, forKey: "handWritingBool")
        NSUserDefaults.standardUserDefaults().setObject(zapfinoOption, forKey: "zapfinoBool")
        NSUserDefaults.standardUserDefaults().setObject(systemOption, forKey: "systemBool")

    }

    @IBAction func systemPressed(sender: AnyObject) {

        handWritingOption = "0"
        zapfinoOption = "0"
        systemOption = "1"

        NSNotificationCenter.defaultCenter().postNotificationName("load", object: nil)
        NSUserDefaults.standardUserDefaults().setObject(handWritingOption, forKey: "handWritingBool")
        NSUserDefaults.standardUserDefaults().setObject(zapfinoOption, forKey: "zapfinoBool")
        NSUserDefaults.standardUserDefaults().setObject(systemOption, forKey: "systemBool")

    }
}

ViewController.swift

class ViewController: UIViewController {

     @IBOutlet weak var noteResult: UITextView!

var handWritingOption = NSUserDefaults.standardUserDefaults().objectForKey("handWritingBool") as? String
    var zapfinoOption = NSUserDefaults.standardUserDefaults().objectForKey("zapfinoBool") as? String
    var systemOption = NSUserDefaults.standardUserDefaults().objectForKey("systemBool") as? String

override func viewDidLoad() {
        super.viewDidLoad()

        NSNotificationCenter.defaultCenter().addObserver(self, selector: "loadList:",name:"load", object: nil)
 }

func loadList(notification: NSNotification){
        println("Font Changed")

        if handWritingOption  == "1" {

            noteResult.font = UIFont(name: "ThinPencilHandwriting", size: 15)
        }

        if zapfinoOption == "1" {

            noteResult.font = UIFont(name: "Zapfino", size: 15)
        }

        if systemOption == "1" {

            noteResult.font = UIFont(name: "Papyrus", size: 15)
        }
    }

}

解决方法 1:

它不工作,因为你使用的 Notification 。如果通知 loadList 是不在 ViewController 注册,当您更改设置时,它不会改变任何东西。要做到这一点进行以下修改。

Step 1︰ 删除使用的通知

Step 2︰ 添加下面的方法在你ViewController

Step 3︰ 调用它在 viewWillAppear() ,如下所示

override func viewWillAppear(animated: Bool) {
    super.viewWillAppear(animated)

    // *** Load font value ***
    handWritingOption = NSUserDefaults.standardUserDefaults().objectForKey("handWritingBool") as? String
    zapfinoOption = NSUserDefaults.standardUserDefaults().objectForKey("zapfinoBool") as? String
    systemOption = NSUserDefaults.standardUserDefaults().objectForKey("systemBool") as? String

    // *** call setFonts ***
    self.setFonts()
}

func setFonts()
{
    if handWritingOption  == "1" {

        noteResult.font = UIFont(name: "ThinPencilHandwriting", size: 15)
    }

    if zapfinoOption == "1" {

        noteResult.font = UIFont(name: "Zapfino", size: 15)
    }

    if systemOption == "1" {

        noteResult.font = UIFont(name: "Papyrus", size: 15)
    }
}

它将设置字体,每次你的视图将显示在屏幕上。

官方微信
官方QQ群
31647020