Adding UI Elements to the CPLayerHostingView...

253 views
Skip to first unread message

Chris T

unread,
Sep 3, 2009, 1:25:11 PM9/3/09
to coreplot-discuss
Hello,

I'm getting some really strange behavior when trying to add a UILabel
to my graph.

Basically, I'd like to add some description label to an area above my
graph like: "Time vs. Whatever"

I'm trying to do this by adding a UILabel to the CPLayerHostingView
with the following code:

CGFloat labelDisplayWidth = 87.0;
CGFloat labelDisplayX = 171;
CGFloat labelDisplayY = 157;
CGFloat labelDisplayHeight = 81.0;

CGRect labelRect = CGRectMake(labelDisplayX, labelDisplayY,
labelDisplayWidth, labelDisplayHeight);
UILabel *labelDisplay = [[UILabel alloc] initWithFrame:labelRect];
labelDisplay.font = [UIFont boldSystemFontOfSize:24.0];
labelDisplay.backgroundColor = [UIColor clearColor];
labelDisplay.textColor = [UIColor colorWithRed:214.0/255.0 green:
156.0/255.0 blue:138.0/255.0 alpha:1.0];
labelDisplay.textAlignment = UITextAlignmentCenter;
labelDisplay.text = [NSString stringWithFormat:@"Time vs. Whatever"];
[self.view addSubview:labelDisplay];
[labelDisplay release];

Here's a link to an image of my results: http://i753.photobucket.com/albums/xx171/chodeguts/graph.jpg

Not sure what to make of this behavior. Whatever positioning params I
use, I get the exact same relection and centered position...

Any ideas, guys?

Thanks,

Chris.

Brad Larson

unread,
Sep 3, 2009, 3:32:52 PM9/3/09
to coreplot...@googlegroups.com
In order to maintain a consistent coordinate system between the Mac
and iPhone, we invert the coordinate space of the layer hosting view
on the iPhone with a transform. UILabels and other iPhone UI elements
assume the iPhone's normal flipped coordinate space, that's why you
are seeing the behavior this text exhibits.

I'd recommend not adding any UIViews to the CPLayerHostingView, but
instead you should add the label to the CPLayerHostingView's superview
and place it in front of the CPLayerHostingView. We do some touch-
detection and other manipulations within the hosting view that might
break standard iPhone UI elements.
______________________
Brad Larson, Ph.D.
Sunset Lake Software
http://www.sunsetlakesoftware.com

Chris Topher

unread,
Sep 3, 2009, 3:44:47 PM9/3/09
to coreplot...@googlegroups.com
Thanks, Brad...

Something like this?

      CGFloat labelDisplayWidth = 50.0;
    CGFloat labelDisplayX = 25;
    CGFloat labelDisplayY =  30;
    CGFloat labelDisplayHeight = 47.0;

   
    CGRect labelRect = CGRectMake(labelDisplayX, labelDisplayY, labelDisplayWidth, labelDisplayHeight);
    UILabel *labelDisplay = [[UILabel alloc] initWithFrame:labelRect];
    labelDisplay.font = [UIFont boldSystemFontOfSize:60.0];
    labelDisplay.backgroundColor = [UIColor clearColor];
    labelDisplay.textColor = [UIColor whiteColor];

    labelDisplay.textAlignment = UITextAlignmentCenter;
    labelDisplay.text = [NSString stringWithFormat:@"Time vs. Whatever"];

    [[hostingView superview] addSubview:labelDisplay];
    [hostingView bringSubviewToFront:labelDisplay];


    [labelDisplay release];

I must be doing something funky cause I don't see the label...


Thank you,

Chris.

Chris Topher

unread,
Sep 3, 2009, 4:22:32 PM9/3/09
to coreplot...@googlegroups.com
OK...getting there...

I'm not seeing anything with that code because [hostingView superview] is nil. I'm assuming that's because I'm casting self.view as the CPLayerHostingView?

    graph = (CPXYGraph *)[theme newGraph];   
    CPLayerHostingView *hostingView = (CPLayerHostingView *)self.view;
    hostingView.hostedLayer = graph;

So, I think I need to add the CPLayerHostingView as a subview of self.view and add the label's as subview's to self.view. Is that correct?

I tried this and don't even get the plot displayed anymore. Is that because of this inverted coordinate business?

Am I barking up the wrong tree here?

Sorry, I get confused easily :P

Chris.

Drew McCormack

unread,
Sep 4, 2009, 3:12:30 AM9/4/09
to coreplot...@googlegroups.com
Hi Chris,
Probably the best approach is to add a new UIView in IB, and drop your hosting view into it as a subview. Then add your label to that new UIView.
Make sense.
Drew

Chris Topher

unread,
Sep 4, 2009, 3:23:17 AM9/4/09
to coreplot...@googlegroups.com
Thanks, Drew.

I'll give that a go...

Chris.

Clumsily thumbed out on my iPhone.

Ivan "Rambius" Ivanov

unread,
Sep 5, 2009, 5:54:42 PM9/5/09
to coreplot...@googlegroups.com
Hello,

I am fighting with the same problem - I want to add a button in my chart.

On Fri, Sep 4, 2009 at 10:23 AM, Chris Topher<pusill...@gmail.com> wrote:
> Thanks, Drew.
> I'll give that a go...
>
> Chris.
> Clumsily thumbed out on my iPhone.
> On Sep 4, 2009, at 12:12 AM, Drew McCormack <drewmc...@mac.com> wrote:
>
> Hi Chris,
> Probably the best approach is to add a new UIView in IB, and drop your
> hosting view into it as a subview. Then add your label to that new UIView.
> Make sense.
> Drew

Drew, I followed your advice. In Interface Builder I added I have a
regular UIView and I added a new CPHostingLayerView to it. I then
retrieve the hosting view and add programmatically a button:

CPTheme *theme = [CPTheme themeNamed:themeName];
barChart = (CPXYGraph *)[theme newGraph];
UIView *view = self.view;
CPLayerHostingView *hostingView;
for (UIView *v in view.subviews) {
if ([v class] == [CPLayerHostingView class]) {
hostingView = (CPLayerHostingView *)v;
NSLog(@"Found hosting view %@", hostingView);
}
}
hostingView.hostedLayer = barChart;

NSLog prints the the found object so I am sure it is not nil. Now I
create the button:

CGFloat labelDisplayWidth = 50.0;
CGFloat labelDisplayX = 25;
CGFloat labelDisplayY = 30;
CGFloat labelDisplayHeight = 47.0;

CGRect labelRect = CGRectMake(labelDisplayX, labelDisplayY,
labelDisplayWidth, labelDisplayHeight);

UIButton *buttonDisplay = [[UIButton alloc] initWithFrame:labelRect];
buttonDisplay.font = [UIFont boldSystemFontOfSize:60.0];
buttonDisplay.backgroundColor = [UIColor clearColor];
[buttonDisplay setTitle:[NSString stringWithFormat:@"Time vs. Whatever"]
forState:UIControlStateNormal];
[buttonDisplay setTitleColor:[UIColor blueColor]
forState:UIControlStateNormal];

Then I add the button and I try to bring it to front

[self.view addSubview:buttonDisplay];
[self.view bringSubviewToFront:buttonDisplay];

[buttonDisplay release];

It does not show up with this piece code. I tried also with

[self.view addSubview:buttonDisplay];
[hostingView bringSubviewToFront:buttonDisplay];

and the button does not show as well. Of course it works with

[hostingView addSubview:buttonDisplay];
[hostingView bringSubviewToFront:buttonDisplay];

but the button is inverted. Any ideas how to show my button?

Regards
Rambius

--
Tangra Mega Rock: http://www.radiotangra.com

Drew McCormack

unread,
Sep 6, 2009, 3:49:21 AM9/6/09
to coreplot...@googlegroups.com
Did you actually connect the 'view' outlet in IB? Is self.view perhaps
nil?

Question 2: Why not just drop the button in the view in IB?

Drew

Ivan "Rambius" Ivanov

unread,
Sep 6, 2009, 3:58:17 PM9/6/09
to coreplot...@googlegroups.com
Hello,

On Sun, Sep 6, 2009 at 10:49 AM, Drew McCormack<drewmc...@mac.com> wrote:
>
> Did you actually connect the 'view' outlet in IB? Is self.view perhaps
> nil?
>
> Question 2: Why not just drop the button in the view in IB?

I found the issue - the background of the button was wrong so it
actually appeared but can not be seen.

Thank you.

Reply all
Reply to author
Forward
0 new messages