根据 Apple 的文档,有一个名为 pickerView(_:attributedTitleForRow:forComponent 的 UIPickerView 委托(delegate)函数。但是,我试图让它工作,但我似乎在这里做错了什么。我希望有人可以帮助我。
为了简单起见,我有一个简单的程序,它在选择器 View 的两个组件中显示两个数组的内容。该程序只是一个 View Controller 和一个选择器 View 。代码呈现给她:
import UIKit
类 ViewController: UIViewController, UIPickerViewDelegate, UIPickerViewDataSource {
var familyNames = [String]()
var fontName = ""
let firstArray = Array(0...99)
let secondArray = Array(0...99)
let fontCount = 0
@IBOutlet weak var samplePickerView: UIPickerView!
@IBOutlet weak var fontLabel: UILabel!
override func viewDidLoad() {
super.viewDidLoad()
familyNames = UIFont.familyNames.sorted()
let fontNames = UIFont.fontNames(forFamilyName: familyNames[17])
fontName = fontNames.first!
samplePickerView.delegate = self
samplePickerView.dataSource = self
}
func numberOfComponents(in pickerView: UIPickerView) -> Int {
return 2
}
func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
if component == 0 {
return firstArray.count
} else {
return secondArray.count
}
}
func pickerView(_ pickerView: UIPickerView, attributedTitleForRow row: Int, forComponent component: Int) -> NSAttributedString? {
var rowTitle = ""
let font = UIFont(name: fontName, size: 18.0)
let stringDictionary = [NSAttributedString.Key.font: font]
switch component {
case 0:
rowTitle = String(format: "%03d", firstArray[row])
case 1:
rowTitle = String(format: "%03d", secondArray[row])
default:
break
}
let returnString = NSAttributedString(string: rowTitle, attributes: stringDictionary as [NSAttributedString.Key : Any])
print(returnString)
return returnString
}
}
选择器 View 现在应该在 Bradley Hand 中显示标题,因此很容易发现它有效。 不幸的是,选择器 View 没有显示属性字符串中的标题。委托(delegate)方法返回的字符串是一个属性字符串,所以它应该可以工作。图片显示情况并非如此。我究竟做错了什么?
在没有找到解决方案后,我向 Apple 申请了 DTS,以了解我做错了什么。显然,我没有做错任何事情,但是方法“pickerView(_pickerView:UIPickerView,attributeTitleForRow 行:Int,forComponent 组件:Int)-> NSAttributedString?”不允许您在属性字符串上进行太多操作。 Apple 的回答是:“UIPickerView 的属性TitleForRow 委托(delegate)不会更改字体属性,但会适用于字体颜色等其他属性。”
幸运的是,由于我想更改字体大小和字体,有一种方法可以在 UIPickerView 中获得不同的字体和字体大小。 Apple 提供的解决方案:“如果要使用 NSAttributedString 中可用的所有字体属性,请使用 viewForRow 并返回 UILabel 作为替代方案。”
现在的代码如下所示: var familyNames = 字符串 变种字体名称 = "" 让 firstArray = Array(0...99) 让 secondArray = Array(0...99) 让 fontCount = 0
@IBOutlet weak var samplePickerView: UIPickerView!
@IBOutlet weak var fontLabel: UILabel!
override func viewDidLoad() {
super.viewDidLoad()
familyNames = UIFont.familyNames.sorted()
let fontNames = UIFont.fontNames(forFamilyName: familyNames[17])
fontName = fontNames.first!
samplePickerView.delegate = self
samplePickerView.dataSource = self
}
func numberOfComponents(in pickerView: UIPickerView) -> Int {
return 2
}
func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
if component == 0 {
return firstArray.count
} else {
return secondArray.count
}
}
func pickerView(_ pickerView: UIPickerView, viewForRow row: Int, forComponent component: Int, reusing view: UIView?) -> UIView {
var rowTitle = ""
let pickerLabel = UILabel()
pickerLabel.textColor = UIColor.blue
switch component {
case 0:
rowTitle = String(format: "%03d", firstArray[row])
case 1:
rowTitle = String(format: "%03d", secondArray[row])
default:
break
}
pickerLabel.text = rowTitle
pickerLabel.font = UIFont(name: fontName, size: 24.0)
pickerLabel.textAlignment = .center
return pickerLabel
}
结果如下所示: 这是我想对选择器 View 中的文本进行的操作。
关于ios - 带有属性字符串的 UIPickerView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52486572/
欢迎光临 OGeek|极客世界-中国程序员成长平台 (http://jike.in/) | Powered by Discuz! X3.4 |