Swift中的NightNight更改主题,attributedText问题

时间:2018-11-15 14:33:51

标签: ios swift uicollectionview themes uicollectionviewcell

我目前正在试验NightNight,这是一个易于使用的库,可以为应用添加明暗主题。如果您有兴趣向应用程序添加两个主题,我建议您对其进行研究。

话虽如此,它就像是一种魅力。尚不完全支持,例如我缺少设置shadowImage的{​​{1}}来更改“顶部边框”或分隔符的功能,但是我想稍后可以通过深入研究源代码来添加它。

我现在面临的一个问题是,我无法更改UITabBarController的{​​{1}}的颜色。我肯定需要使用attributedText属性,因为我想添加UITextView。我可以使用所有功能,但是在主题之间切换时颜色不会改变。

attributedTextlineHeightMultiple的一部分,因此可以重复使用。每当我重新启动应用程序或开始在UITextView中滚动时,文本都会更新(有时-由于重用,当单元格在屏幕上仍然可见时,有时它不会更新)。在我看来,我需要更新或重新加载其他内容才能使该库按需运行。

当前,我以如下方式设置TextView:

UICollectionViewCell

下面是NightNight中源文件的片段,称为NSMutableAttributedString,其中添加了观察者:

UICollectionView

然后_updateTitleAttributes看起来像这样:

let customTextViewStyle = NSMutableParagraphStyle()
customTextViewStyle.lineHeightMultiple = 1.2

let attributedString = NSMutableAttributedString(string: "This is the text that will be shown inside the TextView")

attributedString.setMixedAttributes([NNForegroundColorAttributeName: MixedColor(normal: lightCustomTextViewFontColor, night: darkCustomTextViewFontColor)], range: NSRange(location: 0, length: attributedString.length))
attributedString.addAttribute(NSAttributedString.Key.paragraphStyle, value: customTextViewStyle, range: NSRange(location: 0, length: attributedString.length))
attributedString.addAttribute(NSAttributedString.Key.font, value: UIFont.systemFont(ofSize: normalFontSize), range: NSRange(location: 0, length: attributedString.length))

customTextView.attributedText = attributedString

我非常感谢您解决此问题,因为我自己也无法解决。它与源有关吗-例如它更新不正确吗?与我的public extension NSMutableAttributedString { fileprivate struct AssociatedKeys { static var mixedAttrsKey = "mixedAttrs" } fileprivate var mixedAttrs: [String: [NSRange: MixedColor]] { get { if let dict = objc_getAssociatedObject(self, &AssociatedKeys.mixedAttrsKey) as? [String : [NSRange : MixedColor]] { return dict } self.mixedAttrs = [:] MixedColorAttributeNames.forEach { (mixed) in self.mixedAttrs[mixed] = [:] } return self.mixedAttrs } set { objc_setAssociatedObject(self, &AssociatedKeys.mixedAttrsKey, newValue as AnyObject, .OBJC_ASSOCIATION_RETAIN_NONATOMIC) addNightObserver(#selector(_updateTitleAttributes)) } } } 有关系吗?

谢谢!

0 个答案:

没有答案