2016-10-11 2 views

답변

1

관련된 몇 가지 코드로이 작업을 수행 할 수있는 방법이 하지만 인터페이스 빌더 ... 직접이 작업을 수행 할 수있는 방법이 없습니다.

하위 뷰가 그 자체로 더 많은 하위 뷰를 가질 수 있으므로 하위 뷰를 재귀 적으로보고 텍스트 요소를 추가해야합니다.

여기 스위프트 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 
} 

... 그리고 목적의 방법 -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의 변형을 쓰십시오. 그리고 bounty는 당신의 것입니다 : –

+0

@NikKov Obj-C 방법으로 편집했습니다. –

0

다른 가능한 솔루션이 있지만 위의 "자동화 된"솔루션보다 약간 수동 작업입니다.

UILabel, UITextField 등을 서브 클래 싱하고 해당 클래스의 기본 글꼴을 init에 설정 한 다음 Interface Builder에서 해당 객체를 선택하고 필요에 따라 하위 클래스를 설정합니다.

이것이이 상황에서 필자가 선호하는 해결책이 아니기 때문에 나는 다른 대답에 대해 자세히 설명하지 않을 것입니다. 그러나이 솔루션에 대해 궁금한 점이 있으시면 here's a helpful link에 대해 Swift 1 일부터 UILabel 하위 클래스에 대해 관심이 있습니다. 그 이후로 상황이 조금 바뀌 었다고 말할 것입니다. 그러나 당신이 직접 알아낼 수 있어야합니다.

중요 사항 : 보편적으로 스타일을 설정하는 이들 솔루션 중 어느 것도 Interface Builder에 나타나지 않습니다. 이는 런타임 설정이므로 빌드시 설정이 올바르게 설정되었는지 확인해야합니다.

이러한 옵션 중 하나가 필요한 동적 인 상황이 아니라면 개인적으로는 수동으로 설정하거나 수동으로 설정하는 것이 좋습니다.

관련 문제