calloutview icon indicating copy to clipboard operation
calloutview copied to clipboard

Is it possible to increase the view height and allow a multiline subtitle?

Open bergstrat94 opened this issue 7 years ago • 16 comments

Hey there,

I am wanting to increase the height of the view and display a multi line subtitle. Is this is supported?

bergstrat94 avatar Oct 18 '17 18:10 bergstrat94

You might try creating a custom view for your subtitle and setting this view for the callout's subtitleView property. For example (in Swift):

let customSubtitleView = UILabel(frame: .zero)
customSubtitleView.numberOfLines = 2
customSubtitleView.text = "Line 1\nLine 2"
let myCalloutView = SMCalloutView.platform()
myCalloutView.subtitleView = customSubtitleView

namannik avatar Oct 18 '17 19:10 namannik

Thanks for your reply.

I am pretty new to IOS dev.. Would you be able to show me how to do it in objective C?

bergstrat94 avatar Oct 18 '17 20:10 bergstrat94

This hasn't been tested, but here's what the equivalent in Objective-C should be:

UILabel *customSubtitleView = [[UILabel alloc] initWithFrame:CGRectZero];
customSubtitleView.numberOfLines = 2;
customSubtitleView.text = @"Line 1\nLine 2";
SMCalloutView *myCalloutView = [SMCalloutView platformCalloutView];
myCalloutView.subtitleView = customSubtitleView;

namannik avatar Oct 18 '17 21:10 namannik

Thank you. I tried doing that and it seems to not be showing in the calloutview.

bergstrat94 avatar Oct 18 '17 21:10 bergstrat94

Can you post the portion of code where you're creating the callout and setting the subtitle view?

namannik avatar Oct 18 '17 21:10 namannik

Here ya go: calloutView = [[SMCalloutView alloc] init]; calloutView.title = m.title; calloutView.hidden = NO; UILabel *customSubtitleView = [[UILabel alloc] initWithFrame:CGRectZero]; customSubtitleView.numberOfLines = 2; customSubtitleView.text = @"Line 1\nLine 2"; calloutView.subtitleView = customSubtitleView; calloutView.calloutOffset = CGPointMake(0, -CalloutYOffset); calloutView.rightAccessoryView = nil; CGRect calloutRect = CGRectZero; calloutRect.origin = point; calloutRect.size = CGSizeZero; [calloutView presentCalloutFromRect:calloutRect inView:mapView_ constrainedToView:mapView_ animated:YES];

bergstrat94 avatar Oct 18 '17 21:10 bergstrat94

Maybe try setting a larger size instead of CGRectZero when creating the subtitle:

UILabel *customSubtitleView = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 50, 30)];

(The sizes I used here are sort of arbitrary. I think the callout view is supposed to resize the subtitle to fit the text.)

namannik avatar Oct 18 '17 22:10 namannik

Okay, I will try that out. Thanks for your help :)

bergstrat94 avatar Oct 18 '17 22:10 bergstrat94

I have yet to try the above suggestion however I was able to get behaviour similar to the desired behaviour by messing around with the subtitle and frame heights as well as specifying a number of lines for the subtitlelabel

bergstrat94 avatar Oct 18 '17 22:10 bergstrat94

I tried the above suggestion and It works! sort of.. It seems the calloutview is not adjusting according to the size of the subtitle view

bergstrat94 avatar Oct 18 '17 22:10 bergstrat94

What happens if you call sizeToFit on the subtitle?

[customSubtitleView sizeToFit];

namannik avatar Oct 18 '17 22:10 namannik

So, before adding that I would see "Line 1..." as the subtitle

With the above, I see "Line 1" and can see the top of the L for the second line.

The height of the calloutview itself does not change however.

bergstrat94 avatar Oct 18 '17 22:10 bergstrat94

Another thing you could try is wrapping your subtitle view in another view. According to the documentation in SMCalloutView.h:

wrap your view in a "generic" UIView if you do not want it to be auto-sized

UIView *subtitleView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 50, 30)];
[subtitleView addSubview:customSubtitleView];
myCalloutView.subtitleView = subtitleView;

namannik avatar Oct 18 '17 22:10 namannik

I will try this. Currently I have it working by altering the following line in CalloutContainerHeight

return (CALLOUT_SUB_DEFAULT_CONTAINER_HEIGHT + self.subtitleView.frameHeight - 15);

bergstrat94 avatar Oct 18 '17 22:10 bergstrat94

That will work, but I've found it's usually a good idea to keep third-party libraries unmodified so that they can be easily replaced with a newer version in the future. (I've learned this lesson the hard way.)

namannik avatar Oct 18 '17 22:10 namannik

Good advice! I wrapped the view as you suggested but it fails to resize accordingly.

bergstrat94 avatar Oct 18 '17 23:10 bergstrat94