UIPickerView(单元格大小)

时间:2018-05-12 13:21:49

标签: ios swift uipickerview

我正在尝试制作横向UIPivkerView。我做了一切,但我不明白如何设置单元格的大小,因为有些单词太长。我试着学习每个单词的长度,并根据它设置单元格大小。但它对我不起作用。我注意到如果我按比例增加它的大小和宽度,我可以增加单元格的大小。

我需要像PickerView50 width一样制作50 height,但我不明白如何处理细胞。

如果我设置值85,我会得到:

enter image description here

如果设置50

enter image description here

import UIKit

class ViewController: UIViewController, UIPickerViewDelegate, UIPickerViewDataSource {
    var data = ["Hello", "WorldWorld", "Of", "PickerPickerPicker", "View"]

    let viewPicker = UIPickerView()
    var rotationAngele: CGFloat!

    var width: CGFloat = 85
    var height: CGFloat = 85
    //var width: CGFloat!

    override func viewDidLoad() {
        super.viewDidLoad()

        viewPicker.delegate = self
        viewPicker.dataSource = self

        viewPicker.layer.borderColor = UIColor.black.cgColor
        viewPicker.layer.borderWidth = 1.5

        // Rotation
        rotationAngele = -90 * (.pi / 180)
        viewPicker.transform = CGAffineTransform(rotationAngle: rotationAngele)

        //width = data[4].widthOfString(usingFont: UIFont(name:"verdana", size: 24.0)!)

        // Create
        viewPicker.frame = CGRect(x: 0 - 150, y: 0, width: view.frame.width + 300, height: 85)
        viewPicker.center = self.view.center

        self.view.addSubview(viewPicker)
    }

    func numberOfComponents(in pickerView: UIPickerView) -> Int {
        return 1
    }

    func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
        return data.count
    }

    func pickerView(_ pickerView: UIPickerView, rowHeightForComponent component: Int) -> CGFloat {
        return 85
    }

    func pickerView(_ pickerView: UIPickerView, widthForComponent component: Int) -> CGFloat {
        return 85
    }

    func pickerView(_ pickerView: UIPickerView, viewForRow row: Int, forComponent component: Int, reusing view: UIView?) -> UIView {

        //width = data[row].widthOfString(usingFont: UIFont(name:"verdana", size: 24.0)!)

        let view = UIView()
        view.frame = CGRect(x: 0, y: 0, width: 150, height: 150)

        let label = UILabel()
        label.frame = CGRect(x: 0, y: 0, width: 150, height: 150)

        label.textAlignment = .center
        label.font = UIFont(name:"verdana", size: 24.0)!
        label.text = data[row]
        view.addSubview(label)

        view.transform = CGAffineTransform(rotationAngle: 90 * (.pi / 180))

        return view
    }
}

////////////////////////////////////////////////////
extension String {
    func widthOfString(usingFont font: UIFont) -> CGFloat {

        let fontAttributes = [NSAttributedStringKey.font: font]
        let size = self.size(withAttributes: fontAttributes)
        return size.width
    }

    func heightOfString(usingFont font: UIFont) -> CGFloat {
        let fontAttributes = [NSAttributedStringKey.font: font]
        let size = self.size(withAttributes: fontAttributes)
        return size.height
    }
}

0 个答案:

没有答案