【发布时间】:2015-12-03 04:08:09
【问题描述】:
尝试右/左对齐UIPickerView 中两个组件中的文本。它在所选区域内正确显示,但文本在其他地方重叠。
知道我在这里做错了什么吗?
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
}
编辑:
当我添加这一行时
pickerLabel.frame.size = CGSizeMake(165, 26)
确实有效果,但是选中/未选中的行还是错位了。
【问题讨论】:
-
给你的 UILabel 一个尺寸会有帮助吗?
-
另外,不要将标签文本对齐与属性文本结合起来。如果要使用属性文本,请在属性文本段落样式中包含对齐方式。
-
尝试给
pickerLabel增加尺寸,同时实现rowHeightForComponent函数。 -
更改 pickerLabel 大小确实有效果,但似乎无法同时对齐选中和未选中的文本。
标签: ios swift uikit uipickerview