0

CGContextShowTextAtPointではなく、drawAtPointで文字列を描画しようとしています。

- (UIImage *)drawonimage:(UIImage *)img {
// myString is a string to draw
// fontName is a string (font name)
// fontSize is the font size (CGfloat)

CGFloat w = img.size.width;
CGFloat h = img.size.height;

UIGraphicsBeginImageContextWithOptions(img.size,NO,1.0f);
CGColorSpaceRef colorSpace0 = CGColorSpaceCreateDeviceRGB();
CGContextRef context0 = CGBitmapContextCreate(NULL,w,h,8,4 * w,colorSpace0,kCGImageAlphaPremultipliedFirst);
CGContextDrawImage(context0,CGRectMake(0,0,w,h),img.CGImage);
CGPoint p0 = CGPointMake(0,0);
[img drawAtPoint:p0];
CGPoint p1 = CGPointMake(0,0);
UIColor *aColor;
aColor = [UIColor colorWithRed:100/255.0 green:120.0/255.0 blue:140.0/255.0 alpha:90/100.0];
CGContextSetFillColorWithColor(UIGraphicsGetCurrentContext(),aColor.CGColor);
[myString drawAtPoint:p1 withFont:[UIFont fontWithName:fontName size:fontSize]];
UIImage *Img = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return Img;
}

私が抱えている問題は、文字列が実際に表示される場所です。たとえば、描きたい文字列が「Hello world!」だとします。文字列の左上隅が 0,0 に配置されていません。(写真参照)フォントやフォントサイズなどによって見え方が変わると思います。それで、弦の高さ(幅ではなく)を測定する方法があるのだろうか? または、文字列の左上隅 (矢印) が 0,0 に配置されるようにするにはどうすればよいでしょうか?

ご協力ありがとうございました。

ここに画像の説明を入力

4

1 に答える 1

1

自明ではないようです...sizeWithFont常に高さの可能な最大値を返し、幅の正しい値を返します...

ここで指摘されているように ( UIKit を使用してテキストの正確な高さを計算するにはどうすればよいですか? )、CoreText フレームワーク ( http://developer.apple.com/library/ios/#documentation/StringsTextFonts/ ) を確認する必要があります。 Conceptual/CoreText_Programming/Introduction/Introduction.html#//apple_ref/doc/uid/TP40005533 )。

于 2013-02-21T15:12:16.967 に答える