UIPickerView文本比例不够

时间:2016-05-27 07:15:46

标签: ios swift uipickerview nsattributedstring uipickerviewdatasource

我需要像这样实现UIPickerView:

enter image description here

但是默认实现提供了这个:

enter image description here

我使用了这个UIPickerDatasourse方法:

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

        var pickerLabel = view as? UILabel;

        if (pickerLabel == nil)
        {
            pickerLabel = UILabel()
            pickerLabel?.textAlignment = NSTextAlignment.Center
        }

        pickerLabel?.attributedText = help_getAttributedStringForWalletByAmount(ApiManager.sharedInstance.userService_currentUser!.arrayCurrencyAccounts[row].balance, currency: EnumCurrency(rawValue: ApiManager.sharedInstance.userService_currentUser!.arrayCurrencyAccounts[row].currency))
        pickerLabel?.sizeToFit()

        return pickerLabel!;
    }

是否可以进行更大的缩放?或者可能还有其他解决方法吗?

2 个答案:

答案 0 :(得分:1)

你可以试试,

func pickerView(pickerView: UIPickerView, attributedTitleForRow row: Int, forComponent component: Int) -> NSAttributedString? {
     let color = (row == pickerView.selectedRowInComponent(component)) ? UIColor.orangeColor() : UIColor.blackColor()
     return NSAttributedString(string: colors[row], attributes: [NSForegroundColorAttributeName: color])
 }

 func pickerView(pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
    pickerView.reloadAllComponents()
 }

它正在改变颜色,你在属性字符串中尝试使用fontsize。

<强>更新

如上所述,请参阅this answer,不要只实施titleFoRrow viewForRow。并使用所需的字体大小设置label

希望这会有所帮助:)

答案 1 :(得分:0)

Lion的答案实际上有效,但它并不顺畅,所以当你选择另一行时,它不是用户友好的(我认为它适用于颜色,但对于不同的字体,它不是最好的解决方案)。所以我决定仿效变换。这是代码:

// MARK: - Picker View Help

    func pickerViewHelp_afterRowWasSelected(row: Int) {
        let accountSelected = ApiManager.sharedInstance.userService_currentUser!.arrayCurrencyAccounts[row]
        // save ID of this currency account in NSDefaults
        DefaultsManager.sharedInstance.getDefaultsForSharingBetweenExtensions().setValue(accountSelected.id, forKey: kNSDefaults_mainCurrencyAccountID)
        DefaultsManager.sharedInstance.getDefaultsForSharingBetweenExtensions().synchronize()
        // reload scales
        delay(0) { [weak self] in
            self?.pickerViewHelp_updateFontsForAllRows(self!.pickerViewAccounts, selectedIndex: row)
        }
    }

    func pickerViewHelp_updateFontsForAllRows(pickerView: UIPickerView, selectedIndex: Int) {
        if pickerViewHelp_rowWasChangedBeforeReloadingPicker { // we changed the row
            // remove prev animation
            for i in 0..<pickerView.numberOfRowsInComponent(0) {
                let pickerLabel = pickerViewHelp_dictOfViewsInPicker[i] as? UILabel
                pickerLabel?.layer.removeAllAnimations()
            }
            // start new animation
            for i in 0..<pickerView.numberOfRowsInComponent(0) {
                UIView.animateWithDuration(0.3) { [weak self] in
                    let scale: CGFloat = (i == selectedIndex) ? 1 : 0.5
                    //                let pickerLabel = pickerView.viewForRow(i, forComponent: 0) as? UILabel
                    let pickerLabel = self?.pickerViewHelp_dictOfViewsInPicker[i] as? UILabel
                    var transform = CATransform3DIdentity
                    transform = CATransform3DScale(transform, scale, scale, 1.01)
                    pickerLabel?.layer.transform = transform
                }
            }
        }
        else if pickerViewHelp_previouslySelectedRow == -1  { // first setup of the picker or we selected the same row
            // remove prev animation
            for i in 0..<pickerView.numberOfRowsInComponent(0) {
                let pickerLabel = pickerViewHelp_dictOfViewsInPicker[i] as? UILabel
                pickerLabel?.layer.removeAllAnimations()
            }
            // without animation
            for i in 0..<pickerView.numberOfRowsInComponent(0) {
                let scale: CGFloat = (i == selectedIndex) ? 1 : 0.5
                let pickerLabel = pickerViewHelp_dictOfViewsInPicker[i] as? UILabel
                var transform = CATransform3DIdentity
                transform = CATransform3DScale(transform, scale, scale, 1.01)
                pickerLabel?.layer.transform = transform
            }
        }
        else { // repeat 1 if (for now I want leave in like this, because if smth is wrong, I will need to change it separately
            // remove prev animation
            for i in 0..<pickerView.numberOfRowsInComponent(0) {
                let pickerLabel = pickerViewHelp_dictOfViewsInPicker[i] as? UILabel
                pickerLabel?.layer.removeAllAnimations()
            }
            // start new animation
            for i in 0..<pickerView.numberOfRowsInComponent(0) {
                UIView.animateWithDuration(0.3) { [weak self] in
                    let scale: CGFloat = (i == selectedIndex) ? 1 : 0.5
                    //                let pickerLabel = pickerView.viewForRow(i, forComponent: 0) as? UILabel
                    let pickerLabel = self?.pickerViewHelp_dictOfViewsInPicker[i] as? UILabel
                    var transform = CATransform3DIdentity
                    transform = CATransform3DScale(transform, scale, scale, 1.01)
                    pickerLabel?.layer.transform = transform
                }
            }
        }


    }

    // MARK: - Picker view

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

    func pickerView(pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
        if ApiManager.sharedInstance.userService_currentUser?.arrayCurrencyAccounts == nil { // no account
            return 0
        }
        else {
            let count = ApiManager.sharedInstance.userService_currentUser!.arrayCurrencyAccounts.count
            return count
        }
    }

    var pickerViewHelp_rowWasChangedBeforeReloadingPicker = false

    var pickerViewHelp_dictOfViewsInPicker = [Int: UIView]()

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

        var pickerLabel = view as? UILabel;

        if (pickerLabel == nil)
        {
            pickerLabel = UILabel()
            pickerLabel?.textAlignment = NSTextAlignment.Center
        }
        pickerLabel?.attributedText = help_getAttributedStringForWalletByAmount(ApiManager.sharedInstance.userService_currentUser!.arrayCurrencyAccounts[row].balance, currency: EnumCurrency(rawValue: ApiManager.sharedInstance.userService_currentUser!.arrayCurrencyAccounts[row].currency), scaleOfText: 1)
        pickerViewHelp_dictOfViewsInPicker[row] = pickerLabel

        return pickerLabel!;
    }

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

    var pickerViewHelp_previouslySelectedRow = -1

    func pickerView(pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
        if pickerViewHelp_previouslySelectedRow != row {
            pickerViewHelp_rowWasChangedBeforeReloadingPicker = true
        }
        else {
            pickerViewHelp_rowWasChangedBeforeReloadingPicker = false
        }
        pickerViewHelp_previouslySelectedRow = row
        pickerView.selectRow(row, inComponent: 0, animated: false) // because otherwise sometimes it stops between cells in the picker
        if pickerViewHelp_rowWasChangedBeforeReloadingPicker {
//            print("reload all components")
            pickerView.reloadAllComponents()
        }
        pickerViewHelp_afterRowWasSelected(row)
    }

它也不理想,但我们的设计师喜欢它,所以我们决定使用这种方式