How to change font in NSTextContainer

1.2k views Asked by At

My application is setup as so.

Custom UIView inside a ScrollView.

The custom UIView is getting text generated on top of it with this code.

CoreDavening.m draw function

attStorage = [[NSTextStorage alloc]initWithAttributedString:string];

textContainer = [[NSTextContainer alloc]
                                  initWithSize:CGSizeMake(self.frame.size.width, FLT_MAX)];

layoutManager = [[NSLayoutManager alloc]init];
[layoutManager addTextContainer:textContainer];
[attStorage addLayoutManager:layoutManager];

NSRange glyphRange = [layoutManager
                      glyphRangeForTextContainer:textContainer];
[layoutManager drawGlyphsForGlyphRange: glyphRange atPoint: rect.origin];

In my ViewController.m I'm creating the view like so.

CoreDavening *davenView = [[CoreDavening alloc]initWithFrame:CGRectMake(0, 0,self.view.frame.size.width, [coreDavening heightForStringDrawing])];

    davenView.backgroundColor = [UIColor whiteColor];
    davenView.layer.borderColor = [[UIColor whiteColor]CGColor];
    scrollView.backgroundColor = [UIColor whiteColor];
    [scrollView addSubview:davenView];
    self.scrollView.contentSize = davenView.frame.size;

Im trying to find a way to 1) change the font size (just size) 2) have the scrollView and CustomView resize appropriately.

How can this be done?

EDIT 1: Im creating the custom view in my viewController like this.

CoreDavening *coreDavening = [[CoreDavening alloc]init];

     davenView = [[CoreDavening alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, [coreDavening heightForStringDrawing ])];

and updating/refreshing the custom view like this.

It get triggered when the slider I have set up changes value.

- (IBAction)sliderChanged:(id)sender {

    NSUserDefaults *prefs= [NSUserDefaults standardUserDefaults];
    [prefs setInteger:(long)self.Slider.value forKey:@"fontSize"];
    [prefs synchronize];

    [davenView setNeedsLayout];
}

My custom view controller has the font set up like.

THIS IS IN THE DRAW FUNCTION

attStorage = [[NSTextStorage alloc]initWithAttributedString:string];


    [attStorage addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:(long)[prefs integerForKey:@"fontSize"]] range:NSMakeRange(0, attStorage.length)];

    textContainer = [[NSTextContainer alloc]
                                      initWithSize:CGSizeMake(self.frame.size.width, FLT_MAX)];

    layoutManager = [[NSLayoutManager alloc]init];
    [layoutManager addTextContainer:textContainer];
    [attStorage addLayoutManager:layoutManager];

    NSRange glyphRange = [layoutManager
                          glyphRangeForTextContainer:textContainer];
    [layoutManager drawGlyphsForGlyphRange: glyphRange atPoint: rect.origin];
1

There are 1 answers

8
Sandeep On

It seems like you have the textStorage associated with the layoutManager already. NSTextStorage is the subclass of NSMutableAttributedString. So, this is the class which is responsible to hold the layout and styling information. How have you created your textStorage ? Besides, the styling, NSTextStorage also has methods to allow editing the contents and have dynamic styling.

You can simply set the attribute to your textStorage if you want to change the font.

[_textStorage addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:20.0] range:NSMakeRange(0, _textStorage.length)];

NSTextManager class has quite useful methods to find the bounds and glyphs informations. So, you could use method like below to find the bounds for your text,

- (CGRect)boundingRectWithSize:(CGSize)size
{
    self.textContainer.size = size;

    NSRange glyphRange = [self.layoutManager glyphRangeForTextContainer:self.textContainer];

    CGRect boundingRect = [self.layoutManager boundingRectForGlyphRange:glyphRange
                                                        inTextContainer:self.textContainer];
    boundingRect.origin.x = 0;
    boundingRect.origin.y = 0;

    return boundingRect;
}