I am using the delegate method to set the title of UIPickerView
and setting .center
alignment. But if I switch from English to Arabic, it is right aligned. If the language is Arabic during launch, it left aligns after switching to English.
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
}
if you have using RTL Languages support with components then you need to update alignment forcefully.
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
}