2014-06-19 3 views
0

RectFrame (width = 170, height = 30)에 텍스트 (텍스트는 "0 %", 글꼴 크기 = 30)를 그리는 방법이 있습니다.iOS NSString drawInRect는 항상 RectFrame의 맨 아래에 텍스트를 그립니다.

/* Draw String IOS 7 up */ 
-(void) drawString: (CGRect) contextRect withStr: (NSString *) string { 
    NSMutableParagraphStyle *textStyle = [[NSMutableParagraphStyle defaultParagraphStyle] mutableCopy]; 
    textStyle.lineBreakMode = NSLineBreakByWordWrapping; 
    textStyle.alignment = NSTextAlignmentCenter; 

    UIFont *font = [UIFont systemFontOfSize:_textSize]; 
    UIColor* textColor = [UIColor grayColor]; 

    NSDictionary* stringAttrs = @{NSFontAttributeName:font, NSParagraphStyleAttributeName:textStyle, NSForegroundColorAttributeName:textColor}; 
    [string drawInRect:contextRect withAttributes:stringAttrs]; 
} 

나는 이미지 결과를 게시 할 수 없습니다하지만 결과는 텍스트가 프레임의 맨 아래에 약간의 픽셀을 그려되어 있지만 바닥은 아무리 (하단 프레임 텍스트 하단 일치) 미세 없으며, 프레임과 텍스트의 크기를 모두 조정합니다. 프레임 상단 아래에 항상 "어딘가"로 그려지는 아이디어가 있습니까?

답변

0

작동 방법입니다. 그것은 항상 rect의 꼭대기 아래 몇 픽셀을 그립니다.

위치 지정을보다 효과적으로 제어하려면 CTTypesetter 및 기타 핵심 텍스트 API를 사용하여 텍스트 크기를 계산할 수 있습니다. https://developer.apple.com/library/ios/documentation/StringsTextFonts/Conceptual/CoreText_Programming/Overview/Overview.html#//apple_ref/doc/uid/TP40005533-CH3-SW1

: 당신은 핵심 텍스트를 사용하려고하는 경우

http://www.snip2code.com/Snippet/15903/Simple-CoreText-with-CTLineDraw-and-CTRu/

, 애플의 설명서를 읽으십시오 : 여기에 "간단한"예이다