gpt4 book ai didi

代码。同上。为文本元素设置默认字体

转载 作者:行者123 更新时间:2023-12-04 17:56:39 26 4
gpt4 key购买 nike

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

最佳答案

没有办法直接在 Interface Builder 中执行此操作......但是有一种方法可以通过涉及一些代码来完成此操作。

您需要递归地查看 subview (因为 subview 本身可以有更多的 subview )并在遇到它们时添加文本元素。

这是我的工作,已在 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;
}

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

关于代码。同上。为文本元素设置默认字体,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39973827/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com