My end goal is to draw a report that looks like MS Word using Quartz into a UIView, having and then having that UIView in a UIScrollView.
What I have now is a top level Report Object, and then it’s children are parts of the report to draw like ReportTitle, ReportTable, ReportSplitColumn, etc. The top level Report Object looks like:
@property (nonatomic, retain) UIFont *font;
@property (nonatomic, assign) CGFloat margins;
@property (nonatomic, assign) TextObjectStyle style;
@property (nonatomic, assign) CGColorRef color;
- (CGSize)size;
In my proof of concept, I created a ReportView object that knows how to draw ReportObjects that take a specific ReportObject, and draws it at a specific point. So my drawRect would like something like:
//pseudocode
drawRect: {
//get the current context
CGFloat startHeight = 0;
CGPoint point = CGPointMake(0, startHeight);
startHeight += [self drawReportTitle:currentContext atPoint:CGPointMake(0, point.y)].height;
point.y = startHeight;
startHeight += [self drawParagraph:currentContext withText:paragraphs atPoint:CGPointMake(0, point.y)].height;
point.y = startHeight;
}
What I started trying to do was have my ReportView have a property of NSArray *ReportObjectsArray and then have my caller class set that value, and then have my ReportView draw it. I was hoping I could do something like:
for (ReportView *r in self.ReportObjectsArray) {
startHeight += [self drawReportObject].height atPoint:CGPointMake(0, point.y;
point.y = startHeight
}
Except the problem I realize is, my own classes don’t know how to draw themselves. I think I remember seeing this pattern in a Java book where the class knows how to draw itself. And that does make sense to me. However the problem I ran into was in the end, I want one UIView of type ReportView that is scrollable in UIScrollView. I wasn’t really sure if each ReportObject should subclass UIView instead and then add each of these objects to the UIView in the scrollView as a subView, and then have it draw itself that way. Is that the better way to go? I’m not really sure. If it’s not, what is my alternative? Would I have all my classes have a draw method, but in it, it just posts a notification and then in my ReportView class, I can have it listen to those notifications, and then call the appropriate draw methods? Thanks.
You definitely want to use a hierarchy of
UIViews rather than implementing a lotdrawRects.UIKitis very good about optimization and you defeat that if you do a lot of non-optimzed/naivedrawRects.You might or might not create custom
UIViewsubsclasses for all your objects. It may be enough to use the builtinUIViewsubclasses and just create them based on your report objects. Subclassing is more complicated but is generally needed if you need to do more custom layout (inlayoutSubviews) than the standard struts and springs provide.