2012-10-12 2 views
2

현재 iOS의 UIView에서 CALayerrenderInContext 메소드를 사용하여 PDF 문서를 만듭니다.UIView에서 PDF를 작성하는 iOS

내가 직면 한 문제는 라벨의 선명도입니다. 내가 그렇게 같은 drawLayer을 재정의하는 UILabel 서브 클래스 만들었습니다

/** Overriding this CALayer delegate method is the magic that allows us to draw a vector version of the label into the layer instead of the default unscalable ugly bitmap */ 
- (void)drawLayer:(CALayer *)layer inContext:(CGContextRef)ctx { 
    BOOL isPDF = !CGRectIsEmpty(UIGraphicsGetPDFContextBounds()); 
    if (!layer.shouldRasterize && isPDF) 
     [self drawRect:self.bounds]; // draw unrasterized 
    else 
     [super drawLayer:layer inContext:ctx]; 
} 

이 방법은 나에게 좋은 선명한 텍스트를 그릴 수 있습니다를, 그러나, 문제는 내가 통제권이없는 다른 전망이다. UITableView 또는 UIButton에 포함 된 라벨에 대해 비슷한 작업을 수행 할 수있는 방법이 있습니까? 뷰 스택을 반복하고 더 선명한 텍스트를 그릴 수있는 방법을 찾는 방법을 찾고 있습니다. 이 텍스트가 잘 렌더링 (내 사용자 지정 UILabel의 서브 클래스) Imgur

표준의 텍스트 제어 분할

가 날카로운되지 않습니다 : :

Imgur

편집 여기

은 예입니다 : 다음과 같이 컨텍스트를 내 PDF로 그리는 중입니다 :

UIGraphicsBeginPDFContextToData(self.pdfData, CGRectZero, nil); 
pdfContext = UIGraphicsGetCurrentContext(); 
UIGraphicsBeginPDFPageWithInfo(CGRectMake(0, 0, 612, 792), nil); 
[view.layer renderInContext:pdfContext]; 
+0

가 어떻게이 상황을 '획득하는 우선이 drawLayer 방법의 내 사용자 지정 구현 카테고리 UIView+PDF을 만든 다음 load 방법 나는 다음과 같은 사용 'renderInContext'를 사용하여 다시 그리는가? 거기서 해상도를 두 배로 늘릴 수 있는지 궁금합니다. – matt

+0

내 게시물이 업데이트되었습니다. – danielbeard

답변

2

필자는 뷰 계층 구조를 가로 지르며 UILabel을 모두 drawLayer을 재정의하는 내 사용자 정의 서브 클래스로 설정했습니다. 여기

내가보기 통과하는 방법입니다

+(void) setClassForLabel: (UIView*) label { 
    static Class myFancyObjectClass; 
    myFancyObjectClass = objc_getClass("UIPDFLabel"); 
    object_setClass(label, myFancyObjectClass); 
} 

비교 :

올드 :

Image

와 나는 클래스를 변경하는 방법

새로운 기능 :

Imgur

가이 작업을 수행 할 수있는 더 좋은 방법이지만, 내 목적을 위해 작동하는 것 같다 확실하지.

EDIT : 클래스를 변경하거나 전체 뷰 계층 구조를 순회하지 않는 더 일반적인 방법을 찾았습니다. 나는 방법 swizzling을 사용하고 있습니다. 이 방법을 사용하면 원할 경우 경계가있는 모든 뷰를 둘러싼 멋진 것들을 할 수 있습니다. 여기 예에서 근무

// The "+ load" method is called once, very early in the application life-cycle. 
// It's called even before the "main" function is called. Beware: there's no 
// autorelease pool at this point, so avoid Objective-C calls. 
Method original, swizzle; 

// Get the "- (void) drawLayer:inContext:" method. 
original = class_getInstanceMethod(self, @selector(drawLayer:inContext:)); 
// Get the "- (void)swizzled_drawLayer:inContext:" method. 
swizzle = class_getInstanceMethod(self, @selector(swizzled_drawLayer:inContext:)); 
// Swap their implementations. 
method_exchangeImplementations(original, swizzle); 

: http://darkdust.net/writings/objective-c/method-swizzling

+0

WTF @ setClassForLabel -_-, 잘 연주했습니다. – 0xSina

+0

어떻게 이것을 두 번 upvote 수 있습니까? 훌륭한. – matt

+1

'drawLayer'의 맞춤 구현을 공유 하시겠습니까? –

관련 문제