2016-10-11 12 views

答えて

1

は関与いくつかのコードでこれを達成する方法があるが、 Interface Builderの...で直接これを実行する方法はありません。

サブビューは再帰的に見る必要があります(サブビューはそれ以上のサブビューを持つことができるので)、テキスト要素を追加する必要があります。

がここにいるの私の仕事だ、スウィフト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 
} 

スウィフトに、普遍のスタイルを適用した例:

// 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; 
} 

これは確かにあまりにも美しいものではありませんが、それは私が仕事を完了するために考えることができる唯一の方法です。

+0

objective-Cのバリアントを書いてください、そして、バウンティはあなたのものです:) –

+0

@NikKov Obj-Cメソッドで編集しました。 –

0

解決策もありますが、上記の「自動化」ソリューションよりも手作業が多少あります。

UILabel、UITextFieldなどをサブクラス化し、そのクラスのデフォルトフォントをinitに設定し、Interface Builderでそれぞれのオブジェクトを選択し、必要に応じてサブクラスを設定します。

これは私のこのような状況での私の好みの解決策ではないことを考えれば、私は他の答えのために行った詳細には入りません。しかし、あなたがこの解決策に興味があるなら、スウィフト1日からUILabelのサブクラス化についてhere's a helpful linkについて。それ以来、物事は少し変わったと言いますが、あなた自身がそれを理解することはかなり簡単です。

重要事項:インターフェイスビルダには、スタイルを普遍的に設定するためのこれらの解決策はありません。これはランタイム設定なので、ビルド時に設定が正しく設定されていることを確認するだけで済みます。

個人的には、これらのオプションのいずれかを必要とする動的な状況がない限り、手動でこれらのソリューションのいずれかを手動で設定することをおすすめします。

関連する問題