Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Accessibility #263

Merged
merged 10 commits into from
Jan 27, 2017
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ DerivedData
*.hmap
*.ipa

# AppCode
.idea

# Bundler
.bundle
vendor
36 changes: 27 additions & 9 deletions Example-iOS/DemoStrings.swift
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,10 @@ enum DemoStrings {
let localizedString = "I want to be different. If everyone is wearing <black><BON:noBreakSpace/>black,<BON:noBreakSpace/></black> I want to be wearing <red><BON:noBreakSpace/>red.<BON:noBreakSpace/></red>\n<signed><BON:emDash/>Maria Sharapova</signed> <racket/>"

// Define a colored image that's slightly shifted to account for the line height
let racket = UIImage(named: "Tennis Racket")!.styled(with:
let accessibleRacketImage = UIImage(named: "Tennis Racket")!
// The racket is purely for decoration, so make the accessibility system ignore it
accessibleRacketImage.accessibilityLabel = ""
let racket = accessibleRacketImage.styled(with:
.color(.raizlabsRed), .baselineOffset(-4.0))

// Define styles
Expand Down Expand Up @@ -65,7 +68,9 @@ enum DemoStrings {
/// composition only if you absolutely need to build the string from pieces.
static let compositionExample: NSAttributedString = {
// Define a colored image that's slightly shifted to account for the line height
let boat = UIImage(named: "boat")!.styled(with:
let accessibleBoatImage = UIImage(named: "boat")!
accessibleBoatImage.accessibilityLabel = "boat"
let boat = accessibleBoatImage.styled(with:
.color(.raizlabsRed))

let baseStyle = StringStyle(
Expand All @@ -90,17 +95,30 @@ enum DemoStrings {
], baseStyle: baseStyle)
}()

static let imagesExample = NSAttributedString.composed(of: [
"2".styled(with: .baselineOffset(8)),
UIImage(named: "bee")!,
UIImage(named: "oar")!,
UIImage(named: "knot")!,
"2".styled(with: .baselineOffset(8)),
UIImage(named: "bee")!,
static let imagesExample: NSAttributedString = {

func accessibleImage(named name: String) -> UIImage {
let image = UIImage(named: name)!
image.accessibilityLabel = name
return image
}

let bee = accessibleImage(named: "bee")
let oar = accessibleImage(named: "oar")
let knot = accessibleImage(named: "knot")

return NSAttributedString.composed(of: [
"2".styled(with: .baselineOffset(8)),
bee,
oar,
knot,
"2".styled(with: .baselineOffset(8)),
bee,
], baseStyle: StringStyle(
.font(UIFont(name: "HelveticaNeue-Bold", size: 24)!),
.adapt(.control)
))
}()

static let noBreakSpaceExample: NSAttributedString = {
let noSpaceTextStyle = StringStyle(
Expand Down
8 changes: 8 additions & 0 deletions Sources/Image+Tinting.swift
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,9 @@ public extension BONImage {
// Prevent further tinting
image.isTemplate = false

// Transfer accessibility description
image.accessibilityDescription = self.accessibilityDescription

return image
}
#else
Expand Down Expand Up @@ -113,6 +116,11 @@ public extension BONImage {
image = image.resizableImage(withCapInsets: originalCapInsets, resizingMode: originalResizingMode)
}

// Transfer accessibility label (watchOS not included; does not have accessibilityLabel on UIImage).
#if os(iOS) || os(tvOS)
image.accessibilityLabel = self.accessibilityLabel
#endif

return image
}
#endif
Expand Down
19 changes: 19 additions & 0 deletions Tests/ImageTintingTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ class ImageTintingTests: XCTestCase {
let raizlabsRed = UIColor(red: 0.92549, green: 0.352941, blue: 0.301961, alpha: 1.0)
#endif

let accessibilityDescription = "I’m the very model of a modern accessible image."

func testImageTinting() {
let blackImageName = "rz-logo-black"
let redImageName = "rz-logo-red"
Expand Down Expand Up @@ -85,4 +87,21 @@ class ImageTintingTests: XCTestCase {
BONAssertEqualImages(untintedResult!, tintAttemptResult!)
}

func testAccessibilityIOSAndTVOS() {
#if os(iOS) || os(tvOS)
imageForTest.accessibilityLabel = accessibilityDescription
let tintedImage = imageForTest.tintedImage(color: raizlabsRed)
XCTAssertEqual(tintedImage.accessibilityLabel, accessibilityDescription)
XCTAssertEqual(tintedImage.accessibilityLabel, tintedImage.accessibilityLabel)
#endif
}

func testAccessibilityOSX() {
#if os(OSX)
imageForTest.accessibilityDescription = accessibilityDescription
let tintedImage = imageForTest.tintedImage(color: raizlabsRed)
XCTAssertEqual(tintedImage.accessibilityDescription, accessibilityDescription)
XCTAssertEqual(tintedImage.accessibilityDescription, tintedImage.accessibilityDescription)
#endif
}
}