[英]How do I determine what font is being used (by default) in the UISegmentedControl?
There are a dozen answers to how to set the font of a UISegmentedControl
, but I want to determine what font is being used, without having been manually set.关于如何设置UISegmentedControl
的字体有十几个答案,但我想确定正在使用的字体,而无需手动设置。
override func viewDidLoad() {
super.viewDidLoad()
let s = segmentedControl.state // normal
let ta1 = segmentedControl.titleTextAttributes(for: .normal) // nil
let ta2 = UISegmentedControl.appearance().titleTextAttributes(for: .normal) // nil
}
Apple's API documentation is of no insight. Apple 的 API 文档没有洞察力。
The ultimate purpose is to use the font to calculate the size of the string, and compare to the segment size (if I can get that) and determine if the title is being truncated.最终目的是使用字体来计算字符串的大小,并与段大小进行比较(如果我能得到的话)并确定标题是否被截断。 Similar to How do I check if UILabel is truncated?类似于如何检查 UILabel 是否被截断?
You can get the font from one of the UILabel
instances used to create the UISegmentedControl
doing the following:您可以从用于创建UISegmentedControl
的UILabel
实例之一获取字体,执行以下操作:
override func viewDidLoad() {
...
let font = segmentedControl.subviews(ofType: UILabel.self).first?.font
...
}
Here's the definition of subviews
这是subviews
的定义
extension UIView {
func subviews<T>(ofType type: T.Type) -> [T] {
var views: [T] = self.subviews.compactMap { $0 as? T }
for view in self.subviews {
views.append(contentsOf: view.subviews(ofType: type))
}
return views
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.