我可以更改 UISegmentedControll 中每個段的字體大小嗎?我知道我可以為所有人更改,但我希望每個段都有不同的字體大小。可能嗎?例如,如果我有 3 個段,我希望字體大小為 10、15、20。謝謝
uj5u.com熱心網友回復:
你不能用字串作為片段來做到這一點,但如果你要使用影像,這是可能的。
首先,我們需要一個函式將字串轉換為影像。@NoodleOfDeath在 SO 上的這個答案提供了一個很好的解決方案,它不涉及使用 UIKit 控制元件:
extension String {
func image(withAttributes attributes: [NSAttributedString.Key: Any]? = nil, size: CGSize? = nil) -> UIImage? {
let size = size ?? (self as NSString).size(withAttributes: attributes)
return UIGraphicsImageRenderer(size: size).image { _ in
(self as NSString).draw(in: CGRect(origin: .zero, size: size),
withAttributes: attributes)
}
}
}
然后你只需將影像插入你的 UISegmentedControl 而不是字串:
let segmentedControl = UISegmentedControl(items: [
"Paris".image(withAttributes: [.font: UIFont.systemFont(ofSize: 10)])!,
"London".image(withAttributes: [.font: UIFont.systemFont(ofSize: 15)])!,
"Berlin".image(withAttributes: [.font: UIFont.systemFont(ofSize: 20)])!,
])

轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/430372.html
上一篇:建議用戶將新照片添加到收藏夾
