【问题标题】:Set Xamarin UISearchBar UITextField Font Size / Colour + Background设置 Xamarin UISearchBar UITextField 字体大小/颜色 + 背景
【发布时间】:2015-12-29 17:52:27
【问题描述】:

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

  • 字体类型/大小
  • 字体颜色
  • 文本字段背景颜色,所以搜索栏都是一种颜色

认为看到 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 ...

我将为我刚刚遇到的一个问题提供一个入门答案,但有兴趣了解其他用户如何解决搜索栏的样式问题。

【问题讨论】:

    标签: xamarin xamarin.ios


    【解决方案1】:
    _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
    

    【讨论】:

      猜你喜欢
      • 2018-04-20
      • 2014-04-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-01-09
      • 2021-03-23
      • 2013-03-23
      • 2011-11-07
      相关资源
      最近更新 更多