0

현재 모든 버튼과 텍스트 필드에는 attributedText 값이 속성 문자열로 정의되어 있습니다.UIControl에 대한 NSAttributedString 텍스트 값 변경

간단한 UILabel의 경우를 생각해보십시오. 이 텍스트를 변경해야 할 때마다 UILabel (일부 사용자 작업에 따라), 나는 NSAttributedString에서 속성을 재정의해야합니다. 한 가지 방법은 필요할 때마다 이러한 속성을 생성하는 서브 루틴을 간단히 만드는 것입니다.하지만 편의 메소드가 필요한 여러 가지 레이블 (또는 속성이 지정된 문자열)이있을 수 있다는 점에서 우려가됩니다.

또 다른 하나는 단순히 text 필드를 변경하고 관찰자에게 이러한 속성을 추가하는 것일 수 있지만 그 양은 동일하고 더 복잡한 것입니다.

속성을 재정의하지 않고 위의 간단한 방법으로 달성 할 수 있습니까? NSAttributedString

카테고리, 카테고리 UILabel에, 또는 카테고리 NSDictionary에, 어쩌면 그들 중 혼합 한 가장 적합한 당신과 당신의 프로젝트를하는에 따르면 : 해리의 아이디어 @ 탐색

+1

카테고리는 어떻게됩니까? –

+0

당신이 무엇을 암시하는지 잘 모르겠습니다. 그게 어때? – p0lAris

+0

매번 속성을 재정의해야합니다. 주위에는 속성이 없습니다. 해리가 말하고자하는 것은 UILabel에 범주를 지정하면 코드 중복을 피하고 프로젝트를 통해 모든 UILabel의 특성있는 텍스트를 업데이트하는 편리한 방법을 폭로 할 수 있다는 것입니다. – Champoul

답변

1

, 여기에 몇 가지 아이디어가 있습니다. NSAttributedString의 카테고리를 UILabel 전에 우선적으로 사용하면 다른 유형의 객체 (예 : UITextView)에 대해 NSAttributedString 사용자 정의를 사용하려는 경우에 더 재미있을 수 있습니다.

좋은 시작 :

typedef enum : NSUInteger { 
    AttributeStyle1, 
    AttributeStyle2, 
} AttributeStyles; 

가능한 카테고리 방법 NSDictionary에 :

-(NSDictionary *)attributesForStyle:(AttributeStyles)style 
{ 
    NSDictionary *attributes; 
    switch(style) 
    { 
     case AttributeStyle1: 
      attributes = @{}//Set it 
      break; 
     case AttributeStyle2: 
      attributes = @{}//Set it 
      break; 
     default: 
      attributes = @{}//Set it 
      break; 
    } 
    return attributes; 
} 

분류 할 수 UILabel에 : NSAttributedString에 가능

-(void)setString:(NSString *)string withAttributes:(NSDictionary *)attributes 
{ 
    [self setAttributedText:[[NSAttributedString alloc] initWithString:string attributes:attributes]; 
} 

카테고리 :

,
-(NSAttributedString *)initWithString:(NSString *)string withStyle:(AttributedStyles)style 
{ 
    //Here, a mix is possible using the first method, or doing here the switch case 
    //Ex: return [[NSAttributedString alloc] initWithString:string attributes:[NSDictionary attributesForStyle:style]; 
    //And to use like this: [yourLabel setAttributedText:[[NSAttributedString alloc] initWithString:string withStyle:AttributeStyle1]; 
} 
+0

감사합니다. 나는이 모든 옵션을 실제로 탐구했다. 유일한 문제는 다양한 유형의 라벨/텍스트 필드가 있다는 것입니다. 즉, 다른 글꼴 등이 필요합니다. 나는 setter와 initializer에도 이것을 포함 할 수 있다고 생각한다. 공정! – p0lAris