我正在嘗試配置分段控制欄。
目標是使每個段的文本字體具有不同的顏色。所以它應該看起來像這樣:

最后的幾點說明:
- 此解決方案可能會影響您可能想要設定的其他色調屬性,并且當您想要為選定和取消選定狀態保持相同顏色的文本時很有用
- 未來的 iOS 更新和 Segment Control 的實作可能會影響其作業方式
更新
如果您想為特定段指定標題顏色,您可以假設(賭博)段控制元件應按邏輯順序布置其子視圖。
我強調了假設,因為這個命令不在我們的控制范圍內,你在執行中受到擺布。
Infact, if you set the selected index segmentedControl.selectedSegmentIndex = 1 and if you have 4 segments, the order of laying out the subviews is 0, 2, 3, 1 so the selected segment gets added last.
What could work for your situation is as follows:
// Add a color queue to hold the colors for the 4 segments
var colorQueue: [UIColor] = [.red, .blue, .green, .orange]
private func updateSegmentTextColor(_ rootView: UIView)
{
for subview in rootView.subviews
{
if let label = subview as? UILabel,
!colorQueue.isEmpty
{
// Dequeue the front of the queue
let color = colorQueue.removeFirst()
label.textColor = color
}
updateSegmentTextColor(subview)
}
}
private func addControl() {
let items = ["One", "Two", "Three", "Four"]
let segmentedControl = UISegmentedControl(items: items)
segmentedControl.frame = CGRect(x: 35, y: 200, width: 250, height: 50)
segmentedControl.center = view.center
segmentedControl.tintColor = .blue
view.addSubview(segmentedControl)
// custom function
updateSegmentTextColor(segmentedControl)
// Add the selected index if you need AFTER the updates
segmentedControl.selectedSegmentIndex = 1
}
You get this

轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/426044.html
上一篇:SwiftUI-拼寫檢查多行字串
