使用Objective-C,我使用下面显示的代码来设置/更改选择器的字体系列和大小:
- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view {
UILabel* tView = (UILabel*)view;
if (!tView){
tView = [[UILabel alloc] init];
// Setup label properties - frame, font, colors etc
tView.font = [UIFont fontWithName:@"Times New Roman" size:fontsize];;
}
tView.text = [_mysitedata findKeyForRow:row];
NSLog(@"C key:%@ row:%ld comp:%ld", tView.text, (long int)row, (long int)component);
return tView;
}
然而,Swift不接受从UIView到UILabel的演员阵容,因此我不能遵循这条看起来如下所示的路径:
func pickerView(pickerView: UIPickerView, viewForRow row: Int, forComponent component: Int, reusingView view: UIView?) -> UIView {
let label = view as! UILabel
label.font = UIFont(name: "Times New Roman", size: 1.0)
label.text = pickerData[row]
return label
}
第一个版本(让标签....)在运行时引发异常:
EXC-BAD INSTRUCTION(代码= EXC_I386_INVOP,子代码= 0x0)
答案 0 :(得分:22)
对于Swift 3
func pickerView(_ pickerView: UIPickerView, viewForRow row: Int, forComponent component: Int, reusing view: UIView?) -> UIView {
let label = (view as? UILabel) ?? UILabel()
label.textColor = .black
label.textAlignment = .center
label.font = UIFont(name: "SanFranciscoText-Light", size: 18)
// where data is an Array of String
label.text = pickerData[row]
return label
}
答案 1 :(得分:6)
要更改字体名称和大小,您可以使用viewForRow和属性字符串:
func pickerView(pickerView: UIPickerView, rowHeightForComponent component: Int) -> CGFloat {
return 36.0
}
如果你的字体大小更大,你会想用rowHeightForComponent增加每行的高度:
{{1}}
答案 2 :(得分:4)
如果您希望选择器标签为AUTOSHRINK ...
设置adjustsFontSizeToFitWidth=true
和minimumScaleFactor=0.5
func pickerView(_ pickerView: UIPickerView, viewForRow row: Int, forComponent component: Int, reusing view: UIView?) -> UIView {
var label: UILabel
if let view = view as? UILabel { label = view }
else { label = UILabel() }
label.text = "..."
label.textAlignment = .center
label.font = UIFont.boldSystemFont(ofSize: 20)
label.adjustsFontSizeToFitWidth = true
label.minimumScaleFactor = 0.5
return label
}
答案 3 :(得分:2)
对于swift 2.3
func pickerView(pickerView: UIPickerView, viewForRow row: Int, forComponent component: Int, reusingView view: UIView?) -> UIView{
var label = view as! UILabel!
if label == nil {
label = UILabel()
}
label.font = UIFont(name: "Lato-Regular", size: 17)!
label.text = dataArray[row] as? String
label.textAlignment = .Center
return label
}
适用于Swift 3
func pickerView(pickerView: UIPickerView, viewForRow row: Int, forComponent component: Int, reusingView view: UIView?) -> UIView{
print("Returning Custom label")
var label = view as! UILabel!
if label == nil {
label = UILabel()
}
label?.font = UIFont(name: "Lato-Regular", size: 14)!
label?.text = dataArray[row] as? String
label?.textAlignment = .center
return label!
}
答案 4 :(得分:1)
一个更惯用的Swift编码是:
func pickerView(pickerView: UIPickerView, viewForRow row: Int, forComponent component: Int, reusingView view: UIView?) -> UIView {
guard let label = view as? UILabel else {
preconditionFailure ("Expected a Label")
}
label.font = UIFont(name: "Times New Roman", size: 1.0)
label.text = pickerData[row]
return label
}