From bcdaa23d359bf8d866014973fa17e1c36cab5eef Mon Sep 17 00:00:00 2001 From: Michael Jordan Date: Fri, 14 Mar 2014 11:34:02 -0400 Subject: [PATCH] Add support for adjusting text in the WebView to the preferred zoom scale on iOS 7. iOS will now support adjusting text to preferred point size. --- src/ios/CDVMobileAccessibility.h | 9 ++++ src/ios/CDVMobileAccessibility.m | 70 +++++++++++++++++++++++++++++++- 2 files changed, 78 insertions(+), 1 deletion(-) diff --git a/src/ios/CDVMobileAccessibility.h b/src/ios/CDVMobileAccessibility.h index 6fdb549..cdf08a2 100644 --- a/src/ios/CDVMobileAccessibility.h +++ b/src/ios/CDVMobileAccessibility.h @@ -19,6 +19,8 @@ #import +static const int BASE_UI_FONT_TEXT_STYLE_BODY_POINT_SIZE = 16; + @interface CDVMobileAccessibility : CDVPlugin { NSString* callbackId; NSString* commandCallbackId; @@ -36,14 +38,21 @@ @property BOOL guidedAccessEnabled; @property BOOL invertColorsEnabled; @property BOOL monoAudioEnabled; +@property float mFontScale; + - (void) isScreenReaderRunning:(CDVInvokedUrlCommand*)command; - (void) isClosedCaptioningEnabled:(CDVInvokedUrlCommand*)command; - (void) isGuidedAccessEnabled:(CDVInvokedUrlCommand*)command; - (void) isInvertColorsEnabled:(CDVInvokedUrlCommand*)command; - (void) isMonoAudioEnabled:(CDVInvokedUrlCommand*)command; +- (void) getTextZoom:(CDVInvokedUrlCommand*)command; +- (void) setTextZoom:(CDVInvokedUrlCommand*)command; +- (void) updateTextZoom:(CDVInvokedUrlCommand*)command; - (void) postNotification:(CDVInvokedUrlCommand*)command; - (void) start:(CDVInvokedUrlCommand*)command; - (void) stop:(CDVInvokedUrlCommand*)command; @end + + diff --git a/src/ios/CDVMobileAccessibility.m b/src/ios/CDVMobileAccessibility.m index b68c98f..d0d89d0 100644 --- a/src/ios/CDVMobileAccessibility.m +++ b/src/ios/CDVMobileAccessibility.m @@ -22,7 +22,9 @@ #import @interface CDVMobileAccessibility () -// add any property overrides + // add any property overrides + -(int) mGetTextZoom; + -(void) mSetTextZoom:(int)zoom; @end @implementation CDVMobileAccessibility @@ -34,6 +36,9 @@ @synthesize guidedAccessEnabled; @synthesize invertColorsEnabled; @synthesize monoAudioEnabled; +@synthesize mFontScale; + +#define iOS7Delta (([[[UIDevice currentDevice] systemVersion] floatValue] >= 7.0 ) ? 20 : 0 ) // ////////////////////////////////////////////////// @@ -53,7 +58,11 @@ // set your setting, other init here } + mFontScale = 1; + + [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(onPause) name:UIApplicationDidEnterBackgroundNotification object:nil]; + } // ////////////////////////////////////////////////// @@ -138,6 +147,65 @@ }]; } +-(float) mGetFontScale +{ + float fontScale = 1; + if (iOS7Delta) { + fontScale = [[UIFont preferredFontForTextStyle:UIFontTextStyleBody] pointSize] / BASE_UI_FONT_TEXT_STYLE_BODY_POINT_SIZE; + } + return fontScale; +} + +-(int) mGetTextZoom +{ + int zoom = round(mFontScale * 100); + // NSLog(@"mGetTextZoom %d%%'", zoom); + return zoom; +} + +- (void) getTextZoom:(CDVInvokedUrlCommand *)command +{ + int zoom = [self mGetTextZoom]; + [self.commandDelegate runInBackground:^{ + CDVPluginResult* result = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsInt: zoom]; + [self.commandDelegate sendPluginResult:result callbackId:command.callbackId]; + }]; +} + +-(void) mSetTextZoom:(int)zoom +{ + // NSLog(@"mSetTextZoom %d%%'", zoom); + mFontScale = zoom/100; + if (iOS7Delta) { + NSString *jsString = [[NSString alloc] initWithFormat:@"document.getElementsByTagName('body')[0].style.webkitTextSizeAdjust= '%d%%'", zoom]; + [[self webView] stringByEvaluatingJavaScriptFromString:jsString]; + } +} + +- (void) setTextZoom:(CDVInvokedUrlCommand *)command +{ + if (command != nil && [command.arguments count] > 0) { + int zoom = [[command.arguments objectAtIndex:0] intValue]; + [self mSetTextZoom:zoom]; + + [self.commandDelegate runInBackground:^{ + CDVPluginResult* result = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsInt:zoom]; + [self.commandDelegate sendPluginResult:result callbackId:command.callbackId]; + }]; + } +} + +- (void)updateTextZoom:(CDVInvokedUrlCommand *)command +{ + float fontScale = [self mGetFontScale]; + if (fontScale != mFontScale) { + mFontScale = fontScale; + } + // NSLog(@"updateTextZoom %d%%'", [self mGetTextZoom]); + [self mSetTextZoom:[self mGetTextZoom]]; +} + + - (void)postNotification:(CDVInvokedUrlCommand *)command { CDVPluginResult* result = nil;