【问题标题】:Change the font size and font style of UISearchBar iOS 7更改 UISearchBar iOS 7 的字体大小和字体样式
【发布时间】:2013-10-10 07:07:13
【问题描述】:

如何在 iOS 7 中更改 UISearchBar 的字体大小和字体样式?

UITextField *textField = [[searchBar subviews] objectAtIndex:1];
[textField setFont:[UIFont fontWithName:@"Helvetica" size:20]];

在 iOS 6 中工作,但在 iOS 7 中崩溃

【问题讨论】:

  • 错误是什么?并且永远不要深入研究标准 UI 组件的私有子视图结构。这些东西会改变并使您的代码中断。使用提供的 API 来做这些事情。
  • 请检查:stackoverflow.com/questions/19048766/… 我认为您尝试实现的目标是相同的。
  • @rmaddy 是正确的,我的Answer 中给出了安全方法的详细说明。

标签: objective-c uitextfield ios7 uisearchbar


【解决方案1】:

试试这个,它适用于 iOS 5.0 及更高版本:(也适用于 iOS 7)

- (void)viewDidLoad
{

[[UITextField appearanceWhenContainedIn:[UISearchBar class], nil] setFont:[UIFont fontWithName:@"Helvetica" size:20]];

}

适用于 iOS 8

- (void)viewDidLoad
    {

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

    }

【讨论】:

  • 像魅力一样工作..!
  • 我对此有一点疑问。我已经实现了相同的并且它正在工作。但是如果我导航到某个视图控制器并返回到同一个屏幕,字体将更改为默认值。有什么想法吗?
  • 在 iOS 8 上也是如此。第一次运行良好。但是当我导航到另一个屏幕并返回时,字体又回到了系统默认值。
  • developer.apple.com/library/ios/documentation/UIKit/Reference/… UISearchBar 没有来自文本字段的层次结构。
  • @jeet.chanchawat——没关系。外观是为作为 UISearchBar 的子视图的 UITextField 定义的,其中有一个。
【解决方案2】:

接受的答案在 iOS 7.1 上对我不起作用。我不得不把它改成这样:

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

【讨论】:

  • 这种方式克服了 Ganesh 评论中提到的 [在 iOS 8.0 上尝试过] 的问题(即仅使用 setFont 而不是 setDefaultTextAttribute 文本仅在第一个视图上更改,而不是在导航到问题中的屏幕)。
  • 这个答案更好。
  • 是的,在 iOS 8.1 上,这是唯一对我每次都有效的方法。
【解决方案3】:

iOS 10 Swift 3:

UITextField.appearance(whenContainedInInstancesOf: [UISearchBar.self]).defaultTextAttributes = [NSFontAttributeName:UIFont(name: "Avenir-Heavy", size: 22)!]

【讨论】:

    【解决方案4】:

    appearanceWhenContainedIn: 在 iOS9 中已弃用,请使用以下内容:

    [[UITextField appearanceWhenContainedInInstancesOfClasses:@[[UISearchBar class]]] setDefaultTextAttributes:@{ NSFontAttributeName: [UIFont fontWithName:@"FontName" size:14]}];
    

    【讨论】:

      【解决方案5】:

      正如 rmaddy 所说,您不应依赖标准 UI 组件的私有子视图结构。这些东西会改变并使您的代码中断。您应该使用提供的 API 来执行此操作。

      我认为更安全的方法是:

      [[UITextField appearanceWhenContainedIn:[UISearchBar class], nil] setFont:[UIFont systemFontOfSize:14]];
      

      或者你也可以使用这个示例代码:

      for(UIView *subView in searchBar.subviews) {
          if ([subView isKindOfClass:[UITextField class]]) {
              UITextField *searchField = (UITextField *)subView;
              searchField.font = [UIFont systemFontOfSize:14];
          }
      }
      

      上面的代码也更安全(至少与问题中提到的代码相比),因为它没有使用subviewsindex

      【讨论】:

      • 这里的第二种方法似乎并不那么安全。您仍然依赖子视图结构,因为您假设 UISearchBar 具有 UITextField 作为直接子视图。避免使用索引并不意味着避免使用私有子视图结构。
      • @rickerbh:是的,是的。实际上,即使我认为 OP 应该使用我在回答中提到的第一种方法。
      【解决方案6】:

      对于那些正在寻找工作 Swift 版本的人,I've adapted this answer。 Swift 目前不支持 Objc 可变参数方法,因此不能直接与上述方法一起使用。我们可以通过创建一个不使用可变参数并调用我们需要的对象的 Objective-c 类别来解决这个问题:

      // UIAppearance+Swift.h
      @interface UIView (UIViewAppearance_Swift)
      // appearanceWhenContainedIn: is not available in Swift. This fixes that.
      + (instancetype)my_appearanceWhenContainedIn:(Class<UIAppearanceContainer>)containerClass;
      @end
      

      // UIAppearance+Swift.m
      @implementation UIView (UIViewAppearance_Swift)
      + (instancetype)my_appearanceWhenContainedIn:(Class<UIAppearanceContainer>)containerClass {
          return [self appearanceWhenContainedIn:containerClass, nil];
      }
      @end
      

      请务必在您的桥接头中使用#import "UIAppearance+Swift.h"

      然后只需调用:

      UITextField.my_appearanceWhenContainedIn(UISearchBar.self).font = UIFont.systemFontOfSize(14.0)
      

      【讨论】:

        【解决方案7】:

        对于 Swift 4,您需要引用 NSAttributedStringKey 枚举:

        UITextField.appearance(whenContainedInInstancesOf: [UISearchBar.self])
                .defaultTextAttributes = [NSAttributedStringKey.font.rawValue: UIFont(...)]
        

        【讨论】:

        • 在 iOS11(Swift 4) 中加速工作。特别是 .rawValue 是我所缺少的。
        • 类型“NSAttributedStringKey”(又名“NSString”)没有成员“字体”
        【解决方案8】:

        在 Swift 5 中

        @Piotr Tomasik 的代码在位更改后仍在 swift 5 中工作

        UITextField.appearance(whenContainedInInstancesOf: [UISearchBar.self]).defaultTextAttributes = [NSAttributedString.Key.font:UIFont(name: "poppins", size: 13)!]
        

        【讨论】:

          【解决方案9】:
          • 斯威夫特
          • IOS 8

          对我来说,这使用递归函数来查找实际的文本字段。

          extension UIView {
          
          func recursive_applyTheme_Search(
              #dynamicTextStyle: NSString,
              bgColor: UIColor,
              cursorColor: UIColor,
              textColor: UIColor,
              placeholderTextColor: UIColor,
              borderColor: UIColor,
              borderWidth: CGFloat,
              cornerRadius: CGFloat) {
          
              for subview in self.subviews
              {
                  if subview is UITextField {
          
                      (subview as! UITextField).applyThemeForSearchBar(
                          dynamicTextStyle: dynamicTextStyle,
                          bgColor: bgColor,
                          cursorColor: cursorColor,
                          textColor: textColor,
                          placeholderTextColor: placeholderTextColor,
                          borderColor: borderColor,
                          borderWidth: borderWidth,
                          cornerRadius: cornerRadius)
                  }
                  else { subview.recursive_applyTheme_Search(
                          dynamicTextStyle: dynamicTextStyle,
                          bgColor: bgColor,
                          cursorColor: cursorColor,
                          textColor: textColor,
                          placeholderTextColor: placeholderTextColor,
                          borderColor: borderColor,
                          borderWidth: borderWidth,
                          cornerRadius: cornerRadius) }
              }
          
          }
          }
          

          【讨论】:

          • 另外,大部分 WWDC 视频都建议字体应该符合 Dynamic Type,使用 let font = UIFont.preferredFontForTextStyle(UIFontTextStyleBody)
          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2016-06-07
          • 1970-01-01
          • 2011-06-14
          • 1970-01-01
          • 2014-03-17
          • 2011-11-03
          相关资源
          最近更新 更多