iOS swift : UIPickerView Text Alignment

标签 ios swift uikit uipickerview

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

相关文章:

ios - 如何在不同大小的 Storyboard 中使用相同的 ViewController?

ios - 存档器中的导出按钮灰显

swift - UICollectionViewLayout 子类转换为子类

ios - 无法设置(heroID)用户定义的检查属性

ios - 谷歌云功能通知 iOS

iphone - 如何在iPhone上从互联网下载文件并保存在 'Documents'中?

uikit - 如何使用 UIDocumentPickerViewController 一次导入多个文件?

ios - 水平翻转 MPMoviePlayerController

ios - 在swift中使用继承对象的方法

ios - 重新实例化实例变量 - 一个设计查询