iphone - iOS - 在 Quartz 上垂直缩放文本

标签 iphone ios ipad quartz-graphics

我正在向 quartz 上下文写入一些文本。

我有一个 500 x 200 像素的盒子,我正在里面写一些文本。该框可以具有任何纵横比,但文本将始终按原样的字体比例书写。我想要的是垂直缩放字体并保持水平缩放。

看图。第一个框代表我得到的东西,第二个框代表我想要的东西。

enter image description here

我就是这样写的...

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

有没有办法垂直缩放字体?

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.

谢谢

最佳答案

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

关于iphone - iOS - 在 Quartz 上垂直缩放文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12601124/

相关文章:

iphone - 通过应用程序调用电话

ios - "Reset keyboard dictionary"iOS 模拟器设置中缺少选项

iphone - 添加到现有联系人

ios - Swift 默认参数和选择器语法

ios - Storyboard对于访问云的应用程序来说是个好主意吗?

iphone - willAnimateToRotation 没有被调用

objective-c - 使用 UIImage 黑白 iOS 屏蔽 UIView

iphone - 在 Xcode 6、iOS 8 中使用 Size 类设计 ViewController

ios - launchctl 不适用于 iOS 应用程序

c# - Unity 无法将类型 "string"隐式转换为 "bool"