代码之家  ›  专栏  ›  技术社区  ›  Prescott Chartier

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

  •  0
  • Prescott Chartier  · 技术社区  · 2 年前

    我使用一个特定的选择器设置来模仿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 列表包含在选择器中被截断的字符串。有没有办法可以更改字体大小以使其适合,也可以更改文本颜色?

    0 回复  |  直到 2 年前
        1
  •  0
  •   Liqun Shen-MSFT    2 年前

    您可以覆盖 GetView 方法 UIPickerViewModel .

    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 .