Xcode中。厦门国际银行。设置文本元素的默认字体

时间:2016-10-11 09:05:31

标签: xcode xib

当我创建它们时,如何为xib中的UILabel,UITextView,UITextField等文本元素设置默认字体?现在是系统17.0 enter image description here

2 个答案:

答案 0 :(得分:1)

无法直接在Interface Builder中执行此操作... 有一种方法可以通过一些代码实现此目的。

您需要递归查看子视图(因为子视图本身可以包含更多子视图),并在遇到它们时添加文本元素。

以下是我的工作,经过Swift 3.0.2验证:

/// Recursively checks for UILabels, UITextFields, and UITextViews in a parent UIView and its subviews.
/// - parameter view: The UIView which may contain text elements for further, unified configuration.
/// - returns: A dictionary with UILabels, UITextFields, and UITextViews in their own arrays.
func textElementsInView(_ view: UIView) -> [String : Array<UIView>] {

    // Get the view's subviews.
    let subviews = view.subviews

    // Set up empty arrays for labels, text fields, and text views.
    var labels : [UILabel] = []
    var textFields : [UITextField] = []
    var textViews : [UITextView] = []

    // Check through the subviews in the given view.
    for subview in subviews {

        if subview.isKind(of: UILabel.classForCoder()) {

            // The subview is a label. Add it to the labels array.
            labels.append(subview as! UILabel)

        } else if subview.isKind(of: UITextField.classForCoder()) {

            // The subview is a text field. Add it to the textFields array.
            textFields.append(subview as! UITextField)

        } else if subview.isKind(of: UITextView.classForCoder()) {

            // The subview is a text view. Add it to the textViews array.
            textViews.append(subview as! UITextView)

        } else {

            // The subview isn't itself a text element...but it could have some in it!
            // Let's check it using this very function.
            let elements = textElementsInView(subview)

            // Add the labels...
            let subviewLabels = elements["labels"] as! [UILabel]
            labels.append(contentsOf: subviewLabels)

            // ...and the text fields...
            let subviewTextFields = elements["textFields"] as! [UITextField]
            textFields.append(contentsOf: subviewTextFields)

            // ...and the text views, to their respective arrays.
            let subviewTextViews = elements["textViews"] as! [UITextView]
            textViews.append(contentsOf: subviewTextViews)

        }
    }

    // Once we're done with all that, set up our elements dictionary and return it.
    let elements : [String : Array<UIView>] = ["labels" : labels, "textFields" : textFields, "textViews" : textViews]
    return elements
}

在Swift中普遍应用样式的一个例子:

// Get the text elements in the given view.
let textElements = textElementsInView(view)

// Get the labels from the textElements dictionary.
let labels = textElements["labels"] as! [UILabel]

// Apply styles to any label in the labels array, all together.
for label in labels {
    label.font = UIFont.systemFont(ofSize: 24, weight: UIFontWeightBlack)
    label.textColor = UIColor.black
}

......以及Objective-C中的方法:

- (NSDictionary*)textElementsInView: (UIView*)view {

    // First, set up mutable arrays for our text element types.
    NSMutableArray *labels = [NSMutableArray new];
    NSMutableArray *textFields = [NSMutableArray new];
    NSMutableArray *textViews = [NSMutableArray new];

    // And get an array with our subviews.
    NSArray *subviews = [view subviews];

    // Loop through the subviews in the subviews NSArray
    for(UIView* subview in subviews) {
        if ([subview classForCoder] == [UILabel class]) {
            // If the subview is a UILabel, add it to the labels array.
            [labels addObject:subview];
        } else if ([subview classForCoder] == [UITextField class]) {
            // If the subview is a UITextField, add it to the textFields array.
            [textFields addObject:subview];
        } else if ([subview classForCoder] == [UITextView class]) {
            // If the subview is a UITextView, add it to the labels array.
            [textViews addObject:subview];
        } else {
            // Try running through this function for the view if none of the above matched.
            NSDictionary *subviewTextElements = [self textElementsInView:subview];

            // Get any labels in the subview and add them to the labels array.
            NSArray *subviewLabels = [subviewTextElements objectForKey:@"labels"];
            [labels addObjectsFromArray:subviewLabels];
            // Do the same for UITextFields...
            NSArray *subviewTextFields = [subviewTextElements objectForKey:@"textFields"];
            [labels addObjectsFromArray:subviewTextFields];
            // ...and UITextViews.
            NSArray *subviewTextViews = [subviewTextElements objectForKey:@"textViews"];
            [labels addObjectsFromArray:subviewTextViews];
        }
    }

    // After all that's done, create a dictionary and return it.
    NSDictionary *textElements = @{
                                   @"labels" : labels,
                                   @"textFields" : textFields,
                                   @"textViews": textViews
    };
    return textElements;
}

它当然不是很漂亮,但这是我能想到完成工作的唯一方法。

答案 1 :(得分:0)

还有另一种可能的解决方案,但它比上面更“自动化”的解决方案更省力。

这是继承UILabel,UITextField等的子类,并在init中为该类设置默认字体,然后在Interface Builder中选择各自的对象并根据需要设置子类。

鉴于这不是我对这种情况的首选解决方案,我不会详细介绍我为其他答案所做的细节。但是如果你对这个解决方案感到好奇,here's a helpful link关于从Swift 1天中继承UILabel的问题。我会说从那时起事情发生了一些变化,但是你自己弄清楚它应该是相当简单的。

一个重要的注意事项:通用设置样式的这些解决方案都不会出现在Interface Builder中。这些是运行时设置,因此您只需确保在构建时正确设置设置。

就我个人而言,我建议您手动设置这些解决方案,除非您有一些需要这些选项的动态情况。