我正在使用3个PickerViews并且一些字很大而且不适合PickerView并且看起来像这样" IamAWordVer ..."而且我不希望这种情况发生。我希望它调整字体大小或类似于适合PickerView中的所有单词,但我不会影响PickerView的大小或其他选择器视图的字体大小,包括此PickerView中发生的单词。
例如,所有单词都使用默认值,如果不适合调整大小以适合所有单词。
func pickerView(pickerView: UIPickerView, viewForRow row: Int, forComponent component: Int, reusingView view: UIView?) -> UIView {
let pickerLabel = UILabel()
if pickerView == pvOne{
pickerLabel.adjustsFontSizeToFitWidth = true
pickerLabel.attributedText = oneText[row] as? NSAttributedString
}
else if(pickerView == pvTwo){
pickerLabel.adjustsFontSizeToFitWidth = true
pickerLabel.attributedText = twoText[row] as? NSAttributedString
}
else if(pickerView == pvThree){
pickerLabel.adjustsFontSizeToFitWidth = true
pickerLabel.attributedText = threeText[row] as? NSAttributedString
}
return pickerLabel
}
它显示所有空白
答案 0 :(得分:4)
在其上使用nameOfLabel.adjustsFontSizeToFitWidth = true
。
答案 1 :(得分:1)
我这样解决了:
func pickerView(pickerView: UIPickerView, viewForRow row: Int, forComponent component: Int, reusingView view: UIView?) -> UIView {
let pickerLabel = UILabel()
if pickerView == pvOne{
pickerLabel.adjustsFontSizeToFitWidth = true
let titleData = oneText[row]
let myTitle = NSAttributedString(string: titleData as! String, attributes: [NSFontAttributeName:UIFont(name: "Georgia", size: 20.0)!,NSForegroundColorAttributeName:UIColor.blackColor()])
pickerLabel.attributedText = myTitle
}
else if(pickerView == pvTwo){
pickerLabel.adjustsFontSizeToFitWidth = true
let titleData = twoText[row]
let myTitle = NSAttributedString(string: titleData as! String, attributes: [NSFontAttributeName:UIFont(name: "Georgia", size: 20.0)!,NSForegroundColorAttributeName:UIColor.blackColor()])
pickerLabel.attributedText = myTitle
}
else if(pickerView == pvThree){
pickerLabel.adjustsFontSizeToFitWidth = true
let titleData = threeText[row]
let myTitle = NSAttributedString(string: titleData as! String, attributes: [NSFontAttributeName:UIFont(name: "Georgia", size: 20.0)!,NSForegroundColorAttributeName:UIColor.blackColor()])
pickerLabel.attributedText = myTitle
}
return pickerLabel
}
答案 2 :(得分:0)
您正在将一个String转换为NSAttributedString。
尝试:
func pickerView(pickerView: UIPickerView, viewForRow row: Int, forComponent component: Int, reusingView view: UIView?) -> UIView {
let pickerLabel = UILabel()
if pickerView == pvOne{
pickerLabel.adjustsFontSizeToFitWidth = true
pickerLabel.text = oneText[row]
}
else if(pickerView == pvTwo){
pickerLabel.adjustsFontSizeToFitWidth = true
pickerLabel.text = twoText[row]
}
else if(pickerView == pvThree){
pickerLabel.adjustsFontSizeToFitWidth = true
pickerLabel.text = threeText[row]
}
return pickerLabel
}