Xcode。Xib。为文本元素设置默认字体

Xcode。Xib。为文本元素设置默认字体,xcode,xib,Xcode,Xib,在创建文本元素(如xib中的UILabel、UITextView、UITextField)时,如何为它们设置默认字体?现在是system 17.0。 在Interface Builder中没有直接执行此操作的方法…但是有一种方法可以通过涉及一些代码来完成此操作 您需要递归地查看子视图(因为子视图本身可以有更多的子视图),并在遇到它们时添加文本元素 以下是我在这方面的工作,已在Swift 3.0.2中验证: /// Recursively checks for UILabels, UITextFi

在创建文本元素(如xib中的UILabel、UITextView、UITextField)时,如何为它们设置默认字体?现在是system 17.0。

在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
}
…以及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;
}

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

还有另一种可能的解决方案,但它比上面更“自动化”的解决方案需要更多的体力劳动

这就是将UILabel、UITextField等子类化,并在
init
中为该类设置默认字体,然后在Interface Builder中选择它们各自的对象,并根据需要设置子类

考虑到这不是我在这种情况下首选的解决方案,我不会像在另一个答案中那样详细讨论它。但是,如果您对这个解决方案感兴趣,可以从Swift 1天中对
UILabel
进行子类化。我要说的是,从那时起,情况发生了一些变化,但你自己解决这个问题应该相当简单

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


就个人而言,我建议只手动设置它们,而不是使用这两种解决方案,除非您遇到需要这两种方案中的任何一种的动态情况。

为objective-C编写一个变体,赏金就是您的:)@NikKov使用Obj-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;
}