Reputation: 3163
Attempting to right/left align the text in two components in a UIPickerView
. It's being displayed correctly inside of the selected zone, but the text is overlapping elsewhere.
Any idea what I'm doing wrong here?
func pickerView(pickerView: UIPickerView, viewForRow row: Int, forComponent component: Int, reusingView view: UIView?) -> UIView {
let pickerLabel = UILabel()
var titleData: String!
if component == 0 {
if pickerView.tag == 0 {
titleData = weightData[row].description
} else {
titleData = bodyFatData[row].description
}
pickerLabel.textAlignment = .Right
} else {
titleData = decimalData[row].description
pickerLabel.textAlignment = .Left
}
let myTitle = NSAttributedString(string: titleData, attributes: [NSFontAttributeName:UIFont.systemFontOfSize(22.0),NSForegroundColorAttributeName:
UIColor.blackColor()])
pickerLabel.attributedText = myTitle
return pickerLabel
}
EDIT:
When I add this line
pickerLabel.frame.size = CGSizeMake(165, 26)
It does have an effect, but the selected/unselected rows are still misaligned.
Upvotes: 2
Views: 4912
Reputation: 3323
I think you are simply overlooking the possibilities of this function:
// Swift
func pickerView(pickerView: UIPickerView, widthForComponent component: Int) -> CGFloat {
return CGFloat(50.0)
}
By default the components will center align, and it appears the whole picker will also center align. If you omit the textAlignment
, all the values in a component (column) will line up.
Upvotes: 2