xamarin 如何设置选择器文本的颜色和文本/字体大小?

jdg4fx2g  于 2023-08-01  发布在  其他
关注(0)|答案(1)|浏览(153)

我使用一个特定的选择器设置来模仿Xamarin.iOS中下拉列表的操作。代码为:

public void ConfigureSelectPicker(UITextField pickerTextField, List<string> theData)
    {
        PickerViewModel MyModel = new PickerViewModel();
        MyModel._pickerSource = theData;
        var picker = new UIPickerView
        {
            Model = MyModel,
            ShowSelectionIndicator = true,
            TintColor = UIColor.Blue
        };
        var screenWidth = UIScreen.MainScreen.Bounds.Width;
        var pickerToolBar = new UIToolbar(new RectangleF(0, 0, (float)screenWidth, 44)) { BarStyle = UIBarStyle.Default, Translucent = true };
        var flexibleSpaceButton = new UIBarButtonItem(UIBarButtonSystemItem.FlexibleSpace);
        var doneButton = new UIBarButtonItem(UIBarButtonSystemItem.Done, (sender, e) => pickerTextField.ResignFirstResponder());
        doneButton.Clicked += (object sender, EventArgs e) =>
        {
            pickerTextField.Text = MyModel.SelectedItem;
        };
        pickerToolBar.SetItems(new[] { flexibleSpaceButton, doneButton }, false);

        pickerTextField.InputView = picker;
        pickerTextField.InputAccessoryView = pickerToolBar;
    }

字符串
theData列表包含在选择器中被截断的字符串。有没有一种方法可以改变字体大小,使他们适合也文本颜色?

qlvxas9a

qlvxas9a1#

您可以覆盖UIPickerViewModel中的GetView方法。

public override UIKit.UIView GetView(UIKit.UIPickerView pickerView, nint row, nint component, UIKit.UIView view)
{
    
    var pickerLabel = view as UILabel;
    if (pickerLabel == null)
    {
        pickerLabel = new UILabel();
        pickerLabel.Font = UIFont.SystemFontOfSize(5);
        pickerLabel.TextColor = UIColor.Red;
    }
    // you should again give the value to the pickerLabel Text based on the row or component of your picker
    if (component = 0)        
    {
        pickerLabel.Text = names[row];
    }
    else
    {
        pickerLabel.Text = row.ToString()        
    }

    return pickerLabel;
}

字符串
有关更多信息,您可以参考Picker control in Xamarin.iOS和示例代码:PickerControl

相关问题