0

I am writing some text to a quartz context.

I have a box of 500 x 200 pixels and I am writing some text inside. The box can have any aspect ratio but the text will always write with the font proportions as it is. What I want is to scale the font vertically and keep the horizontal scale.

See the picture. The first box represents what I am getting, the second one, what I want.

enter image description here

this is how I am writing it...

CGRect rect = CGRectMake(0,0,500,200);
[myText drawInRect:rect
        withFont:aFont
        lineBreakMode:UILineBreakModeTailTruncation
        alignment:UITextAlignmentCenter];

is there a way to scale the font vertically?

NOTE: as this is being written in a PDF context, I don't want to render the text to a image context and scale it vertically, because I don't want to lose resolution.

thanks

Duck
  • 34,902
  • 47
  • 248
  • 470
  • I can't think of a way this would work. The fonts are essentially just a set of fixed aspect glyphs, so even if you could figure out a way to tell iOS to render with a larger height, you'd still lose resolution. Even graphics programs like GIMP don't allow that kind of text rendering. – Mike M Sep 26 '12 at 12:19
  • what happens when a text is rendered to a PDF context? is it rendered vectorially? If it is then, in theory, it can be scaled to any aspect ratio without losing resolution. I have found this CGContextSetTextMatrix function. What is the purpose of that? in theory it appears to be what I want, but when I create a CGAffineTransformMakeScale (1.0f, 1.5f) to see if it will scale the text 50% in Y, nothing changes... – Duck Sep 26 '12 at 12:37
  • maybe it would work with a truetype font? aren't those vector based? – Mike M Sep 26 '12 at 13:27
  • as far as I know, all fonts are vector... – Duck Sep 26 '12 at 14:00
  • HELLO THERE ! Kindly Take a look at the bellow answer and say whether it helps you or not?? – Bala Sep 27 '12 at 11:24
  • That's fine , you are back now!! :) – Bala Sep 27 '12 at 11:30

1 Answers1

1
  - (void) scaleTextVerticallyWithContext:(CGContextRef)myContext withRect:(CGRect)contextRect
{
    CGFloat w, h;
    w = contextRect.size.width;
    h = contextRect.size.height;

    CGAffineTransform myTextTransform;    
    CGContextSelectFont (myContext, "Helvetica-Bold", h/10, kCGEncodingMacRoman);
    CGContextSetCharacterSpacing (myContext, 10);
    CGContextSetTextDrawingMode (myContext, kCGTextFillStroke); 

    CGContextSetRGBFillColor (myContext, 0, 1, 0, .5);
    CGContextSetRGBStrokeColor (myContext, 0, 0, 1, 1);  

         /*  
          *  CGAffineTransformMakeScale ( CGFloat sx, CGFloat sy );
          *  sx: The factor by which to scale the x-axis of the coordinate system.
          *  sy: The factor by which to scale the y-axis of the coordinate system.  
          */

    myTextTransform = CGAffineTransformMakeScale(1,8);

    CGContextSetTextMatrix (myContext, myTextTransform);
    CGContextShowTextAtPoint (myContext, 40, 0, "Hello There", 9);
}

- (void)drawRect:(CGRect)rect{

    // Drawing code
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGRect viewBounds = self.bounds;
    CGContextTranslateCTM(context, 0, viewBounds.size.height);
    CGContextScaleCTM(context, 1, -1);

    [self scaleTextVerticallyWithContext:context withRect:viewBounds];
}
Bala
  • 2,895
  • 1
  • 19
  • 60