Skip to content

Instantly share code, notes, and snippets.

View keehun's full-sized avatar

Keehun keehun

View GitHub Profile
@keehun
keehun / dynamictype3-snippet2.swift
Last active February 15, 2019 17:42
Practical Dynamic Type: Attributed Strings, Snippet 2
// ...
label.adjustsFontForContentSizeCategory = true
NotificationCenter.default.addObserver(self,
selector: #selector (uiContentSizeCategoryChanged),
name: .FontMetricsContentSizeCategoryDidChange,
object: nil)
// ...
// ...
@keehun
keehun / dynamictype3-snippet1.swift
Last active February 15, 2019 17:48
Practical Dynamic Type, Part 3: Attributed Strings
import Foundation
import UIKit
// MARK: - AttributedStringView
class AttributedStringView: UIView {
/// `FontSetter` is a closure type used to recalculate the font size.
typealias FontSetter = () -> UIFont
/// A `UILabel` that will render the attributed string with Dynamic Type.
extension NSCharacterSet {
var characters: [String] {
/// An array to hold all the found characters
var characters: [String] = []
/// Iterate over the 17 Unicode planes (0..16)
for plane:UInt8 in 0..<17 {
/// Iterating over all potential code points of each plane could be expensive as
/// there can be as many as 2^16 code points per plane. Therefore, only search
let string4 = "᧐᪂᧐"
print("\"\(string4)\" only contains numbers: \(isValidNumeral(input4: string4))")
/// "᧐᪂᧐" only contains numbers: true
func isValidNumeral(input4: String) -> Bool {
let numbers = CharacterSet.decimalDigits
let rangeOfInvalidCharacters = input4.rangeOfCharacter(from: numbers.inverted)
return rangeOfInvalidCharacters == nil
}
let string1 = "qwerty12345"
let string2 = "qwerty"
let string3 = "12345"
let numbers: NSCharacterSet = NSCharacterSet.decimalDigits as NSCharacterSet
let character: UTF32Char = UTF32Char(48) /// U+0030 is "0" (48 Decimal = 0x30 Hex)
numbers.longCharacterIsMember(character) /// returns `true`
print(CharacterSet.decimalDigits)
/// <CFCharacterSet Predefined DecimalDigit Set>
let stringToCheck: String = "42"
let numbers: CharacterSet = CharacterSet.decimalDigits
let stringIsANumber: Bool = stringToCheck.rangeOfCharacter(from: numbers.inverted) == nil
/// returns: `true`
@keehun
keehun / decimalDigits.txt
Last active December 5, 2018 22:52
All Characters in CharacterSet.decimalDigits
0
1
2
3
4
5
6
7
8
9
@keehun
keehun / TestHal9000.swift
Created August 9, 2018 03:35
Practical Dynamic Type, Part 2
import XCTest
@testable import PracticalDynamicTypePart2
class PracticalDynamicTypePart2Tests: XCTestCase {
var subject: Hal9000!
override func setUp() {
super.setUp()
subject = Hal9000()