Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8674855
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T19:48:26+00:00 2026-06-12T19:48:26+00:00

I am currently creating PDF documents from a UIView in iOS by using CALayer

  • 0

I am currently creating PDF documents from a UIView in iOS by using CALayer and the renderInContext method.

The problem I am facing is the sharpness of labels. I have created a UILabel subclass that overrides drawLayer like so:

/** 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];
}

This method lets me draw nice crisp text, however, the problem is with other views that I don’t have control over. Is there any method that would allow me to do something similar for labels embedded in UITableView or UIButton. I guess I’m looking for a way to iterate through the view stack and do something to let me draw sharper text.

Here is an example:
This text renders nicely (my custom UILabel subclass)
Imgur

The text in a standard segmented control isn’t as sharp:

Imgur

Edit: I am getting the context to draw into my PDF as follows:

UIGraphicsBeginPDFContextToData(self.pdfData, CGRectZero, nil);
pdfContext = UIGraphicsGetCurrentContext();
UIGraphicsBeginPDFPageWithInfo(CGRectMake(0, 0, 612, 792), nil);
[view.layer renderInContext:pdfContext];
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T19:48:27+00:00Added an answer on June 12, 2026 at 7:48 pm

    I ended up traversing the view hierarchy and setting every UILabel to my custom subclass that overrides drawLayer.

    Here is how I traverse the views:

    +(void) dumpView:(UIView*) aView indent:(NSString*) indent {
        if (aView) {
            NSLog(@"%@%@", indent, aView);      // dump this view
    
            if ([aView isKindOfClass:[UILabel class]])
                [AFGPDFDocument setClassForLabel:aView];
    
            if (aView.subviews.count > 0) {
                NSString* subIndent = [[NSString alloc] initWithFormat:@"%@%@",
                                   indent, ([indent length]/2)%2==0 ? @"| " : @": "];
                for (UIView* aSubview in aView.subviews)
                    [AFGPDFDocument dumpView:aSubview indent:subIndent];
            }
        }
    }
    

    And how I change the class:

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

    The comparison:

    Old:

    Image

    New:

    Imgur

    Not sure if there is a better way to do this, but it seems to work for my purposes.

    EDIT: Found a more generic way to do this that doesn’t involve changing the class or traversing through the whole view hierarchy. I am using method swizzling. This method also lets you do cool things like surrounding every view with a border if you want. First I created a category UIView+PDF with my custom implementation of the drawLayer method, then in the load method I use the following:

    // 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);
    

    Worked from the example here: http://darkdust.net/writings/objective-c/method-swizzling

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i am using itextsharp and creating a PDF with images. currently the images i
I'm currently converting some legacy code to create PDF files using iTextSharp. We're creating
im currently creating a graph for an app, using coreplot and have a problem
I'm creating PDF files with PHP using TCPDF. I have a small problem with
I'm currently creating an iPhone app (Xcode 4.3.1, IOS 5) that could use Bluetooth
I'm currently creating a iOS 5 iPad app where there will be heavy network
I'm trying to generate some LaTeX code which from thereon should generate PDF documents.
I am creating a simple reporting program using java and iReport (from jasper), which
I'm currently creating both the client and server app using ActiveResource for web servicing.
I am currently creating a frontend editor for wordpress using TDO Mini Forms and

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.