OGeek|极客世界-中国程序员成长平台

标题: iOS swift : UIPickerView Text Alignment [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-13 12:14
标题: iOS swift : UIPickerView Text Alignment

尝试将 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



Best Answer-推荐答案


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

// 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/






欢迎光临 OGeek|极客世界-中国程序员成长平台 (http://sqlite.in/) Powered by Discuz! X3.4