Skip to content

Instantly share code, notes, and snippets.

View dadederk's full-sized avatar
💻

Daniel Devesa dadederk

💻
View GitHub Profile
@dadederk
dadederk / MyChartView.swift
Created September 2, 2022 17:02
Code example defining an accessibiliyt chart descriptor for a view. Trying to specify some gridlines in the x axes.
import Foundation
import Accessibility
import UIKit
class MyChartView: UIView, AXChart {
var accessibilityChartDescriptor: AXChartDescriptor? {
get {
let xAxis = AXNumericDataAxisDescriptor(title: "Lines of code", range: 0...10, gridlinePositions: [2.5, 5.0, 7.5]) { number in
"\(number) lines"
}
@dadederk
dadederk / ContentView.swift
Created August 24, 2022 09:33
Trying to figure out how .accessibilityLabeledPair(role: id: in:) works.
import SwiftUI
struct ContentView: View {
@Namespace var namespace
@State private var isOn = false
var body: some View {
HStack {
Text("I agree on the terms and conditions")
.accessibilityLabeledPair(role: .label, id: "aPair", in: namespace)
@dadederk
dadederk / ImageAttachment.swift
Last active July 14, 2022 11:14
Set an appropriate accessibility label to a UILabel if it is configured with an attributed string that contains an image attachment. #365DaysIOSAccessibility. Day 57.
let magnifyingGlassIcon = UIImage(systemName: "magnifyingglass")!
let searchButton = UIButton()
let searchTutorialLabel = UILabel()
searchButton.accessibilityLabel = "search"
let textAttachment = NSTextAttachment(image: magnifyingGlassIcon)
let string = "Select the <icon> button to find elements in the list"
let attributedString = NSMutableAttributedString(string: string)
let attributedStringIcon = NSAttributedString(attachment: textAttachment)
let iconPlaceholderRange = attributedString.string.range(of: "<icon>")!
@dadederk
dadederk / AccessibilitySliderValue.swift
Created June 21, 2022 13:18
The accessibility value of a slider can be overridden so the units of the value makes sense. #365DaysIOSAccessibility. Day 34.
override var accessibilityValue: String? {
get {
let formatter = MeasurementFormatter()
let measurement = Measurement<UnitLength>(
value: Double(value),
unit: .kilometers
)
formatter.unitStyle = .long
return formatter.string(from: measurement)
}
@dadederk
dadederk / AccessibilityAdjustable.swift
Created June 21, 2022 13:16
You can implement accessibility increment and decrement to adjust the accessibility value of a component with the adjustable accessibility trait. #365DaysIOSAccessibility. Day 33.
override func accessibilityIncrement() {
guard value < 5 else { return }
value += 1
accessibilityValue = "\(value) of 5"
sendActions(for: .valueChanged)
}
override func accessibilityDecrement() {
guard value > 1 else { return }
value -= 1
@dadederk
dadederk / DateComponentsFormatter.swift
Created June 2, 2022 21:55
Use the spellOut style on Date Components Formatters to get a readable version of durations suitable for accessibility labels. #365DaysIOSAccessibility. Day 15.
let dailyAverageLabel = UILabel()
let abbreviatedReadableFormatter = DateComponentsFormatter()
abbreviatedReadableFormatter.allowedUnits = [.hour, .minute]
abbreviatedReadableFormatter.unitsStyle = .spellOut
let abbreviatedReadableDuration = abbreviatedReadableFormatter.string(from: 12240)
dailyAverageLabel.accessibilityLabel = abbreviatedReadableDuration
@dadederk
dadederk / AccessibilitySpeechSpellOut.swift
Created May 31, 2022 09:54
Make VoiceOver to spell out characters in a label using an accessibility attributed label and the accessibilitySpeechSpellOut attribute. #365DaysIOSAccessibility. Day 12.
let codeLabel = UILabel()
let attributedLabel = NSAttributedString(
string: "BAC1234567D",
attributes: [.accessibilitySpeechSpellOut: true]
)
title.accessibilityAttributedLabel = attributedLabel
@dadederk
dadederk / AccessibilitySpeechPunctuation.swift
Last active May 31, 2022 09:56
Get VoiceOver to speak punctuation marks by using an accessibility attributed label and the accessibilitySpeechPunctuation attribute. #365DaysIOSAccessibility. Day 13.
let codeLabel = UILabel()
let attributedLabel = NSAttributedString(
string: "let myString: String = \"Hello world\"",
attributes: [.accessibilitySpeechPunctuation: true]
)
codeLabel.accessibilityAttributedLabel = attributedLabel
@dadederk
dadederk / AccessibilitySpeechPitch.swift
Created May 31, 2022 09:43
Change VoiceOver's pitch using an accessibility attributed label and the accessibilitySpeechPitch attribute. #365DaysIOSAccessibility. Day 11.
let tweetView = UIView()
let userName = "Daniel Devesa Derksen-Staats"
let userHandle = "@dadederk"
let timestamp = ""
let text = "Tweeting about developing accessible #iOS apps in #365daysIOSAccessibility #accessibility #a11y" // 80, 92, 115
// Returns pairs of hashtag strings and the range in the tweet
func hashtags(_ string: String) -> [(String, NSRange)] {...}
let tweetLabel = [userName, userHandle, timestamp, text].joined(separator: ". ")
@dadederk
dadederk / AccessibilitySpeechIPANotation.swift
Last active May 31, 2022 09:57
Correct VoiceOver's pronunciation using an accessibility attributed label and the accessibilitySpeechIPANotation attribute. #365DaysIOSAccessibility. Day 10.
let recipeNameLabel = UILabel()
let attributedLabel = NSAttributedString(
string: "Paella",
attributes: [.accessibilitySpeechIPANotation: "pɑːˈeɪə"]
)
recipeNameLabel.accessibilityAttributedLabel = attributedLabel