iOS swift : UIPickerView Text Alignment
<p><p>尝试将 <code>UIPickerView</code> 中两个组件中的文本右/左对齐。它在所选区域内正确显示,但文本在其他地方重叠。 </p>
<p>知道我在这里做错了什么吗?</p>
<pre><code>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.description
} else {
titleData = bodyFatData.description
}
pickerLabel.textAlignment = .Right
} else {
titleData = decimalData.description
pickerLabel.textAlignment = .Left
}
let myTitle = NSAttributedString(string: titleData, attributes: [NSFontAttributeName:UIFont.systemFontOfSize(22.0),NSForegroundColorAttributeName:
UIColor.blackColor()])
pickerLabel.attributedText = myTitle
return pickerLabel
}
</code></pre>
<p> <a href="/image/xSI9m.png" rel="noreferrer noopener nofollow"><img src="/image/xSI9m.png" alt="enter image description here"/></a> </p>
<p>编辑:</p>
<p>当我添加这一行时</p>
<p><code>pickerLabel.frame.size = CGSizeMake(165, 26)</code></p>
<p>确实有效果,但是选中/未选中的行还是错位了。</p>
<p> <a href="/image/6N44k.png" rel="noreferrer noopener nofollow"><img src="/image/6N44k.png" alt="enter image description here"/></a> </p></p>
<br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
<p><p>我认为您只是忽略了此功能的可能性:</p>
<pre><code>// Swift
func pickerView(pickerView: UIPickerView, widthForComponent component: Int) -> CGFloat {
return CGFloat(50.0)
}
</code></pre>
<p> <a href="/image/5ZxxCm.png" rel="noreferrer noopener nofollow"><img src="/image/5ZxxCm.png" alt="centered and tight components"/></a> </p>
<p>默认情况下,组件将居中对齐,看起来整个选择器也将居中对齐。如果省略<code>textAlignment</code>,组件(列)中的所有值都会对齐。</p></p>
<p style="font-size: 20px;">关于iOSswift: UIPickerView Text Alignment,我们在Stack Overflow上找到一个类似的问题:
<a href="https://stackoverflow.com/questions/34057734/" rel="noreferrer noopener nofollow" style="color: red;">
https://stackoverflow.com/questions/34057734/
</a>
</p>
页:
[1]