iOS中的拾取轮称为
UIPickerView
我们可以在渲染器中获得以下控件:
UITextField textField = Control;
UIPickerView pickerView = textField.InputView as UIPickerView;
然后我们可以根据您的需要使用
Delegate
.
首先我们应该得到
dataSource
在您的情况下,我们可以得到如下结果:
Picker myPicker = Element;
itemList = myPicker.Items.ToList();
其次,创建委托并将列表设置为参数:
pickerView.Delegate = new MyPickerViewDelegate(itemList);
public class MyPickerViewDelegate: UIPickerViewDelegate
{
List<string> itemList;
public MyPickerViewDelegate(List<string> list)
{
itemList = list;
}
}
最后,我们可以开始定制以下事件:
//Define the Font size or style
public override NSAttributedString GetAttributedTitle(UIPickerView pickerView, nint row, nint component)
{
var text = new NSAttributedString(
itemList[(int)row],
font: UIFont.SystemFontOfSize(24),
foregroundColor: UIColor.Red,
strokeWidth: 4
);
return text;
}
//Define the row height
public override nfloat GetRowHeight(UIPickerView pickerView, nint component)
{
return 80;
}
此外,如果要自定义更灵活的位置(包括放置),可以使用以下方法:
public override UIView GetView(UIPickerView pickerView, nint row, nint component, UIView view)
{
UIView contentView = new UIView(new CGRect(0, 0, UIScreen.MainScreen.Bounds.Size.Width, 80));
UILabel label = new UILabel();
label.Frame = contentView.Bounds;
contentView.AddSubview(label);
label.Text = itemList[(int)row];
//Change the label style
label.Font = UIFont.SystemFontOfSize(24);
label.TextColor = UIColor.Red;
return contentView;
}
以下是我的自定义渲染器,供您参考:
public class MyPickerRenderer : PickerRenderer
{
List<string> itemList;
protected override void OnElementChanged(ElementChangedEventArgs<Picker> e)
{
base.OnElementChanged(e);
Picker myPicker = Element;
itemList = myPicker.Items.ToList();
UITextField textField = Control;
UIPickerView pickerView = textField.InputView as UIPickerView;
pickerView.Delegate = new MyPickerViewDelegate(itemList);
}
}