gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 04:24:10 24 4
gpt4 key购买 nike

我正在向 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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com