Rob*_*usa 7 picker uipickerview swift swift2
使用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;
}
Run Code Online (Sandbox Code Playgroud)
但是,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
}
Run Code Online (Sandbox Code Playgroud)
第一个版本(让标签....)在运行时引发异常:
EXC-BAD INSTRUCTION(代码= EXC_I386_INVOP,子代码= 0x0)
Wil*_*son 23
对于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
}
Run Code Online (Sandbox Code Playgroud)
要更改字体名称和大小,您可以使用viewForRow和属性字符串:
func pickerView(pickerView: UIPickerView, viewForRow row: Int, forComponent component: Int, reusingView view: UIView?) -> UIView {
var label = view as! UILabel!
if label == nil {
label = UILabel()
}
var data = pickerData[row]
let title = NSAttributedString(string: data!, attributes: [NSFontAttributeName: UIFont.systemFontOfSize(36.0, weight: UIFontWeightRegular)])
label.attributedText = title
label.textAlignment = .Center
return label
}
Run Code Online (Sandbox Code Playgroud)
如果你使字体大小变大,你会想用rowHeightForComponent增加每行的高度:
func pickerView(pickerView: UIPickerView, rowHeightForComponent component: Int) -> CGFloat {
return 36.0
}
Run Code Online (Sandbox Code Playgroud)
如果要将选取器标签贴到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
}
Run Code Online (Sandbox Code Playgroud)
更惯用的 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
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
18156 次 |
| 最近记录: |