折腾:
期间,发现字体太大
导致左边一列有些文字都无法正常显示了:
想要设置对应的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,设置字体和颜色,就可以了。