【问题标题】:iOS Swift: UIPickerView Text AlignmentiOS Swift:UIPickerView 文本对齐
【发布时间】: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


【解决方案1】:

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

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

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

【讨论】:

  • 这其实是件好事,谢谢,它使它在只有少量文本时看起来更整洁
猜你喜欢
  • 2012-06-19
  • 1970-01-01
  • 2017-03-24
  • 2013-10-13
  • 1970-01-01
  • 2016-03-13
  • 2020-07-12
  • 2012-10-07
  • 1970-01-01
相关资源
最近更新 更多