我如何在 segmentcontrol 中调整字体大小,就像一个标签,它有一个名为 adjustsFontSizeToFitWidth 的属性,所以如果文本大于标签宽度,字体大小将变得更小以适应宽度,我如何为 uisegmetncontrol 做同样的事情?
Use setTitleTextAttrribute to set font for for segment control
SegmentControl.setTitleTextAttributes([NSFontAttributeName: UIFont(name: REGULAR_FONT, size: 13.0)],
for: UIControlState())
SegmentControl.setTitleTextAttributes([NSFontAttributeName: UIFont(name: BOLD_FONT, size: 13.0)], for:.selected)
Added the swift version of answer mentioned in the comment:-
func setWidthTosegmetControl(view :UIView) {
let subviews = view.subviews
for subview in subviews {
if subview is UILabel {
let label: UILabel? = (subview as? UILabel)
print("label found: \(label?.text)")
label?.adjustsFontSizeToFitWidth = true
label?.minimumScaleFactor = 0.1
}else {
setWidthTosegmetControl(view: subview)
}
}
}
I had to do it in the view controller and call the function in viewDidAppear.
func autoshrinkSegmentFontSize() {
for subview in segments.subviews {
for label in subview.subviews {
if let myLabel = subview as? UILabel {
myLabel.adjustsFontSizeToFitWidth = true
myLabel.minimumScaleFactor = 0.5
}
}
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.