0

프로그래밍 방식으로 특성이 지정된 문자열을 만들 수 있기 때문에 UIView 하위 클래스에서 IB_DESIGNABLE을 사용하고 있지만 인터페이스 작성기 (서식을 보려면 응용 프로그램을 실행하지 않아도 됨)에서이 코드를 표시해야합니다.IB_DESIGNABLE 속성이 지정된 문자열 사용

나는

- (void)prepareForInterfaceBuilder; 

에 내 코드를 삽입 할 수있는 들었다 그리고 그것은 어느 정도 작동합니다. 인터페이스 빌더에 나타납니다. 하지만 APP를 실행하면 서식이 손실됩니다. 인터페이스 빌더에는 계속 표시되지만 앱에는 표시되지 않습니다.

아래의 방법을 사용하여 Attributed String을 (를) 만들려고했지만 인터페이스 작성기 나 응용 프로그램이 실행되지 않을 때 나타납니다.

- (instancetype)initWithFrame:(CGRect)frame; 
- (void)drawRect:(CGRect)frame; 

그러나 APP에서는 렌더링 할 메소드를 찾았지만 인터페이스 빌더에서는 표시하지 않습니다.

- (instancetype)initWithCoder:(NSCoder *)aDecoder; 

이렇게 말하면 솔루션은 두 가지 방법 만 사용하는 것입니다. 그러나 나는 두 세계의 장점을 취할 수있는 또 다른 방법이 있는지 궁금해하고있었습니다.

또한 코드 조각을 추가하여 내가하는 일을 보여주고이 쿼리에 대한 완성도를 제공합니다.

IB_DESIGNABLE 
@interface FooLabel1 : UILabel 
@property (nonatomic, copy) IBInspectable NSAttributedString *attributedText; 
@end 

@implementation FooLabel1 

- (instancetype)initWithCoder:(NSCoder *)aDecoder { 
    if (self = [super initWithCoder:aDecoder]) { 
     [self localizeattributedString]; 
    } 
    return self; 
} 

- (void)localizeattributedString { 
    NSMutableAttributedString *mat = [[NSMutableAttributedString alloc] initWithString:NSLocalizedString(
      @"Hello" 
      @"Darkness my old friend" 
     , nil) attributes:@{ 
     NSForegroundColorAttributeName : [UIColor orangeColor], 
    }]; 
    [mat appendAttributedString:[[NSAttributedString alloc] initWithString:NSLocalizedString(@"world!", nil) attributes:@{ 
      NSFontAttributeName : [UIFont boldSystemFontOfSize:60], 
      NSForegroundColorAttributeName : [UIColor blueColor] 
    }]]; 
    self.attributedText = [mat autorelease]; 
} 

- (void)prepareForInterfaceBuilder { 
    [self localizeattributedString]; 
} 

@end 

답변

0

문제의 해결 방법은 올바르게 작동하지만 잘못된 이유로 인해 발생합니다. 다음과 같이 구성 방법 (localizeattributedString)을 initWithCoder:initWithFrame:에서 호출하고 싶습니다.

prepareForInterfaceBuilderIB_DESIGNABLE보기를 렌더링 컨텍스트에서 호출되는 특별한 방법이다. 예를 들어 일반적으로 사용자 정의보기가 웹 서비스에서 데이터의 일부로 가져 오는 경우 prepareForInterfaceBuilder에 대신 예제 데이터를 제공합니다.

@implementation FooLabel1 

- (instancetype)initWithCoder:(NSCoder *)aDecoder { 
    if (self = [super initWithCoder:aDecoder]) { 
     [self localizeattributedString]; 
    } 
    return self; 
} 

- (instancetype)initWithFrame:(CGRect)frame { 
    if (self = [super initWithFrame:frame]) { 
     [self localizeattributedString]; 
    } 
    return self; 
} 

- (void)localizeattributedString { 
    ... 
} 

@end