代码之家  ›  专栏  ›  技术社区  ›  MacUserT

带属性字符串的UIPickerView

  •  2
  • MacUserT  · 技术社区  · 6 年前

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

    import UIKit
    

    类ViewController:UIViewController、UIPickerViewDeleteGate、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
    }
    

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

    2 回复  |  直到 6 年前
        1
  •  2
  •   MacUserT    6 年前

    在没有找到解决方案之后,我请求苹果公司的DTS来了解我做错了什么。显然,我没有做错什么,但是方法“pickerView(\pickerView:UIPickerView,attributeditleforrow row:Int,forComponent:Int)->nsattributestring?“不允许您对属性化字符串进行太多操作。苹果给出的答案是:“UIPickerViews attributeditleforrow delegate不会更改字体属性,但会对字体颜色等其他属性起作用。”

    幸运的是,因为我想更改字体大小和字体,所以有一种方法可以在UIPickerView中获得不同的字体和字体大小。苹果提供的解决方案是:“如果你想使用nsattributestring中的所有字体属性,可以使用viewForRow并返回UILabel作为替代。”

    现在代码如下所示: 设firstArray=Array(0…99) 设secondArray=Array(0…99)

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

    enter image description here

        2
  •  0
  •   Community T.Woody    4 年前

    问题来了

    familyNames = UIFont.familyNames.sorted()
    

    字体族 ,但是 UIFont 初始值设定项 作为一个参数,而不是它的 . 从 Apple Documentation :

    家庭名称

    字体系列名称与字体的基本名称相对应,例如Times New Roman。您可以将返回的字符串传递给 fontNames(forFamilyName:) 方法检索可用于该族的字体名称列表。 然后可以使用相应的字体名称来检索实际的字体对象。

    UIFont.fontNames(forFamilyName: String)
    

    另外,不要通过像这样的神奇索引号访问字体系列名称 familyNames[17] ,这是一个糟糕的方法。你可能会越界,这将使你的应用程序崩溃。

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