Skip to content

Instantly share code, notes, and snippets.

View krzyzanowskim's full-sized avatar

Marcin Krzyzanowski krzyzanowskim

View GitHub Profile
VERSION := 0.1.0
# Repository
SRC_DIR := src
PARSER_REPO_URL ?= $(shell git -C $(SRC_DIR) remote get-url origin)
$(eval PARSER_NAME=$(shell basename $(PARSER_REPO_URL) | cut -d '-' -f3 | sed 's#.git##' ))
UPPER_PARSER_NAME := $(shell echo $(PARSER_NAME) | tr a-z A-Z)
@krzyzanowskim
krzyzanowskim / NSTextLocation+Equatable.swift
Created January 30, 2022 20:55
NSTextLocation missing Equatable helper
extension NSTextLocation {
static func == (lhs: Self, rhs: Self) -> Bool {
lhs.compare(rhs) == .orderedSame
}
static func != (lhs: Self, rhs: Self) -> Bool {
lhs.compare(rhs) != .orderedSame
}
@krzyzanowskim
krzyzanowskim / README.md
Last active January 23, 2022 20:16
FB9856587

Given a NSTextContentStorage with a single character \n (newline), a layout manager results in 2 line fragments.

Two things to discuss here:

  1. The range of a second TextLineFragment is {1, 0}, that is 0 length range, yet the textLineFragment.attributedString.string value is "\n"
  2. Why two lines at all? I expected a single line. this seems to be a case only if \n is at the end of the storage string. The string "\n\n\n" results in 3 fragments, and 4 lines.
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 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 [email protected]
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)