带有属性字符串的 UIPickerView

UIPickerView with attributes string

根据 Apple 的文档,有一个名为 pickerView(_:attributedTitleForRow:forComponent:) 的 UIPickerView 委托函数。但是,我试图让它工作,但我似乎在这里做错了什么。我希望有人能帮助我。

为了简单起见,我有一个简单的程序,它在选择器视图的两个组件中显示两个数组的内容。该程序只是一个视图控制器和一个选择器视图。代码呈现给她:

import UIKit

class 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
}

}

选择器视图现在应显示 Bradley Hand 中的标题,因此很容易发现有效的标题。 不幸的是,选择器视图没有在属性字符串中显示标题。委托方法返回的字符串是一个属性字符串,所以它应该可以工作。图片显示情况并非如此。我究竟做错了什么?

问题来了

familyNames = UIFont.familyNames.sorted()

你得到了 字体家族 的名称,但是 UIFont 初始化程序将 字体名称 作为参数,而不是它的 姓氏。来自 Apple Documentation:

familyNames

Font family names correspond to the base name of a font, such as Times New Roman. You can pass the returned strings to the fontNames(forFamilyName:) method to retrieve a list of font names available for that family. You can then use the corresponding font name to retrieve an actual font object.

因此,如果您想获取给定姓氏的字体名称,请使用以下方法。然后你可以使用它们来创建字体。

UIFont.fontNames(forFamilyName: String)

此外,不要通过像 familyNames[17] 这样的魔术索引号来访问您的字体系列名称,这是一种糟糕的方法。您可能会越界,这会使您的应用程序崩溃。

我会将我的字体名称存储在一个常量中,例如

let pickerFontName = "MyAwesomeFont"

// later in your program
let font = UIFont(name: pickerFontName, size: 18.0)

在没有找到解决方案后,我在 Apple 请求了 DTS 以了解我做错了什么。显然,我没有做错任何事,但是 "pickerView(_ pickerView: UIPickerView, attributedTitleForRow row: Int, forComponent component: Int) -> NSAttributedString?" 方法不允许您对属性字符串进行太多操作。 Apple 的回答是:"UIPickerView’s attributedTitleForRow delegate won’t change the font attribute, but will work for other attributes like font color."

幸运的是,由于我想更改字体大小和字体,所以有一种方法可以在 UIPickerView 中获取不同的字体和字体大小。苹果提供的解决方案:"If you want to use all the font attributes available in NSAttributedString, use viewForRow and return a UILabel as an alternative."

代码现在看起来像这样: var familyNames = 字符串 var 字体名称 = "" 让 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
}

结果如下所示: 这是我想对选择器视图中的文本进行的操作。