Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Here's a subclass of <code>UIView</code> that does what you want...</p> <p>You can place it in a <code>UIScrollView</code>. Call <code>-sizeThatFits:</code> to get the correct frame size during your superview's <code>-layoutSubviews</code>. (also for your enclosing scroll view's <em>contentSize</em>.)</p> <p>Note that I included a category on UILabel that returns a reasonable size to <code>-sizeThatFits:</code>, you may want to provide your own implementation.</p> <p><strong>View.h</strong></p> <pre><code>#import &lt;UIKit/UIKit.h&gt; @interface View : UIView @property ( nonatomic, strong, readonly ) UILabel * descriptionLabel ; @property ( nonatomic, strong, readonly ) UILabel * nameLabel ; @property ( nonatomic, strong, readonly ) UIImageView * pdfImageView ; @property ( nonatomic ) CGPDFDocumentRef pdf ; @end </code></pre> <p><strong>View.m</strong></p> <pre><code>#import "View.h" @implementation UILabel (SizeThatFits) -(CGSize)sizeThatFits:(CGSize)fitSize { return [ self.text sizeWithFont:self.font constrainedToSize:fitSize ] ; } @end @interface View () @property ( nonatomic, strong, readonly ) UIImage * pdfImage ; @end @implementation View @synthesize descriptionLabel = _descriptionLabel ; @synthesize nameLabel = _nameLabel ; @synthesize pdfImageView = _pdfImageView ; @synthesize pdfImage = _pdfImage ; -(void)dealloc { CGPDFDocumentRelease( _pdf ) ; } -(CGSize)sizeThatFits:(CGSize)size { CGSize fitSize = (CGSize){ size.width, CGFLOAT_MAX } ; CGSize result = { size.width, 0 } ; result.height = [ self.headerLabel sizeThatFits:fitSize ].height + [ self.label sizeThatFits:fitSize ].height + self.pdfImage.size.height * fitSize.width / self.pdfImage.size.width ; return result ; } -(void)layoutSubviews { CGRect bounds = self.bounds ; CGRect slice ; CGRectDivide( bounds, &amp;slice, &amp;bounds, [ self.headerLabel sizeThatFits:bounds.size ].height, CGRectMinYEdge ) ; self.headerLabel.frame = slice ; CGRectDivide( bounds, &amp;slice, &amp;bounds, [ self.label sizeThatFits:bounds.size ].height, CGRectMinYEdge ) ; self.label.frame = slice ; self.pdfImageView.frame = bounds ; } -(void)setPdf:(CGPDFDocumentRef)pdf { CGPDFDocumentRelease( _pdf ) ; _pdf = CGPDFDocumentRetain( pdf ) ; _pdfImage = nil ; } -(UILabel *)descriptionLabel { if ( !_descriptionLabel ) { UILabel * label = [[ UILabel alloc ] initWithFrame:CGRectZero ] ; label.numberOfLines = 0 ; // // ... configure label here // [ self addSubview:label ] ; _descriptionLabel = label ; } return _descriptionLabel ; } -(UILabel *)nameLabel { if ( !_nameLabel ) { UILabel * label = [[ UILabel alloc ] initWithFrame:CGRectZero ] ; label.numberOfLines = 0 ; // // ... configure label here // [ self addSubview:label ] ; _nameLabel = label ; } return _nameLabel ; } -(UIView *)pdfImageView { if ( !_pdfImageView ) { UIImageView * imageView = [[ UIImageView alloc ] initWithFrame:CGRectZero ] ; [ self addSubview:imageView ] ; _pdfImageView = imageView ; } return _pdfImageView ; } -(UIImage *)pdfImage { if ( !_pdfImage ) { CGPDFPageRef page = CGPDFDocumentGetPage( self.pdf, 1 ) ; // 1 indexed CGRect mediaBox = CGPDFPageGetBoxRect( page, kCGPDFMediaBox ) ; UIGraphicsBeginImageContextWithOptions( mediaBox.size, NO, self.window.screen.scale ) ; CGContextRef c = UIGraphicsGetCurrentContext() ; CGContextScaleCTM( c, 1.0f, -1.0f ) ; CGContextTranslateCTM( c, 0.0, -mediaBox.size.height ) ; CGContextDrawPDFPage( c, page ) ; _pdfImage = UIGraphicsGetImageFromCurrentImageContext() ; self.pdfImageView.image = _pdfImage ; UIGraphicsEndImageContext() ; } return _pdfImage ; } @end </code></pre> <p>If you need a page number footer, you can add another UILabel at the bottom, following the same pattern.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload