문제
이상한 상황 - Apple의 예제는 작품이지만 약간 변경 한 후에는 텍스트가 표시되지 않습니다. 이 코드는 파란색 배경을 올바르게 그립니다. 그러나 내가 무엇을하든 상관없이 텍스트를 그리는 것을 거부합니다.
#import <UIKit/UIKit.h>
@interface CWnd : UIWindow @end
@implementation CWnd
- (void) drawRect : (CGRect) i_poRect
{
// This is working : windows is blue.
CGContextRef oContex = UIGraphicsGetCurrentContext();
CGContextSetRGBFillColor( oContex, 0, 0, 255, 1 );
CGContextFillRect( oContex, i_poRect );
// This is not working : not text is displayed.
CGContextSelectFont( oContex, "Monaco", 10, kCGEncodingFontSpecific );
CGContextSetRGBStrokeColor( oContex, 255, 0, 0, 1 );
CGContextSetRGBFillColor( oContex, 255, 0, 0, 1 );
CGContextSetTextDrawingMode( oContex, kCGTextFill );
CGContextSetTextPosition( oContex, 100, 100 );
CGContextShowText( oContex, "abc", 3 );
}
@end
@interface CDelegate : NSObject <UIApplicationDelegate> @end
@implementation CDelegate
- (void)applicationDidFinishLaunching : (UIApplication *) i_poApp
{
CGRect oRect = [ [ UIScreen mainScreen ] bounds ];
[ [ [ CWnd alloc] initWithFrame : oRect ] makeKeyAndVisible ];
}
@end
int main(int argc, char *argv[])
{
return UIApplicationMain( argc, argv, nil, @"CDelegate" );
}
해결책
문제없이 다음 코드를 사용하여 쿼츠 컨텍스트에 직접 텍스트를 그렸습니다.
CGContextSetStrokeColorWithColor(context, strokeColor);
CGContextSetFillColorWithColor(context, strokeColor);
CGContextSelectFont(context, "Helvetica", fontSize, kCGEncodingMacRoman);
CGContextSetTextDrawingMode(context, kCGTextFill);
CGContextSetTextPosition(context, 0.0f, round(fontSize / 4.0f));
CGContextShowText(context, [text UTF8String], strlen([text UTF8String]));
귀하의 경우에 무엇이 잘못되고 있는지는 분명하지 않습니다. 이 경우 조심해야 할 두 가지가 있습니다.이 경우 텍스트는 Uiviews와 그 Calayers의 뒤집힌 좌표 공간으로 인해 거꾸로 떨어질 수 있으며 Rhythmic Fistman이 지적한 것처럼 UTF 인코딩을 처리하지 않습니다.
성능이 적은 접근 방식은 다음과 같은 작업을 수행하는 것이 좋습니다.
CGContextSetFillColorWithColor(context, strokeColor);
[text drawAtPoint:CGPointMake(0.0f, 0.0f) withFont:[UIFont fontWithName:@"Helvetica" size:fontSize]];
다른 팁
이봐, 방금 내 코드에서 메모를 찾았습니다.
보기에 CGContextShowTextAtPoint
함께 작동하지 않습니다 CGContextSetFont
.
대신 사용해야합니다 CGContextSelectFont
. 분명히, 응?
어쨌든, 나는 이것이 사실이 아님을 알고 있지만, 당신은 CGContextShowTextAtPoint
.
이것은 아마도 별도의 대답에 속하지 않았을 것입니다.
다른 사람 (여기 그리고 여기)는 약간의 아핀 변환을 수행하고 사용했습니다 CGContextShowTextAtPoint
작동하는 방법을 작동시키기위한 방법 :
CGContextSelectFont(oContex, @"Monaco", 10, kCGEncodingFontSpecific);
CGContextSetTextDrawingMode(oContex, kCGTextFill);
CGContextSetRGBFillColor(oContex, 1.0, 0.0, 0.0, 1.0);
CGAffineTransform xform = CGAffineTransformMake(1.0, 0.0, 0.0, -1.0, 0.0, 0.0);
CGContextSetTextMatrix(oContex, xform);
CGContextShowTextAtPoint(oContex, 100, 100, "abc", 3);
또 다른 한 가지 문제는 CGContextSetRGBFillColor
방법은 0.0에서 1.0 범위의 부동 소수점 인수를 취합니다. 당신은 255를 사용하여 "풀 컬러"를 나타내는데,이 방법은 그 방법이 기대하는 것이 아닙니다. 0 ~ 255 범위에 더 편한 경우 다음은 작동해야합니다.
CGContextSetRGBFillColor(oContex, 255/255.0, 0/255.0, 0/255.0, 1.0);
이봐, 인코딩 문제가 될 수 있습니다.
변경해보십시오 kCGEncodingFontSpecific
에게 kCGEncodingMacRoman
.
나중에 모든 것이 작동하면 CGContextShowTextAtPoint
.
쓰레기, 비공개이며 필요한 글리프 버전을 사용하려면
Forbidden API를 호출하십시오. 텍스트 도면에 uikit을 사용하는 것이 훨씬 좋습니다.
그것은 유니 코드에 정통하고 또한 캐릭터를 위해 괜찮은 글리프 대체를합니다.
글꼴에는 존재하지 않습니다.
또 다른 가능성이 있으며, 이는 당신이 지정하는 텍스트 위치가 당신이 그리려고하는 사각형 외부에 있다는 것입니다. 대체하여 이것을 쉽게 테스트 할 수 있습니다
CGContextSetTextPosition( oContex, 100, 100 );
~와 함께
CGContextSetTextPosition( oContex, CGRectGetMidX(i_poRect), CGRectGetMidY(i_poRect));
cgcontextestettextmatrix 메소드를 사용하십시오. "거꾸로 문제"는 매트릭스에서 나온 것입니다.