2013-10-12 1 views
31

내 textViews의 형식은 iOS 6에서 정상적으로 작동했지만 더 이상 iOS 7에서는 작동하지 않습니다. 텍스트 키트의 경우 많은 내용이 변경되었습니다. 그것은 매우 혼란스러워졌습니다. 누군가가 이것을 간단하게 도와줌으로써 누군가를 도와 줄 수 있기를 바랍니다.iOS 7의 UITextView에서 속성이 지정된 텍스트의 색상과 정렬을 어떻게 설정할 수 있습니까?

내 정적 UITextView는 원래 textColortextAlignment 속성 값으로 지정되었습니다. 그런 다음 NSMutableAttributedString을 만들어 속성에 할당 한 다음 textView의 attributedText 속성에 할당했습니다. 정렬 및 색상이 더 이상 iOS 7에서 적용되지 않습니다.

어떻게 해결할 수 있습니까? 이러한 속성이 효과가 없다면 더 이상 존재하지 않는 이유는 무엇입니까?

UITextView *titleView = [[UITextView alloc]initWithFrame:CGRectMake(0, 90, 1024, 150)]; 
titleView.textAlignment = NSTextAlignmentCenter; 
titleView.textColor = [UIColor whiteColor]; 

NSMutableAttributedString *title = [[NSMutableAttributedString alloc]initWithString:@"Welcome"]; 
UIFont *font = [UIFont fontWithName:@"Avenir-Light" size:60]; 
[title addAttribute:NSParagraphStyleAttributeName value:font range:NSMakeRange(0, title.length)]; 
titleView.attributedText = title; 

[self.view addSubview:titleView]; 

답변

62

호기심, 속성은 UILabel에 대한 고려하지만, UITextView

에 대해 왜 그냥 비슷한 기인 문자열로 색상 및 정렬 속성을 추가하지 않습니다하지 : 여기에 텍스트 뷰의 생성입니다 당신이 폰트로하는 방식? 같은

뭔가 :

NSMutableAttributedString *title = [[NSMutableAttributedString alloc]initWithString:@"Welcome"]; 
UIFont *font = [UIFont fontWithName:@"Avenir-Light" size:60]; 
[title addAttribute:NSFontAttributeName value:font range:NSMakeRange(0, title.length)]; 

//add color 
[title addAttribute:NSForegroundColorAttributeName value:[UIColor whiteColor] range:NSMakeRange(0, title.length)]; 

//add alignment 
NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init]; 
[paragraphStyle setAlignment:NSTextAlignmentCenter]; 
[title addAttribute:NSParagraphStyleAttributeName value:paragraphStyle range:NSMakeRange(0, title.length)]; 

titleView.attributedText = title; 

편집 : 다음 속성과 작동이 방법을 변경 먼저 텍스트를 지정합니다.

UITextView *titleView = [[UITextView alloc]initWithFrame:CGRectMake(0, 90, 1024, 150)]; 

//create attributed string and change font 
NSMutableAttributedString *title = [[NSMutableAttributedString alloc]initWithString:@"Welcome"]; 
UIFont *font = [UIFont fontWithName:@"Avenir-Light" size:60]; 
[title addAttribute:NSFontAttributeName value:font range:NSMakeRange(0, title.length)]; 

//assign text first, then customize properties 
titleView.attributedText = title; 
titleView.textAlignment = NSTextAlignmentCenter; 
titleView.textColor = [UIColor whiteColor]; 
+0

이렇게하면 NSFontAttributeName 특성이 더 이상 적용되지 않는 것 같습니다. 괜찮아요, 색상은 괜찮지 만 글꼴 크기는 시스템 크기 인 것 같습니다. 난 다른 모든 속성을 먼저 할당 한 후 글꼴 특성을 적용하려고 시도하고 이상하게도 응용 프로그램이 '캐치되지 않은 예외로 인해 응용 프로그램 종료' 'NSInvalidArgumentException', 이유 : '- [UICTFont textBlocks] : 인스턴스 0x8b1d1c0'로 전송 된 인식 할 수없는 선택기와 충돌합니다. – Joe

+0

두 번째 대안 (텍스트를 먼저 할당 한 다음 속성을 수정)을 언급한다고 가정하지만,이 예에서는 글꼴 사용자 정의를 포함하지 않았으므로 전체 예제를 포함하도록 수정합니다. – jlhuertas

+0

두 코드 샘플 모두 동일한 결과를 산출합니다. 따라서 UITextView의 속성을 할당하는 것은 필요하지 않은 것 같습니다. 네가 그걸 넣을 이유가 있었 니? – Joe