在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-错误指令(代码=EXC_I386_INVOP,子代码= 0x 0)
5条答案
按热度按时间czq61nw11#
适用于Swift 3
u1ehiz5o2#
要更改字体名称和大小,可以使用viewForRow和一个属性化字符串:
如果您将字体大小设置得更大,则需要使用rowHeightForComponent增加每行的高度:
lrpiutwd3#
如果您希望选取器标签自动收缩...
设置
adjustsFontSizeToFitWidth=true
和minimumScaleFactor=0.5
gfttwv5a4#
适用于swift 2.3
适用于雨燕3
uklbhaso5#
更符合Swift习惯的编码是: