reports icon indicating copy to clipboard operation
reports copied to clipboard

FB12276827: Proposal: `.highLegibility()` font modifier

Open samhenrigold opened this issue 1 year ago • 0 comments

  • Date: 2023-06-08
  • Resolution: Open
  • Area: Developer Technologies & SDKs
  • OS: All
  • Type: Suggestion
  • Keywords: SwiftUI, Font Modifier

Description

The San Francisco font has a alternate stylistic set that renders characters with more distinct letterforms — zeros are slashed, the uppercase i has the little bits that stick out at the top and bottom, and 4's are opened at the top. This is particularly useful for combatting homoglyph attacks.

In Swift, you have to use kStylisticAltSixOnSelector which doesn't seem "shelf-stable". A font modifier to enable those high-legibility styles for a block of text would be fantastic.

Files

image

fontWithHighLegibility.swift

import SwiftUI
import UIKit

extension UIFont {
//    https://raw.githubusercontent.com/anonymouscamera/anonymous-camera/main/anonymous-camera/Helpers/Helpers.swift
    func fontWithFeature(key: Int, value:Int) -> UIFont {
        let originalDesc = self.fontDescriptor
        let features:[UIFontDescriptor.AttributeName: Any] = [
            UIFontDescriptor.AttributeName.featureSettings : [
                [
                    UIFontDescriptor.FeatureKey.featureIdentifier: key,
                    UIFontDescriptor.FeatureKey.typeIdentifier: value
                ]
            ]
        ]
        let newDesc = originalDesc.addingAttributes(features)
        return UIFont(descriptor: newDesc, size: 0.0)
    }
    
    func fontWithHighLegibility() -> UIFont {
        return self.fontWithFeature(key: kStylisticAlternativesType, value: kStylisticAltSixOnSelector)
    }
}

struct ContentView: View {
    
    let sampleText = "LOS ANGELES IS HOME to one of the best-organized and most politically sophisticated labor movements in the nation, and the ordinance, calling for a wage floor of $9.39 per hour with health insurance or $10.64 without it, had easily passed the city council the month before."
    
    var body: some View {
        
        List {
            Section("Default") {
                Text(sampleText)
                    .font(.title2)
            }
            Section("Font With High Legibility") {
                Text(sampleText)
                    .font(Font(
                        UIFont.preferredFont(forTextStyle: .title2)
                            .fontWithHighLegibility()
                    ))
            }
        }
    }
    
}

samhenrigold avatar Mar 27 '24 13:03 samhenrigold