gpt4 book ai didi

iOS swift : UIPickerView Text Alignment

转载 作者:塔克拉玛干 更新时间:2023-11-02 21:11:05 27 4
gpt4 key购买 nike

尝试右/左对齐 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
}

enter image description here

编辑:

当我添加这一行

pickerLabel.frame.size = CGSizeMake(165, 26)

确实有效果,但是选中/未选中的行仍然错位。

enter image description here

最佳答案

我认为您只是忽略了此功能的可能性:

// Swift
func pickerView(pickerView: UIPickerView, widthForComponent component: Int) -> CGFloat {
return CGFloat(50.0)
}

centered and tight components

默认情况下组件会居中对齐,看起来整个选择器也会居中对齐。如果省略 textAlignment,组件(列)中的所有值将对齐。

关于iOS swift : UIPickerView Text Alignment,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34057734/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com