Search code examples
ccore-text

Align text to bottom of a frame using Core Text


I have an attributed string that I want to draw bottom-aligned into a rectangular path, using Core Text. Is there a way to get CTFrameSetter / CTFrame to do this, or do I need to do it manually? The manual way being:

  1. Figure out the height of the frame using CTFramesetterSuggestFrameSizeWithConstraints
  2. Adjust the height of the path.

Solution

  • You'll have to do it manually.

    CGRect boundingBox = CTFontGetBoundingBox(font);
    
    //Get the position on the y axis
    float midHeight = self.frame.size.height / 2;
    midHeight -= boundingBox.size.height / 2;
    
    CGPathAddRect(path, NULL, CGRectMake(0, midHeight, self.frame.size.width, boundingBox.size.height));
    

    Reference: Vertical align with Core Text?