最新消息:20210816 当前crifan.com域名已被污染,为防止失联,请关注(页面右下角的)公众号

[已解决]ActionSheetPicker中PickerView中的字体太大

iOS crifan 3460浏览 0评论

折腾:

[已解决]swift实现左右联动选择

期间,发现字体太大

导致左边一列有些文字都无法正常显示了:

想要设置对应的PickView中的字体,小一点

搜:

ActionSheetCustomPicker titleForRow

但是截止目前,试了几种方法:

(1)

返回attributedTitleForRow:

    func pickerView(pickerView: UIPickerView, attributedTitleForRow row: Int, forComponent component: Int) -> NSAttributedString? {

        gLog.verbose("pickerView=\(pickerView), row=\(row), component=\(component)")

        var curTitle = ""

        switch component {

        case 0:

            curTitle = gCurUserItem.customerSourceList[row].text

        case 1:

            if row < gCurUserItem.customerSourceList[selectedFirstLevelSourceIndex].subSourceList.count {

                curTitle = gCurUserItem.customerSourceList[selectedFirstLevelSourceIndex].subSourceList[row].text

            }

        default:

            break

        }

        gLog.verbose("curTitle=\(curTitle)")

        let attributedTitle:NSAttributedString = NSAttributedString(string: curTitle, attributes: [

            NSFontAttributeName : RightTextFieldTextFont,

            NSForegroundColorAttributeName : RightTextFieldTextColor

            ]

        )

        gLog.verbose("attributedTitle=\(attributedTitle)")

        return attributedTitle

    }

但是也是没用,字体没变化。

(2)

去手动设置对应的pickerTextAttributes:

代码:

                    let customerSourcePicker = ActionSheetCustomPicker(title: "客户来源", delegate: self, showCancelButton: true, origin: self.view)

                    customerSourcePicker.pickerTextAttributes = [

                        NSFontAttributeName : RightTextFieldTextFont,

                        NSForegroundColorAttributeName : RightTextFieldTextColor

                    ]

                    customerSourcePicker.showActionSheetPicker()

还是没有解决。

(3)设置了代理和数据源:

    func actionSheetPicker(actionSheetPicker: AbstractActionSheetPicker!, configurePickerView pickerView: UIPickerView!) {

        gLog.verbose("actionSheetPicker=\(actionSheetPicker), pickerView=\(pickerView)")

        pickerView.delegate = self

        pickerView.dataSource = self

    }

也还是没用

-》感觉是默认

ActionSheetCustomPicker.showPickerWithTitle传入的delegate,就是:

ActionSheetCustomPickerDelegate

其本身已经继承了:UIPickerViewDelegate, UIPickerViewDataSource

-》所以是已经设置过了。

参考:

选择器 – 日期选择,内容选择 – 简书

基本搞清楚了:

默认的PickerView的delegate的逻辑

搜:

uipickerview font size

ios – Change UIPickerView font size for different column – Stack Overflow

iphone – How to change the Font size in UIPickerView? – Stack Overflow

算了,去试试,之前想试试没去试的:viewForRow

用代码:

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

        gLog.verbose("pickerView=\(pickerView), row=\(row), component=\(component)")

        var curTitle = ""

        switch component {

        case 0:

            curTitle = gCurUserItem.customerSourceList[row].text

        case 1:

            if row < gCurUserItem.customerSourceList[selectedFirstLevelSourceIndex].subSourceList.count {

                curTitle = gCurUserItem.customerSourceList[selectedFirstLevelSourceIndex].subSourceList[row].text

            }

        default:

            break

        }

        gLog.verbose("curTitle=\(curTitle)")

        let titleLabelView = UILabel()

        titleLabelView.text = curTitle

        titleLabelView.textAlignment = .Center

        titleLabelView.font = RightTextFieldTextFont

        titleLabelView.textColor = RightTextFieldTextColor

        gLog.verbose("titleLabelView=\(titleLabelView)")

        return titleLabelView

    }

终于生效了,字体变小,颜色正常了:

[总结]

此处,想要改变ActionSheetPicker中PickerView的字体:

不论是返回NSAttributedString,还是ActionSheetCustomPicker去设置属性pickerTextAttributes都没效果,只能是:

用viewForRow,新建一个label,设置字体和颜色,就可以了。

转载请注明:在路上 » [已解决]ActionSheetPicker中PickerView中的字体太大

发表我的评论
取消评论

表情

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址
89 queries in 0.170 seconds, using 22.04MB memory