我使用delegate方法设置UIPickerView
的标题并设置.center
对齐。但如果我从英语换成阿拉伯语,它是右对齐的。如果语言是阿拉伯语在启动期间,它左对齐后切换到英语。
func pickerView(_ pickerView: UIPickerView, viewForRow row: Int, forComponent component: Int, reusing view: UIView?) -> UIView {
let title = getPickerViewTitle(for: row)
let label = UILabel(frame: CGRect(x: 0, y: 0, width: 300, height: 37))
label.text = title
label.textAlignment = .center
label.backgroundColor = UIColor.clear
return label
}
最佳答案
如果您对组件使用RTL语言支持,那么您需要强制更新对齐方式。
func pickerView(_ pickerView: UIPickerView, viewForRow row: Int, forComponent component: Int, reusing view: UIView?) -> UIView {
var pickerLabel : UILabel
if let label = view as? UILabel {
pickerLabel = label
} else {
pickerLabel = UILabel()
pickerLabel.textColor = UIColor.black
if pickerLabel.effectiveUserInterfaceLayoutDirection == .rightToLeft {
pickerLabel.textAlignment = NSTextAlignment.center
}
}
pickerLabel.text = "10"
pickerLabel.sizeToFit()
return pickerLabel
}