我该怎么做才能调整文本大小而不在 UIPickerView 中显示 ...?
What can i do to resize the text and not show the ... in UIPickerView?
我正在使用 3 个 PickerView,但有几个词很大,不适合 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
}
现在显示全部为空
在上面使用nameOfLabel.adjustsFontSizeToFitWidth = true
。
我是这样解决的:
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
}
您正在将字符串转换为 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
}
我正在使用 3 个 PickerView,但有几个词很大,不适合 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
}
现在显示全部为空
在上面使用nameOfLabel.adjustsFontSizeToFitWidth = true
。
我是这样解决的:
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
}
您正在将字符串转换为 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
}