2

I am using a UITextView that gets its attributedText set from an rtf file to show T&C, Privacy pages etc.

The rtf file is already formatted so it contains differently sized titles, paragraphs etc. Is there a way of saying something like "make the font size everywhere 3pt larger"? Surely I'm not going to specify a character range for every sentence and title in the file and manually set the font size.

Here's what I'm doing:

@implementation RTFViewController

- (instancetype)initWithRTFURL:(NSURL *)url title:(NSString *)title {
    self = [super init];

    if (self) {
        self.navigationItem.title = title;
        self.text = [[NSAttributedString alloc] initWithFileURL:url
                                                        options:@{NSDocumentTypeDocumentAttribute:NSRTFTextDocumentType}
                                             documentAttributes:nil
                                                          error:nil];
    }

    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];

    self.textView = [UITextView newAutoLayoutView];
    self.textView.editable = NO;
    self.textView.textContainerInset = UIEdgeInsetsMake(20.0f, 20.0f, 20.0f, 20.0f);
    self.textView.dataDetectorTypes = UIDataDetectorTypeAll;

    self.textView.attributedText = self.text;
    [self.view addSubview:self.textView];
}

@end

EDIT

I'm able to achieve what I want by looping through the string and setting the font to a multiple of the original font size, as described here: https://stackoverflow.com/a/19387401/2026098

Would still be nice to have a more concise solution, one that doesn't involve looking at every single character in turn.

Community
  • 1
  • 1
Elise
  • 5,086
  • 4
  • 36
  • 51
  • 1
    Where did you see looping at every character? Actually, it loops over attributes, not characters. – kirander Oct 07 '15 at 11:19
  • I see. so it isn't as horribly inefficient as I imagined? – Elise Oct 07 '15 at 12:21
  • No. It filtered over one attribute and therefore very efficient. – kirander Oct 07 '15 at 12:40
  • Well, unless the original text often shifts between different font sizes, right? Isn't there one attribute per ranges of chars that use different styling? – Elise Oct 07 '15 at 13:06
  • 1
    The names of attributes are predefined. So it is only one attribute for all ranges with different styles. The method will return all ranges, where it was used. BTW, you should avoid premature optimisation. Just use this method and measure efficiency later. But anyway, it will not be critical. – kirander Oct 07 '15 at 13:16

0 Answers0