【问题标题】:How to change properties of UITextField inside UISearchBar如何在 UISearchBar 中更改 UITextField 的属性
【发布时间】:2014-03-11 19:27:25
【问题描述】:

基本上我正在访问这个网址

UITextField * textField= self.searchBar.textField;

我们只是说函数 .textField 确实获得了 textField。我已经验证了好几次了。

textField.adjustsFontSizeToFitWidth =true;
textField.minimumFontSize =10;
textField.enablesReturnKeyAutomatically =NO;

我跟着上面的代码。

当文本太长时,self.searchBar 中的 UITextField 仍然不会改变字体大小。

我想知道为什么。

【问题讨论】:

  • 尝试继承MySearchBar : UISearchBar并在layoutSubviews方法中自定义。
  • 我应该怎么做?

标签: ios objective-c ios7 uitextfield uisearchbar


【解决方案1】:

试着让你的文本字段像这样......

UITextField *textField = [searchbar valueForKey:@"_searchField"];

And then customize it according to you.

textField.adjustsFontSizeToFitWidth =true;
textField.minimumFontSize =10;
textField.enablesReturnKeyAutomatically =NO;

【讨论】:

  • 我知道如何获取文本字段。例如,我可以让它右对齐。但是,我无法使文本字段中的字体自动调整。
【解决方案2】:

当您想要更改包含在其他类实例中的某个类实例时,您应该尝试使用UIAppearance 实例方法。 (寻找视图层次结构是解决方法)。

[[UITextField appearanceWhenContainedIn:[UISearchBar class], nil] setTextColor:[UIColor blueColor]];

这里在documentation by Apple中有很好的解释。

【讨论】:

    【解决方案3】:

    试试这段代码..这样你就可以在 UISearchBar 中更改和管理 UITextField 的属性

    for (UIView *searchBarSubview in [searchbar subviews])
    {
        if ([searchBarSubview conformsToProtocol:@protocol(UITextInputTraits)])
        {
            @try
            {
                [(UITextField *)searchBarSubview setBorderStyle:UITextBorderStyleRoundedRect];
                 [[UITextField appearanceWhenContainedIn:[UISearchBar class], nil] setFont:[UIFont fontWithName:@"jots" size:10]];
    
                [[UITextField appearanceWhenContainedIn:[UISearchBar class], nil] adjustsFontSizeToFitWidth];
    
                [[UITextField appearanceWhenContainedIn:[UISearchBar class], nil] enablesReturnKeyAutomatically];
    
            }
            @catch (NSException * e)
            {
                // ignore exception
            }
        }
    }
    

    希望对你有帮助..

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-10-13
      • 2013-10-01
      • 2014-12-14
      • 2012-06-02
      相关资源
      最近更新 更多