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.
This commit is contained in:
Michael Jordan
2014-03-14 11:34:02 -04:00
parent ab2950874b
commit bcdaa23d35
2 changed files with 78 additions and 1 deletions

View File

@@ -19,6 +19,8 @@
#import <Cordova/CDVPlugin.h>
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

View File

@@ -22,7 +22,9 @@
#import <MediaAccessibility/MediaAccessibility.h>
@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;