Skip to content

Commit

Permalink
Image Accessibility (#543)
Browse files Browse the repository at this point in the history
* Configure Accessibility Parameters on Images.
  • Loading branch information
orospakr committed Oct 30, 2020
1 parent 34832a4 commit 7edab98
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 5 deletions.
6 changes: 5 additions & 1 deletion Sources/Models/Image.swift
Expand Up @@ -15,13 +15,17 @@ public struct Image: Decodable {
public var size: Int
public var width: Int
public var url: URL
public var accessibilityLabel: String?
public var isDecorative: Bool

public init(height: Int, isURLOptimizationEnabled: Bool, name: String, size: Int, width: Int, url: URL) {
public init(height: Int, isURLOptimizationEnabled: Bool, name: String, size: Int, width: Int, url: URL, accessibilityLabel: String?, isDecorative: Bool) {
self.height = height
self.isURLOptimizationEnabled = isURLOptimizationEnabled
self.name = name
self.size = size
self.width = width
self.url = url
self.accessibilityLabel = accessibilityLabel
self.isDecorative = isDecorative
}
}
3 changes: 3 additions & 0 deletions Sources/UI/ScreenViewController.swift
Expand Up @@ -240,6 +240,9 @@ open class ScreenViewController: UICollectionViewController, UICollectionViewDat

let background = screen.background

backgroundImageView.isAccessibilityElement = !(background.image?.isDecorative ?? true)
backgroundImageView.accessibilityLabel = background.image?.accessibilityLabel

switch background.contentMode {
case .fill:
backgroundImageView.contentMode = .scaleAspectFill
Expand Down
11 changes: 7 additions & 4 deletions Sources/UI/Views/BlockCell.swift
Expand Up @@ -91,17 +91,20 @@ class BlockCell: UICollectionViewCell {
let view = content ?? self

guard !(block is TextPollBlock), !(block is ImagePollBlock), !(block is WebViewBlock) else {
// Polls and webviews implement their own a11y.
// Polls and WebViews implement their own accessibility.
return
}

// Some Rover blocks do not currently have a11y alternative descriptions available.
let hasContent = !(block is ImageBlock) && !(block is RectangleBlock)
// true if the block is an image and is marked as isDecorative.
let isImageWithoutContent = ((block as? ImageBlock)?.image.isDecorative) ?? false

// Some Rover blocks should not be visible to accessibility:
let hasContent = !(block is RectangleBlock) && !isImageWithoutContent

// All Rover blocks that have meaningful content should be a11y, or if they at least have tap behaviour.
view.isAccessibilityElement = hasContent || block.tapBehavior != .none

// tapbehaviour be mapped to the `link` a11y trait:
// TabBehavior is mapped to the `link` accessibility trait:
switch block.tapBehavior {
case .goToScreen(_), .openURL(_, _), .presentWebsite(_):
view.accessibilityTraits.applyTrait(trait: .link, to: true)
Expand Down
3 changes: 3 additions & 0 deletions Sources/UI/Views/Extensions/UIImageView.swift
Expand Up @@ -16,6 +16,9 @@ extension UIImageView {
self.alpha = 0.0
self.image = nil

self.isAccessibilityElement = background?.image.map { !$0.isDecorative } ?? false
self.accessibilityLabel = background?.image?.accessibilityLabel

// Background color is used for tiled backgrounds
self.backgroundColor = UIColor.clear

Expand Down
2 changes: 2 additions & 0 deletions Sources/UI/Views/ImageCell.swift
Expand Up @@ -30,6 +30,8 @@ class ImageCell: BlockCell {
imageView.alpha = 0.0
imageView.image = nil

imageView.accessibilityLabel = imageBlock.image.accessibilityLabel

if let image = ImageStore.shared.image(for: imageBlock.image, frame: frame) {
imageView.image = image
imageView.alpha = 1.0
Expand Down

0 comments on commit 7edab98

Please sign in to comment.