





























Text Zoom - Simple example
If you zoom the text in this demo over 113%, you will see this text reposition itself to ensure it doesn't obscure important parts of this photo.
In order to see the above demo in action, resize/zoom just the text on this page over 113%. Do not zoom all the content on this page, just the text. If you have never done this before, jump to the section of this article that shows how to resize text in the browser of your choice. You can also see a clean room example of this demo and compare it with a page that doesn't have the fix applied.
As a user, I find text resizing quite helpful. Ever since I started wearing glasses 10 years ago, the first thing I do when I get a new mobile device is open up the browser preferences and change the default size of text (Note that I am not talking about resizing everything via page zoom. I mean resizing just the text, since I find horizontal scrolling through a zoomed page very annoying). I am sure I am not the only one: there are a lot of tech savvy people are over 35, which is the age when a lot of people normally lose sharpness in their vision. WCAG 2.1 has a guideline to help people with all sorts of vision loss, but given the terse read W3C documentation, I’m sure a lot of you will appreciate this simplified explanation of the “Resize Text” guideline: “you should allow for up to a 200% resize of text without dropping any content or functions”.
As a developer, you must ensure your site can resize text. This means avoiding using pixels in CSS for font-size and using em or rem instead, since Chrome and IE will not resize font-sizes that are defined in pixels. If you want still want to use pixels for font-sizing, but want your favourite CSS pre-processor to convert these values to rems for you, CSS tricks has a great page to help you out.
As a developer, you must also ensure that people can still use your site when the text is resized. Here’s the scenario: you have an existing website that has been audited and accessibility issues have been raised. The auditor has found that when the text in a component on the site has been resized or zoomed 200%, the text is not legible (due to contrast issues, the text is cut off, text is running into other text, or similar reasons). After 10 minutes of looking into the code, you realize that the problem is not easily fixed with CSS or a simple markup change, due to the way the component is styled, and that there is no media query that is directed towards resized text. You will have to spend hours refactoring the code … unless you can find a way to capture an event that fires when you resize the text so you can put special classes on the top of the component when the problems happen. No such native event exists today! If only there was a lightweight JavaScript library that can do this today!
How do you solve this problem? Use my text-zoom-resize JavaScript package, available both at the text-zoom-resize GitHub repository or using npm to import it into your current project
You can include the project into your page using a plain ol’ script tag:
<script src="path/to/textZoomEvent.js"></script>
The textZoomEvent object also exports itself as a module, so you can use it with npm and import it into your project easily.
Before using an event handler to capture the event, you must initialize the library using textZoomEvent.init():
// You must give this value as the argument for the following // function when the doc is not zoomed: // // parseFloat(getComputedStyle(document.documentElement).fontSize // textZoomEvent.init(16);
As mentioned in the comment, you must give the default, unzoomed value of the document element as an argument in order for this script to work. This is so the script can understand if the page loaded with the text already resized (most, if not all, browsers will persist the text zoom level for a website, even when reloading a page, until the user resets it manually).
resizeFactorYou can find the current zoom factor using textZoomEvent.resizeFactor(). It is advised to check this as soon as the script is initialized in order to find if the page’s text is current resized.
textzoom eventNow create a textzoom event handler so you can find out when the user resizes the text:
You can also use the textzoom event to fire when the user zooms the text with their browser:
document.addEventListener('textzoom', textZoomEvent);
Note that you can find the resizeFactor using the event object inside the textzoom event handler:
function textZoomEvent(e) {
console.log('The current text resize factor is', e.detail.resizeFactor());
}
Now to test! Most browsers support text zoom, but many developers (and users) don’t know how to use this feature. Here is what you need to know:
body {
/*
* This tells Safari to use the OS's base font and
* the size set in the iOS Accessibility settings.
*/
font: -apple-system-body;
/*
* Put whatever font you want to use here. The font
* size will still be grabbed by the iOS Accessibility
* settings.
*/
font-family: "Times New Roman", serif;
}
I encourage everyone to put these styles in their base styles. It will make visually impaired iOS users happy. The only caveat here is that the font resize will not happen until after the user refreshes the browser. Thanks to the user “clshortf…@gmail.com” in this Chromium bug report for sharing this info.
Note that Chrome will not resize text that is sized in px units.
Please note that Chrome for Android has some serious differences than all other browsers. Text is only resized inside HTML element has more than 217 characters in it, and only if they have a dynamic height. This is not useful as an accessibility feature, since it is not guaranteed to resize all the content on the page. Because of this, text-zoom-resize does not support Chrome for Android.
A bug has been filed a year and a half ago with Google on this issue, and I have submitted my own comments to it. Hopefully this will be resolved soon.
(A more visual representation of the second step above can be found at How to Change the Size of Text, Icons, and More in Android at the How To Geek website).
px units.(This list was lifted from Zoom & Resizing Text from Yale University’s Usability & Web Accessibility site).
To make a long story short, the library inserts a visually hidden <iframe> tag at the beginning of the page (this iframe is also hidden from screen readers using aria-hidden="true"). The width of this iframe is 1em. Because its width is measured in em measurements, when a user resizes text, the iframe’s pixel width will change and will fire a resize event. We then find out what the iframe’s offsetWidth is, divide that from the value we passed in step 1, and we get the resizeFactor. A simple solution that solves a difficult problem.
<iframe> resize event is not new. I borrowed this idea from this blog post by Hedgerow retrieved from The Wayback Machine此内容由惯性聚合(RSS阅读器)自动聚合整理,仅供阅读参考。 原文来自 — 版权归原作者所有。