Với Objective-C, tôi đã sử dụng mã hiển thị bên dưới để thiết lập/thay đổi họ phông chữ và kích thước của một bảng chọn:thay đổi phông chữ và kích thước của nó trong một bảng chọn trong nhanh chóng
- (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;
}
Tuy nhiên, Swift không chấp nhận một đúc từ UIView để UILabel và do đó tôi không thể đi theo con đường này mà sẽ giống như hình dưới đây:
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
}
Các stament đầu tiên (cho phép nhãn ....) throuws một ngoại lệ tại thời gian chạy:
EXC-BAD HƯỚNG DẪN (mã = EXC_I386_INVOP, subcode = 0x0)
Liên kết này [link] http://stackoverflow.com/questions/27455345/uipickerview-wont-allow-changing-font- tên và kích thước-thông qua-đại biểu-attributedt [> liên kết] giải quyết vấn đề. –