Skip to content

Commit ad36a15

Browse files
author
trancee
committed
Added width to the styles configuration to set width of InfoWindow.
1 parent 44f6f1e commit ad36a15

1 file changed

Lines changed: 60 additions & 55 deletions

File tree

src/ios/GoogleMaps/GoogleMapsViewController.m

Lines changed: 60 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -372,7 +372,27 @@ -(UIView *)mapView:(GMSMapView *)mapView markerInfoWindow:(GMSMarker*)marker
372372
rightImg = [self loadImageFromGoogleMap:@"bubble_right@2x"];
373373
float scale = leftImg.scale;
374374
int sizeEdgeWidth = 10;
375-
375+
376+
int width = 0;
377+
378+
if (styles && [styles objectForKey:@"width"]) {
379+
NSString *widthString = [styles valueForKey:@"width"];
380+
381+
if ([widthString hasSuffix:@"%"]) {
382+
double widthDouble = [[widthString stringByReplacingOccurrencesOfString:@"%" withString:@""] doubleValue];
383+
384+
width = (int)((double)mapView.frame.size.width * (widthDouble / 100));
385+
} else if ([widthString isNumeric:widthString]) {
386+
double widthDouble = [widthString doubleValue];
387+
388+
if (widthDouble <= 1) {
389+
width = (int)((double)mapView.frame.size.width * (widthDouble));
390+
} else {
391+
width = (int)widthDouble;
392+
}
393+
}
394+
}
395+
376396
//-------------------------------------
377397
// Calculate the size for the contents
378398
//-------------------------------------
@@ -382,16 +402,14 @@ -(UIView *)mapView:(GMSMapView *)mapView markerInfoWindow:(GMSMarker*)marker
382402
isTextMode = false;
383403
NSArray *tmp = [title componentsSeparatedByString:@","];
384404
NSData *decodedData;
385-
#if !defined(__IPHONE_8_0)
386-
decodedData = [[NSData alloc] initWithBase64Encoding:(NSString *)tmp[1]];
387-
#else
405+
#ifdef __IPHONE_7_0
388406
if ([PluginUtil isIOS7_OR_OVER]) {
389-
decodedData = [NSData dataFromBase64String:tmp[1]];
407+
decodedData = [[NSData alloc] initWithBase64Encoding:(NSString *)tmp[1]];
390408
} else {
391-
#if !defined(__IPHONE_7_0)
392-
decodedData = [[NSData alloc] initWithBase64Encoding:(NSString *)tmp[1]];
393-
#endif
409+
decodedData = [NSData dataFromBase64String:tmp[1]];
394410
}
411+
#else
412+
decodedData = [NSData dataFromBase64String:tmp[1]];
395413
#endif
396414

397415
base64Image = [[UIImage alloc] initWithData:decodedData];
@@ -412,15 +430,15 @@ -(UIView *)mapView:(GMSMapView *)mapView markerInfoWindow:(GMSMarker*)marker
412430
}
413431
}
414432
if (isBold == TRUE && isItalic == TRUE) {
415-
#ifdef __IPHONE_7_0
433+
if ([PluginUtil isIOS7_OR_OVER] == true) {
416434
// ref: http://stackoverflow.com/questions/4713236/how-do-i-set-bold-and-italic-on-uilabel-of-iphone-ipad#21777132
417435
titleFont = [UIFont systemFontOfSize:17.0f];
418436
UIFontDescriptor *fontDescriptor = [titleFont.fontDescriptor
419437
fontDescriptorWithSymbolicTraits:UIFontDescriptorTraitBold | UIFontDescriptorTraitItalic];
420438
titleFont = [UIFont fontWithDescriptor:fontDescriptor size:0];
421-
#else
439+
} else {
422440
titleFont = [UIFont fontWithName:@"Helvetica-BoldOblique" size:17.0];
423-
#endif
441+
}
424442
} else if (isBold == TRUE && isItalic == FALSE) {
425443
titleFont = [UIFont boldSystemFontOfSize:17.0f];
426444
} else if (isBold == TRUE && isItalic == FALSE) {
@@ -430,33 +448,14 @@ -(UIView *)mapView:(GMSMapView *)mapView markerInfoWindow:(GMSMarker*)marker
430448
}
431449

432450
// Calculate the size for the title strings
433-
CGSize tmpSize = CGSizeMake(mapView.frame.size.width - 13, mapView.frame.size.height - 13);
434-
#if !defined(__IPHONE_8_0)
435-
textSize = [title sizeWithFont:titleFont constrainedToSize: tmpSize];
436-
#else
437-
NSDictionary *attr = @{ NSFontAttributeName: titleFont};
438-
textSize = [title boundingRectWithSize:tmpSize
439-
options:NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingTruncatesLastVisibleLine
440-
attributes:attr
441-
context:nil].size;
442-
#endif
451+
textSize = [title sizeWithFont:titleFont constrainedToSize: CGSizeMake(mapView.frame.size.width - 13, mapView.frame.size.height - 13)];
443452
rectSize = CGSizeMake(textSize.width + 10, textSize.height + 22);
444453

445454
// Calculate the size for the snippet strings
446455
if (snippet) {
447456
snippetFont = [UIFont systemFontOfSize:12.0f];
448457
snippet = [snippet stringByReplacingOccurrencesOfString:@"\n" withString:@""];
449-
450-
451-
#if !defined(__IPHONE_8_0)
452-
snippetSize = [snippet sizeWithFont:snippetFont constrainedToSize: tmpSize];
453-
#else
454-
NSDictionary *attr = @{ NSFontAttributeName: snippetFont};
455-
snippetSize = [snippet boundingRectWithSize:tmpSize
456-
options:NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingTruncatesLastVisibleLine
457-
attributes:attr
458-
context:nil].size;
459-
#endif
458+
snippetSize = [snippet sizeWithFont:snippetFont constrainedToSize: CGSizeMake(mapView.frame.size.width - 13, mapView.frame.size.height - 13)];
460459
rectSize.height += snippetSize.height + 4;
461460
if (rectSize.width < snippetSize.width + leftImg.size.width) {
462461
rectSize.width = snippetSize.width + leftImg.size.width;
@@ -468,6 +467,10 @@ -(UIView *)mapView:(GMSMapView *)mapView markerInfoWindow:(GMSMarker*)marker
468467
} else {
469468
rectSize.width += sizeEdgeWidth;
470469
}
470+
471+
if (width > 0) {
472+
rectSize.width = width;
473+
}
471474

472475
//-------------------------------------
473476
// Draw the the info window
@@ -594,7 +597,7 @@ -(UIView *)mapView:(GMSMapView *)mapView markerInfoWindow:(GMSMarker*)marker
594597
}
595598

596599
CGRect textRect = CGRectMake(5, 5 , rectSize.width - 10, textSize.height );
597-
#if defined(__IPHONE_7_0)
600+
if ([PluginUtil isIOS7_OR_OVER] == true) {
598601
// iOS7 and above
599602
NSMutableParagraphStyle *style = [[NSMutableParagraphStyle alloc] init];
600603
style.lineBreakMode = NSLineBreakByWordWrapping;
@@ -607,41 +610,43 @@ -(UIView *)mapView:(GMSMapView *)mapView markerInfoWindow:(GMSMarker*)marker
607610
};
608611
[title drawInRect:textRect
609612
withAttributes:attributes];
610-
#else
613+
614+
615+
} else {
611616
// iOS6
612617
[titleColor set];
613618
[title drawInRect:textRect
614619
withFont:titleFont
615620
lineBreakMode:NSLineBreakByWordWrapping
616621
alignment:textAlignment];
617-
#endif
622+
}
618623
//CGContextSetRGBStrokeColor(context, 1.0, 0.0, 0.0, 0.5);
619624
//CGContextStrokeRect(context, textRect);
620625
}
621626

622627
//Draw the snippet
623628
if (snippet) {
624629
CGRect textRect = CGRectMake(5, textSize.height + 10 , rectSize.width - 10, snippetSize.height );
625-
#if defined(__IPHONE_7_0)
626-
// iOS7 and above
627-
NSMutableParagraphStyle *style = [[NSMutableParagraphStyle alloc] init];
628-
style.lineBreakMode = NSLineBreakByWordWrapping;
629-
style.alignment = textAlignment;
630-
631-
NSDictionary *attributes = @{
632-
NSForegroundColorAttributeName : [UIColor grayColor],
633-
NSFontAttributeName : snippetFont,
634-
NSParagraphStyleAttributeName : style
635-
};
636-
[snippet drawInRect:textRect withAttributes:attributes];
637-
#else
638-
// iOS6
639-
[[UIColor grayColor] set];
640-
[snippet drawInRect:textRect
641-
withFont:snippetFont
642-
lineBreakMode:NSLineBreakByWordWrapping
643-
alignment:textAlignment];
644-
#endif
630+
if ([PluginUtil isIOS7_OR_OVER] == true) {
631+
// iOS7 and above
632+
NSMutableParagraphStyle *style = [[NSMutableParagraphStyle alloc] init];
633+
style.lineBreakMode = NSLineBreakByWordWrapping;
634+
style.alignment = textAlignment;
635+
636+
NSDictionary *attributes = @{
637+
NSForegroundColorAttributeName : [UIColor grayColor],
638+
NSFontAttributeName : snippetFont,
639+
NSParagraphStyleAttributeName : style
640+
};
641+
[snippet drawInRect:textRect withAttributes:attributes];
642+
} else {
643+
// iOS6
644+
[[UIColor grayColor] set];
645+
[snippet drawInRect:textRect
646+
withFont:snippetFont
647+
lineBreakMode:NSLineBreakByWordWrapping
648+
alignment:textAlignment];
649+
}
645650
}
646651
} else {
647652
//Draw the content image

0 commit comments

Comments
 (0)