2014-02-19 7 views
2

모든 예제는 NSMutableAttributedString을 텍스트보기/편집과 관련된 텍스트와 속성을 유지하기위한 "백킹 저장소"로 보여줍니다. std :: string과 같은 alternate 나 데이터베이스의 내용을 사용하려면 어떻게해야합니까? 테스트와 마찬가지로, 서브 클래스를 생성하고 필수 메소드를 오버로드 할 때 기본 값을 반환하도록 하드 코드했습니다. 그러나 iPhone 5 장치에서 실행할 때 홈 버튼을 누를 때까지 검은 색 화면 만 표시됩니다. 시스템은 지속적으로 attributesAtIndex : location : effectiveRange : range :를 호출하며 CPU 사용량 수준은 100 %까지 올라가고 App은 절대 수행하지 않습니다. 그것은 문자열 호출을 수행합니다 한 번 방법을, 그러나 단지 attributesAtIndex를 호출 유지 : 위치 : effectiveRange : 범위NSTextStorage를위한 대체 백킹 저장소

예 :

@implementation MyTextStorage 
{ 
} 

- (id)init 
{ 
    self = [super init]; 

    if (self) 
    { 
    } 

    return self; 
} 


#pragma mark - Reading Text 

- (NSString *)string 
{ 
    return @"Static"; 
} 

- (NSDictionary *)attributesAtIndex:(NSUInteger)location effectiveRange:(NSRangePointer)range 
{ 
    return @{NSFontAttributeName: [UIFont fontWithName:@"Noteworthy-Bold" size:36] } ; 
} 

#pragma mark - Text Editing 

- (void)replaceCharactersInRange:(NSRange)range withString:(NSString *)str 
{ 
    // Empty - Don't allow editing 
} 

- (void)setAttributes:(NSDictionary *)attrs range:(NSRange)range 
{ 
    // Empty - Don't allow editing 
} 

- (void)processEditing 
{ 
    [super processEditing]; 
} 




@end 

를 그리고 여기이되는 설정 방법입니다 :

// property 
    self.textStorage = [MyTextStorage new]; 

    // Create layout manager, and attach text storage to layout manager. 
    NSLayoutManager* layoutManager = [[NSLayoutManager alloc] init]; 
    [self.textStorage addLayoutManager: layoutManager]; 

    // Create text container and attach to layout manager 
    CGSize size = self.view.bounds.size; 
    size.height = size.height/2; 

    NSTextContainer* textContainer = [[NSTextContainer alloc] initWithSize: size]; 
    [layoutManager addTextContainer: textContainer]; 

    // Create text view, given text container. 
    CGRect frame1 = CGRectMake(0, 20, size.width, size.height); 
    UITextView *tv1 = [[UITextView alloc] initWithFrame:frame1 textContainer: textContainer]; 
    [self.view addSubview: tv1]; 

NSTextStorage가 클래스 클러스터라는 것을 이해합니다.이 클래스 클러스터는 추상 클래스 팩토리임을 의미합니다. 나는 왜 내가 왜 다른 "후원 가게"를 사용할 수 없는지 이해하지 못한다. 내 계획은 std :: string (내 데이터가 어디에서 왔는지 이유 때문에)을 사용하고 상수 코드 스타일 (예 : 위 코드 에서처럼)을 반환하는 것이 었습니다. 나는 Mac OS X 문서를 포함하여 NSTextStorage, NSLayoutManager, NSTextContainer 및 NSTextView에 관한 모든 것을 읽을 수있었습니다 (iOS에서이 작업을 수행하고 있음에도 불구하고). 감사!

답변

1

NSRangePointer은 기본적으로 NSRange에 대한 포인터입니다. null이 아닌 경우 메소드를 종료하기 전에 설정해야합니다. 이와 같이 :

- (NSDictionary *)attributesAtIndex:(NSUInteger)location effectiveRange:(NSRangePointer)range { 
    if(range != NULL){ 
     range->location = 0; 
     range->length = self.string.length; 
    } 
    return @{NSFontAttributeName: [UIFont fontWithName:@"Noteworthy-Bold" size:36] } ; 
} 
+0

죄송합니다.이 문제를 테스트하는 데 시간이 걸립니다. 정말 고마워. – PaulPerry