xip을 만들 때 UILabel, UITextView, UITextField (xib)의 텍스트 요소에 기본 글꼴을 설정할 수 있습니까? 이제 시스템 17.0입니다.
Xcode. Xib. 텍스트 요소의 기본 글꼴 설정
답변
관련된 몇 가지 코드로이 작업을 수행 할 수있는 방법이 하지만 인터페이스 빌더 ... 직접이 작업을 수행 할 수있는 방법이 없습니다.
하위 뷰가 그 자체로 더 많은 하위 뷰를 가질 수 있으므로 하위 뷰를 재귀 적으로보고 텍스트 요소를 추가해야합니다.
여기 스위프트 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;
}
확실히 예쁘지는 않지만 내가 할 수있는 유일한 방법입니다.
다른 가능한 솔루션이 있지만 위의 "자동화 된"솔루션보다 약간 수동 작업입니다.
UILabel, UITextField 등을 서브 클래 싱하고 해당 클래스의 기본 글꼴을 init
에 설정 한 다음 Interface Builder에서 해당 객체를 선택하고 필요에 따라 하위 클래스를 설정합니다.
이것이이 상황에서 필자가 선호하는 해결책이 아니기 때문에 나는 다른 대답에 대해 자세히 설명하지 않을 것입니다. 그러나이 솔루션에 대해 궁금한 점이 있으시면 here's a helpful link에 대해 Swift 1 일부터 UILabel
하위 클래스에 대해 관심이 있습니다. 그 이후로 상황이 조금 바뀌 었다고 말할 것입니다. 그러나 당신이 직접 알아낼 수 있어야합니다.
중요 사항 : 보편적으로 스타일을 설정하는 이들 솔루션 중 어느 것도 Interface Builder에 나타나지 않습니다. 이는 런타임 설정이므로 빌드시 설정이 올바르게 설정되었는지 확인해야합니다.
이러한 옵션 중 하나가 필요한 동적 인 상황이 아니라면 개인적으로는 수동으로 설정하거나 수동으로 설정하는 것이 좋습니다.
- 1. Xcode 6.1 내비게이션 막대 텍스트 글꼴 설정
- 2. 기본 Xcode 4.5 글꼴
- 3. simple_form - "select"요소의 기본 텍스트 설정
- 4. xcode 프로젝트의 기본 글꼴 스타일, 색상 및 크기 설정
- 5. Matlab 설정 기본 글꼴
- 6. 설정 텍스트 글꼴
- 7. SWT 셸의 기본 글꼴 설정
- 8. C#의 기본 글꼴 설정
- 9. PowerPoint 테이블의 기본 글꼴 설정
- 10. NIB/XIB 파일의보기 크기 설정
- 11. 다양한 화면 크기의 기본 글꼴 크기에 상대적인 글꼴 크기 설정
- 12. Xcode에서 기본 Xib 파일 변경
- 13. 서식있는 텍스트 필드의 기본 글꼴
- 14. 신발 요소의 기본 글꼴을 설정 하시겠습니까?
- 15. R 기본 그래픽 : 텍스트/그래픽 요소의 크기를 설정하는 방법은 무엇입니까?
- 16. 하위 XIB 기본 XIB 엽니 다
- 17. Xcode 스타일의 글꼴 선택 도구
- 18. Xcode Storyboard and xib connection
- 19. Xcode 및 XIB 자체 설명서
- 20. Xcode 4 및 xib 컨트롤러
- 21. 원하는 Xib 대신 기본 Xib가로드됩니까?
- 22. xcode 8.1 xib 편집 중단, 텍스트 편집기 해결 방법?
- 23. 기본 글꼴 기능 설정 방법은 무엇입니까?
- 24. 안드로이드 애플 리케이션의 기본 글꼴 크기 설정
- 25. Blackberry 장치 - 설정/기본 글꼴 크기
- 26. 기본 글꼴 사용/강제 설정 방법
- 27. RTF 파일의 기본 글꼴 크기 설정
- 28. Xcode 6의 XIB 파일에서 현지화가 작동하지 않습니다.
- 29. Firemonkey에서 글꼴 설정
- 30. xcode 외부의 .xib 파일을 어떻게 수정합니까?
objective-C의 변형을 쓰십시오. 그리고 bounty는 당신의 것입니다 : –
@NikKov Obj-C 방법으로 편집했습니다. –