Skip to content

Instantly share code, notes, and snippets.

Avatar

Marcin Krzyzanowski krzyzanowskim

View GitHub Profile
View SwiftUITextFieldEditingCase.swift
import SwiftUI
import PlaygroundSupport
struct TestView: View {
@State var text: String = ""
@State var dummy: String = "dummy"
@State var showTextField1: Bool = true
@State var showTextField2: Bool = true
var body: some View {
@krzyzanowskim
krzyzanowskim / Image+customSymbol.swift
Created Dec 23, 2021
Image system and custom SFSymbol initializer
View Image+customSymbol.swift
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 Dec 5, 2021
FB9762603: TextField with NumberFormatter() is broken on macOS 12 - no values
View FB9762603.swift
/*
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.
*/
View TextFormatter.swift
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
}
}
View largeNumbers.swift
// 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
View PersonNameComponentsFormatter.swift
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 Dec 19, 2021
Calculate frame of String, that fits given width
View StringGetSizeThatFits.swift
// 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 Jul 10, 2021
In Swift \r\n is a single Character, just like \n
View newline.swift
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:
View TrieAutocomplete.swift
import Foundation
/*
D
O
O
G R
S
@krzyzanowskim
krzyzanowskim / BOM.swift
Created May 22, 2021
one bom to rule them all
View BOM.swift
let bom: String = "\u{feff}" // The character \uFEFF is the BOM character for all UTFs (8, 16 LE and 16 BE)