有没有可能在一个地方更改iOS移动应用程序的所有UILabel颜色?
每当应用程序加载时,我必须在一个地方更改所有UILabel颜色。我知道有一种方法可以通过设置textColor分别更改UILabel的颜色。因为这是一个耗时的过程,通过编程或使用故事板更改所有UILabel 我需要知道如何通过在一个地方添加textColor来更改标签颜色,从而简化操作。即使是更改控制器标签颜色,也非常相似,例如通过UILabel类别有没有可能在一个地方更改iOS移动应用程序的所有UILabel颜色?,ios,uilabel,Ios,Uilabel,每当应用程序加载时,我必须在一个地方更改所有UILabel颜色。我知道有一种方法可以通过设置textColor分别更改UILabel的颜色。因为这是一个耗时的过程,通过编程或使用故事板更改所有UILabel 我需要知道如何通过在一个地方添加textColor来更改标签颜色,从而简化操作。即使是更改控制器标签颜色,也非常相似,例如通过UILabel类别 任何片段都将有助于我实现这一点。单个控制器的UIlabel颜色代码良好 有各种各样的方法来处理这个问题。您可能不应该使用tintColor属性,因
任何片段都将有助于我实现这一点。单个控制器的UIlabel颜色代码良好 有各种各样的方法来处理这个问题。您可能不应该使用tintColor属性,因为苹果打算将其作为可点击项目的视觉提示 我建议创建
UILabel
的自定义子类。我们称之为ColorChangingLabel
。定义一个NotificationCenter
通知名称labelColorChanged
,当您广播该通知时,将一个键/值对textColor
添加到包含新文本颜色的通知用户信息中。如果希望能够更改,还可以添加可选的背景色键/值对。(事实上,您可以添加一整套不同的键/值对,以便更改自定义标签的不同属性。)
在自定义UILabel子类中:
- 定义一个函数
。有吗 函数调用addLabelColorChangedObserver()
,为您的应用程序添加一个观察者通知中心
通知,在通知的labelColorChanged
中查找userInfo
,并使用该颜色更改标签的文本颜色textColor
- 重写
和init(frame:)
并使用这些方法 调用init(coder:)
函数addLabelColorChangedObserver()
- 为UILabel子类实现一个deinit方法,该方法将删除其 观察员
userInfo
字典发布通知
完成所有这些操作后,浏览应用程序的所有故事板,选择每个标签,并使用“身份检查器”将对象的类更改为自定义的颜色更改标签
类。应该这样做
编辑:
如果希望能够逐个视图控制器以颜色更改为目标,则可以将标签设置为具有owningViewController
属性,设置后,当他们将自己添加为labelColorChanged
通知的观察者时,他们将添加owningViewController
作为对象
参数。然后,要告诉单个视图控制器的标签更改其颜色,您需要使用目标视图控制器作为对象
参数发送通知
这里需要技巧的是在标签上设置owningViewController
属性。您可能希望有一种方法能够在加载视图控制器的视图时自动设置它。在视图控制器的viewDidLoad方法中,您可能会调用一个递归遍历视图控制器层次结构的方法,将自身设置为所有颜色更改标签的owningViewController
编辑2:
我实现了一个CustomLabel类,如上所述。代码如下:
//
// CustomLabel.swift
// CustomLabel
//
// Created by Duncan Champney on 4/20/17.
// Copyright © 2017 Duncan Champney. All rights reserved.
//
import UIKit
let labelColorChangedNotice: NSNotification.Name = NSNotification.Name("labelColorChangedNotice")
let textColorKey = "textColor"
let backgroundColorKey = "backgroundColor"
class CustomLabel: UILabel {
static var classLabelColorChangeObserver: Any!
static var startingTextColor: UIColor?
static var startingTextBGColor: UIColor?
override class func initialize() {
//Have the CustomLabel class add an observer to record changes to the text color and/or background color even if there are no CustomLabel instances on-screen.
classLabelColorChangeObserver = NotificationCenter.default.addObserver(forName: labelColorChangedNotice,
object: nil,
queue: nil ) {
notification in
if let textColor = notification.userInfo?[textColorKey] as? UIColor {
CustomLabel.startingTextColor = textColor
}
if let backgroundColor = notification.userInfo?[backgroundColorKey] as? UIColor {
CustomLabel.startingTextBGColor = backgroundColor
}
}
}
var labelColorChangeObserver: Any?
override init(frame: CGRect) {
super.init(frame: frame)
labelColorChangeObserver = addLabelColorChangedObserver()
}
required init?(coder: NSCoder) {
super.init(coder: coder)
labelColorChangeObserver = addLabelColorChangedObserver()
}
deinit {
if let labelColorChangeObserver = labelColorChangeObserver {
NotificationCenter.default.removeObserver(observer: labelColorChangeObserver)
}
}
func addLabelColorChangedObserver() {
if let startingTextColor = CustomLabel.startingTextColor {
self.textColor = startingTextColor
}
if let startingTextBGColor = CustomLabel.startingTextBGColor {
self.backgroundColor = startingTextBGColor
}
labelColorChangeObserver = NotificationCenter.default.addObserver(forName: labelColorChangedNotice,
object: nil,
queue: nil ) {
[weak self] //Use a capture list to avoid a retain cycle
notification in
//Once we're in the notification's closure, capture self strongly, or bail if it's nil.
guard let strongSelf = self else {
return
}
//If we've been given a textColor, install it in the label.
if let textColor = notification.userInfo?[textColorKey] as? UIColor {
strongSelf.textColor = textColor
}
//If we've been given a backgroundColor, install it in the label.
if let backgroundColor = notification.userInfo?[backgroundColorKey] as? UIColor {
strongSelf.backgroundColor = backgroundColor
}
}
}
}
创建UILabel的自定义类,并将其设置为故事板中的所有标签,在需要为观察者发布NSNotification时更改自定义类中的文本颜色?是否需要单独调用所有UILabel?否。如果创建UILabel的子类为通知添加侦听器,则在广播通知时,屏幕上的所有标签都将更改颜色。但是,请注意,后缀创建的标签将不会显示。您可能应该定义一个静态变量来保存当前标签颜色,让类观察相同的通知并更改静态变量,让init方法使用静态变量来设置标签颜色谢谢您为管理UI提供了有效的方法。我可以使用这种方法来动态定制视图。一旦我将上述所有更改集成到我的应用程序中,我将立即与您联系。