我正在使用一个名为 HMSegmentedControl
的分段控件库,它并不太复杂,但是,设置它的 titleTextAttributes
给我带来了一些麻烦。
titleTextAttributes
是类型 [AnyHashable: Any]!
通过 Swift。在 objc 文件中,它是 @property (nonatomic, strong) NSDictionary *titleTextAttributes
这是我正在做的:
let segmentedControl = HMSegmentedControl(sectionTitles: ["All", "A", "B"])!
segmentedControl.titleTextAttributes = [
NSFontAttributeName: Styleguide.Fonts.someFont.font(ofSize: 9),
NSForegroundColorAttributeName: UIColor.lightGray
]
Xcode 为 NSFontAttributeName
和 NSForegroundColorAttributeName
提供了“使用未解析的标识符”错误
最佳答案
嗯,这是因为那些是 Obj-C 标识符。你应该使用 Swift 的:
NSAttributedStringKey.font
(或者通常只是简单的 .font
,swift 可以在处理本地对象时推断出其余部分,也许在这种情况下不会,但如果它被声明为 AnyHashable: 任何
)
和 NSAttributedStringKey.foregroundColor
。
所以你想要的是:
segmentedControl.titleTextAttributes = [
NSAttributedStringKey.font: Styleguide.Fonts.someFont.font(ofSize: 9),
NSAttributedStringKey.foregroundColor: UIColor.lightGray
]
关于swift - 为 UIControl 子类设置 selectedTitleTextAttributes,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51274140/