Skip to content

Commit ac61299

Browse files
authored
feat(message): Y offset for Html in-app (#76) LP-4281
1 parent 948648a commit ac61299

File tree

1 file changed

+44
-19
lines changed

1 file changed

+44
-19
lines changed

Leanplum-SDK/Classes/LPMessageTemplates.m

Lines changed: 44 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@
7979
#define LPMT_ARG_HTML_HEIGHT @"HTML Height"
8080
#define LPMT_ARG_HTML_WIDTH @"HTML Width"
8181
#define LPMT_ARG_HTML_ALIGN @"HTML Align"
82+
#define LPMT_ARG_HTML_Y_OFFSET @"HTML Y Offset"
8283
#define LPMT_ARG_HTML_ALIGN_TOP @"Top"
8384
#define LPMT_ARG_HTML_ALIGN_BOTTOM @"Bottom"
8485
#define LPMT_ARG_APP_ICON @"__iOSAppIcon"
@@ -456,6 +457,7 @@ - (void)defineActions
456457
[LPActionArg argNamed:LPMT_ARG_HTML_ALIGN withString:LPMT_ARG_HTML_ALIGN_TOP],
457458
[LPActionArg argNamed:LPMT_ARG_HTML_HEIGHT withNumber:@0],
458459
[LPActionArg argNamed:LPMT_ARG_HTML_WIDTH withString:@"100%"],
460+
[LPActionArg argNamed:LPMT_ARG_HTML_Y_OFFSET withString:@"0px"],
459461
[LPActionArg argNamed:LPMT_HAS_DISMISS_BUTTON withBool:NO],
460462
[LPActionArg argNamed:LPMT_ARG_HTML_TEMPLATE withFile:nil]]
461463
withResponder:messageResponder];
@@ -964,39 +966,42 @@ - (void)updatePopupLayout
964966
_popupView.center = CGPointMake(screenWidth / 2.0, screenHeight / 2.0);
965967

966968
if ([context.actionName isEqualToString:LPMT_HTML_NAME]) {
967-
// HTML5 banner logic to support top/bottom alignment with dynamic size.
969+
// Calculate the height. Fullscreen by default.
968970
CGFloat contextArgHeight = [[context numberNamed:LPMT_ARG_HTML_HEIGHT] doubleValue];
969971
CGFloat htmlHeight = contextArgHeight;
970972
if (htmlHeight < 1) {
971973
htmlHeight = screenHeight;
972974
}
973-
CGFloat htmlY = 0;
974-
NSString *htmlAlign = [context stringNamed:LPMT_ARG_HTML_ALIGN];
975-
if ([htmlAlign isEqualToString:LPMT_ARG_HTML_ALIGN_BOTTOM]) {
976-
htmlY = screenHeight - htmlHeight;
977-
}
975+
976+
// Status bar offset.
977+
CGFloat statusBarOffset = 0;
978978
#if LP_NOT_TV
979-
// Offset Banner if the status bar is present.
980979
UIApplication *app = [UIApplication sharedApplication];
981-
if ([htmlAlign isEqualToString:LPMT_ARG_HTML_ALIGN_TOP] &&
982-
contextArgHeight > 1 && !app.statusBarHidden) {
983-
htmlY += app.statusBarFrame.size.height;
980+
if (!app.statusBarHidden) {
981+
statusBarOffset = app.statusBarFrame.size.height;
984982
}
985983
#endif
986984

985+
// Calculate Y Offset.
986+
CGFloat yOffset = 0;
987+
NSString *contextArgYOffset = [context stringNamed:LPMT_ARG_HTML_Y_OFFSET];
988+
if (contextArgYOffset && [contextArgYOffset length] > 0) {
989+
CGFloat percentRange = screenHeight - htmlHeight - statusBarOffset;
990+
yOffset = [self valueFromHtmlString:contextArgYOffset percentRange:percentRange];
991+
}
992+
993+
// HTML banner logic to support top/bottom alignment with dynamic size.
994+
CGFloat htmlY = yOffset + statusBarOffset;
995+
NSString *htmlAlign = [context stringNamed:LPMT_ARG_HTML_ALIGN];
996+
if ([htmlAlign isEqualToString:LPMT_ARG_HTML_ALIGN_BOTTOM]) {
997+
htmlY = screenHeight - htmlHeight - yOffset - statusBarOffset;
998+
}
999+
9871000
// Calculate HTML width by percentage or px (it parses any suffix for extra protection).
9881001
NSString *contextArgWidth = [context stringNamed:LPMT_ARG_HTML_WIDTH] ?: @"100%";
9891002
CGFloat htmlWidth = screenWidth;
9901003
if (contextArgWidth && [contextArgWidth length] > 0) {
991-
if ([contextArgWidth hasSuffix:@"%"]) {
992-
NSString *percentageValue = [contextArgWidth stringByReplacingOccurrencesOfString:@"%"
993-
withString:@""];
994-
htmlWidth = screenWidth * [percentageValue floatValue] / 100.;
995-
} else {
996-
NSCharacterSet *letterSet = [NSCharacterSet letterCharacterSet];
997-
NSArray *components = [contextArgWidth componentsSeparatedByCharactersInSet:letterSet];
998-
htmlWidth = [[components componentsJoinedByString:@""] floatValue];
999-
}
1004+
htmlWidth = [self valueFromHtmlString:contextArgWidth percentRange:screenWidth];
10001005
}
10011006

10021007
CGFloat htmlX = (screenWidth - htmlWidth) / 2.;
@@ -1025,6 +1030,26 @@ - (void)updatePopupLayout
10251030
}
10261031
}
10271032

1033+
/**
1034+
* Get float value by parsing the html string that can have either % or px as a suffix.
1035+
*/
1036+
- (CGFloat)valueFromHtmlString:(NSString *)htmlString percentRange:(CGFloat)percentRange
1037+
{
1038+
if (!htmlString || [htmlString length] == 0) {
1039+
return 0;
1040+
}
1041+
1042+
if ([htmlString hasSuffix:@"%"]) {
1043+
NSString *percentageValue = [htmlString stringByReplacingOccurrencesOfString:@"%"
1044+
withString:@""];
1045+
return percentRange * [percentageValue floatValue] / 100.;
1046+
}
1047+
1048+
NSCharacterSet *letterSet = [NSCharacterSet letterCharacterSet];
1049+
NSArray *components = [htmlString componentsSeparatedByCharactersInSet:letterSet];
1050+
return [[components componentsJoinedByString:@""] floatValue];
1051+
}
1052+
10281053
- (CGSize)getTextSizeFromButton:(UIButton *)button
10291054
{
10301055
UIFont* font = button.titleLabel.font;

0 commit comments

Comments
 (0)