设置Xamarin UISearchBar UITextField字体大小/颜色+背景

时间:2015-12-29 17:52:27

标签: xamarin xamarin.ios

有很多关于如何在UISearchbar中访问UITextField的Objective-c变体,因此您可以比暴露的SearchBar方法更具体地设置样式,即我们想要更改 -

  • 字体类型/尺寸
  • font color
  • 文本字段背景颜色,因此搜索栏都是一种颜色

认为看到Xamarin解决方案很好,因为某些obj-c技术似乎没有(我可能只是缺少代码)来翻译 -

例如,在 -

Change the font size and font style of UISearchBar iOS 7

- (void)viewDidLoad
{

[[UITextField appearanceWhenContainedIn:[UISearchBar class], nil] setDefaultTextAttributes:@{
        NSFontAttributeName: [UIFont fontWithName:@"Helvetica" size:20],
  }];

}

看起来像是一个胜利者,但不确定是否可以将其转换为Xamarin,因为这些属性并非“看似”暴露 -

UITextField.AppearanceWhenContainedIn(typeof (UISearchBar))).HowToSetFont ...

我会为我刚刚遇到的一个做出初步答案,但有兴趣了解其他用户如何解决搜索栏的样式。

1 个答案:

答案 0 :(得分:4)

_filterSearchBar = new UISearchBar
{
    BarTintColor = UIColor.Red,     // We want a red search bar with text field background same as outer "margin"
    TintColor = UIColor.White,
    SearchBarStyle = UISearchBarStyle.Default,
    BackgroundImage = UIImage.FromFile("transparent1x1.png")     // "trick" to ensure background is clean  
};

// Main Part - Use KVO to access text field
var uiTextField = (UITextField)_filterSearchBar.ValueForKey(new NSString("_searchField"));

uiTextField.Font = YourFontHere;
uiTextField.TextColor = UIColor.White;
uiTextField.BackgroundColor = UIColor.Red;            

// Placeholder colour is white 
uiTextField.AttributedPlaceholder = new NSAttributedString("SearchBarPlaceholder", null, UIColor.White);

// We still have not sorted spyglass icon colour et al to white but can try set icon image later