Skip to content

Instantly share code, notes, and snippets.

View krzyzanowskim's full-sized avatar

Marcin Krzyzanowski krzyzanowskim

View GitHub Profile
@krzyzanowskim
krzyzanowskim / Image+customSymbol.swift
Created December 23, 2021 12:08
Image system and custom SFSymbol initializer
import SwiftUI
extension Image {
init(symbol name: String, accessibilityDescription: String? = nil) {
if let nsImage = NSImage(systemSymbolName: name, accessibilityDescription: accessibilityDescription) {
self.init(nsImage: nsImage)
} else if let nsImage = NSImage(named: name) {
nsImage.accessibilityDescription = accessibilityDescription
self.init(nsImage: nsImage)
@krzyzanowskim
krzyzanowskim / FB9762603.swift
Last active December 5, 2021 10:53
FB9762603: TextField with NumberFormatter() is broken on macOS 12 - no values
/*
16 November 2021
SwiftUI.TextField used with NumberFormatter() as a formatter does not set/update value on macOS 12 (Monterey). This is regression as it works ok on macOS 11 (BigSur)
Please find attached Playground code and code screenshot with marked bug
Related FB9180913. Reported June 15.
*/
class TextFormatter: Formatter {
override func string(for obj: Any?) -> String? {
obj as? String
}
override func getObjectValue(_ obj: AutoreleasingUnsafeMutablePointer<AnyObject?>?, for string: String, errorDescription error: AutoreleasingUnsafeMutablePointer<NSString?>?) -> Bool {
obj?.pointee = string as AnyObject
return true
}
}
@krzyzanowskim
krzyzanowskim / largeNumbers.swift
Created October 21, 2021 10:39
TextKit large numbers
// This file defines two numbers to use to mean "really big".
// The first is called LargeNumberForText, and it was not arbitrarily chosen.
// The actual value was chosen to be around the largest floating point value possible that can preserve at least pixel precision.
// Because of the nature of floating point numbers, the bigger they get, the less precise they get.
// It is not wise to use bigger dimensions for text system objects because, even if you ever fill all that space,
// by the time you get to the far reaches, the letters won't have the precision necessary to look and act correctly.
// This limitation of floating point coordinates goes all the way down into postscript, and holds for any type postscript graphics.
// Because the Cocoa text system respects this limit in various ways, a second constant, NotQuiteAsLargeNumberForText,
// is used for the field-like text views created by the FieldAspect class. This is simply half of LargeNumberForText;
// at sizes as large as LargeNumberForText, the text s
import Foundation
let formatter = PersonNameComponentsFormatter()
formatter.style = .long
do {
let components = formatter.personNameComponents(from: "Marcin Krzyżanowski")!
print(components.givenName!) // Marcin
print(components.familyName!) // Krzyżanowski
}
@krzyzanowskim
krzyzanowskim / StringGetSizeThatFits.swift
Last active November 12, 2023 14:51
Calculate frame of String, that fits given width
// Excerpt from https://github.com/krzyzanowskim/CoreTextWorkshop
// Licence BSD-2 clause
// Marcin Krzyzanowski marcin@krzyzanowskim.com
func getSizeThatFits(_ attributedString: NSAttributedString, maxWidth: CGFloat) -> CGSize {
let framesetter = CTFramesetterCreateWithAttributedString(attributedString)
let rectPath = CGRect(origin: .zero, size: CGSize(width: maxWidth, height: 50000))
let ctFrame = CTFramesetterCreateFrame(framesetter, CFRange(), CGPath(rect: rectPath, transform: nil), nil)
@krzyzanowskim
krzyzanowskim / newline.swift
Last active July 10, 2021 20:51
In Swift \r\n is a single Character, just like \n
let str = "dupa\r\nblada"
for i in str.indices {
switch str[i] {
case "\n":
print("\\n found") // \n not found
case "\r":
print("\\r found") // \r not found
case "\r\n":
print("\\r\\n found") // found
default:
@krzyzanowskim
krzyzanowskim / TrieAutocomplete.swift
Last active June 29, 2021 17:39
Trie autocomplete
import Foundation
/*
D
O
O
G R
S
@krzyzanowskim
krzyzanowskim / BOM.swift
Created May 22, 2021 18:29
one bom to rule them all
let bom: String = "\u{feff}" // The character \uFEFF is the BOM character for all UTFs (8, 16 LE and 16 BE)
let str = "abc😂"
str.count // 4
str.utf16.count // 5
str.utf8.count // 7
import Foundation
(str as NSString).length // 5
(str as NSString).lengthOfBytes(using: String.Encoding.utf8.rawValue) // 7