나는 context.ShowTextAtPoint() 메소드를 사용하여 iPhone에 텍스트를 가운데 정렬하는 방법을 찾고있다.아이폰의 중심 텍스트 ShowTextAtPoint
4
A
답변
8
BETER 늦게 결코보다는 :
먼저 (보이지 않는 모드) 뷰의 텍스트를 넣어 :
CGTextDrawingMode mode = CGContextGetTextDrawingMode(ctx);
CGContextSetTextDrawingMode(ctx, kCGTextInvisible);
CGContextShowTextAtPoint(ctx, 0, 0, @"test", strlen("test"));
그런 다음 텍스트의 위치를 얻을 볼에 다시 모드를 설정합니다
CGPoint pt = CGContextGetTextPosition(ctx);
CGContextSetTextDrawingMode(ctx, mode);
이제 보이지 않는 텍스트의 위치가 있습니다. 그런 다음 화면 중앙 (160)을 사용하고 새 텍스트를 입력하십시오.
CGContextShowTextAtPoint(ctx, 160 - pt.x/2, 200, @"test", strlen("test"));
2
왼쪽 및 오른쪽 정렬 할 수있는 코드입니다. 당신은 그것으로부터 중심을 찾을 수 있습니다.
#import <UIKit/UIKit.h>
@interface AdustableUILabel : UILabel{
CGFloat characterSpacing;
}
@property CGFloat characterSpacing;
@end
#import "AdustableUILabel.h"
@implementation AdustableUILabel
@synthesize characterSpacing;
- (void)drawTextInRect:(CGRect)rect
{
if (characterSpacing)
{
CGContextRef context = UIGraphicsGetCurrentContext();
CGFloat size = self.font.pointSize;
CGContextSelectFont (context, [self.font.fontName UTF8String], size, kCGEncodingMacRoman);
CGContextSetCharacterSpacing (context, characterSpacing);
CGContextSetTextDrawingMode (context, kCGTextFill);
CGContextSetRGBFillColor(context, 255/255.0, 255/255.0, 255/255.0, 255/255.0);
// Rotate text to not be upside down
CGAffineTransform xform = CGAffineTransformMake(1.0, 0.0, 0.0, -1.0, 0.0, 0.0);
CGContextSetTextMatrix(context, xform);
const char *cStr = [self.text UTF8String];
//get the UILabel alignment
if(self.textAlignment == UITextAlignmentLeft){
// Drawing code
//just draw it in the current Rect offset from LHS
CGContextShowTextAtPoint (context, rect.origin.x, rect.origin.y + size, cStr, strlen(cStr));
}else{
//RHS
//draw in invisible mode, get the size then subtract from width of rect to get left hand x of the text
CGContextSetTextDrawingMode(context, kCGTextInvisible);
CGContextShowTextAtPoint (context, rect.origin.x, rect.origin.y + size, cStr, strlen(cStr));
//Then get the position of the text and set the mode back to visible:
CGPoint pt = CGContextGetTextPosition(context);
//Draw at new position
CGContextSetTextDrawingMode(context, kCGTextFill);
CGContextShowTextAtPoint(context, rect.size.width - pt.x, rect.origin.y + size, cStr, strlen(cStr));
//===============
}
}
else
{
// no character spacing provided so do normal drawing
[super drawTextInRect:rect];
}
}
@end
관련 문제
- 1. CSS 세로 텍스트 중심
- 2. 아이폰의 라벨에있는 텍스트 끊기
- 3. CGBitmapContext에 "ShowTextAtPoint"함수가있는 독일어 umlaute
- 4. 아이폰의 멀티 라인 텍스트 필드
- 5. 아이폰의 텍스트 파일에 로그인 정보 저장하기
- 6. 링크 및 이미지 중심
- 7. 중심 필드
- 8. 아이폰의 ProgressBar
- 9. 아이폰의 UITextField
- 10. 아이폰의 RESTful 웹 서비스
- 11. 프로그래밍 언어/패러다임 (서브 텍스트/예제 중심 프로그래밍)
- 12. 아이폰의 로그인 화면
- 13. 멀티 터치 환경에서 사용자 중심
- 14. UI 중심 및 도메인 중심 데이터 모델 - 장단점
- 15. 아이폰의 컨트롤에 포커스가 작동하지 않습니까?
- 16. 아이폰의 UITableView에서 특정 셀/행을 선택하면 텍스트 색상이 바뀝니 까?
- 17. 아이폰의 UITableView에서 특정 셀/행을 선택하면 텍스트 색상이 바뀝니 까?
- 18. 중심 이미지가 흐림
- 19. WPF에서 선의 중심 찾기
- 20. CSS의 세로 중심 요소
- 21. NSTextView에서 문자 중심 찾기
- 22. 세로 중심 페이지의 div
- 23. Ajax 중심 오픈 소스
- 24. IE의 중심 배경 이미지
- 25. Box2D 질량 중심
- 26. 배경 이미지 중심 유지
- 27. 코드로 NSRect 중심 지정하기
- 28. IE6의 모달 중심 문제
- 29. 배경 중심 알고리즘
- 30. Django의 중심 디자인보기