3
votes

Every example shows an NSMutableAttributedString as the "backing store" for keeping the text and attributes related to viewing/editing text. How can I use an alternate, such as std::string or maybe content from a database. Just as a test, I have created a subclass and hardcoded it to return default values when I overrode the required methods. However, when I run it on an iPhone 5 device, it just shows a black screen, until I hit the Home button. The system continuously calls attributesAtIndex:location:effectiveRange: range: and the CPU usage level goes up to 100% and the App never does anything. It does call the string: method once, but then just keeps calling the attributesAtIndex:location:effectiveRange:range

The example:

@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

And here is how it is being setup:

    // 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];

I understand NSTextStorage is a Class Cluster, which seems to mean that it is an abstract class factory. I really don't understand why I can't use another "backing store". My plan was to use a std::string (because of reasons of where my data is coming from) and then return a constant attribute style (like in the above code). I've been reading everything I can about NSTextStorage, NSLayoutManager, NSTextContainer and NSTextView, including mac OS X documents (even though I'm doing this on iOS). Thanks!

1

1 Answers

2
votes

NSRangePointer is basically a pointer to NSRange. You will need to set it before exit the method if it is not null. Like this:

- (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] } ;
}