Change the font size and font style of UISearchBar iOS 7

后端 未结 9 834
借酒劲吻你
借酒劲吻你 2020-12-13 03:17

How can I change the font size and font style of UISearchBar in iOS 7?

UITextField *textField = [[searchBar subviews] objectAtIndex:1];
[textField setFont:[U         


        
相关标签:
9条回答
  • 2020-12-13 03:48

    appearanceWhenContainedIn: is deprecated in iOS9, use the following:

    [[UITextField appearanceWhenContainedInInstancesOfClasses:@[[UISearchBar class]]] setDefaultTextAttributes:@{ NSFontAttributeName: [UIFont fontWithName:@"FontName" size:14]}];
    
    0 讨论(0)
  • 2020-12-13 03:48
    • SWIFT
    • IOS 8

    For me, this worked using a recursive function to find the actual textfield.

    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) }
        }
    
    }
    }
    
    0 讨论(0)
  • 2020-12-13 03:49

    iOS 10 Swift 3:

    UITextField.appearance(whenContainedInInstancesOf: [UISearchBar.self]).defaultTextAttributes = [NSFontAttributeName:UIFont(name: "Avenir-Heavy", size: 22)!]
    
    0 讨论(0)
提交回复
热议问题