jjzjj

iOS swift : UIPickerView Text Alignment

coder 2024-01-23 原文

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

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

最佳答案

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

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

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

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

有关iOS swift : UIPickerView Text Alignment的更多相关文章

随机推荐