对于大字体,a 中的线条Picker
是重叠的。我该如何更改Picker's
行高? (提示:.lineSpacing
修饰符不会这样做。)
See Also
这个问题类似于Ejaaz,但他的问题至今尚未得到答复。
问题
The Code
以下可运行代码产生上述结果。我并不是真的想要不同大小的线条,我只是想要大字体能够正确适应。我试过插入Spacers
,添加.frame
这里和那里的修饰符,.lineSpacing
, padding()
...也许只是还没有找到正确的组合?
struct ContentView: View {
@State private var selected = 0
var body: some View {
Picker(selection: self.$selected, label: Text("Letters")) {
Text("A").font(.system(size: 30))
Text("B").font(.system(size: 40))
Text("C").font(.system(size: 50))
Text("D").font(.system(size: 60))
Text("E").font(.system(size: 70))
Text("F").font(.system(size: 80))
}
}
}
你必须包裹UIPickerView
in a UIViewRepresentable
然后使用:
func pickerView(_ pickerView: UIPickerView, rowHeightForComponent component: Int) -> CGFloat
...更改行高。
Example
它只是展示了如何更改行高,您仍然需要为您的内容扩展它......
struct PickerView: UIViewRepresentable {
var data: [[String]]
@Binding var selections: Int
// Assign custom coordinator for delegate functions.
func makeCoordinator() -> PickerView.Coordinator {
Coordinator(self)
}
// Creates the view object and configures its initial state.
func makeUIView(context: UIViewRepresentableContext<PickerView>) -> UIPickerView {
let picker = UIPickerView(frame: .zero)
picker.dataSource = context.coordinator
picker.delegate = context.coordinator
return picker
}
// Updates the state of the specified view with new information from SwiftUI.
func updateUIView(_ view: UIPickerView, context: UIViewRepresentableContext<PickerView>) {
// for i in 0...(self.selections.count - 1) {
// view.selectRow(self.selections[i], inComponent: i, animated: false)
// }
}
// Coordinator acting as the delegate in SwiftUI.
class Coordinator: NSObject, UIPickerViewDataSource, UIPickerViewDelegate {
var parent: PickerView
//init(_:)
init(_ pickerView: PickerView) {
self.parent = pickerView
}
// Set your custom row height here!
func pickerView(_ pickerView: UIPickerView, rowHeightForComponent component: Int) -> CGFloat {
return 80
}
//numberOfComponents(in:)
func numberOfComponents(in pickerView: UIPickerView) -> Int {
return self.parent.data.count
}
//pickerView(_:numberOfRowsInComponent:)
func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
return self.parent.data[component].count
}
//pickerView(_:titleForRow:forComponent:)
func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
return self.parent.data[component][row]
}
//pickerView(_:didSelectRow:inComponent:)
func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
// self.parent.selections[component] = row
}
}
}
要在 SwiftUI 视图中使用它,只需将自定义PickerView()
在你的身体里。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)