Skip to content

Instantly share code, notes, and snippets.

View krzyzanowskim's full-sized avatar

Marcin Krzyzanowski krzyzanowskim

View GitHub Profile
@krzyzanowskim
krzyzanowskim / FB15131180-extraline.md
Last active May 23, 2025 23:48
FB15131180 TextKit extra line frame is incorrect and does not respect layout fragment size (Regression)

Starting macOS 15/iOS 18 TextKit 2 extra line frame is more broken (height always been broken) than previously. The same code return different values when run on macOS 14/iOS 17 and macOS 15/iOS 18.

The problem is that extra line fragment (NSTextLineFragment) origin and size is incorrect. I sketch the problem with the code snippet below:

// Storage
let textContentManager = NSTextContentStorage()

// Layout
@krzyzanowskim
krzyzanowskim / .gitconfig
Last active February 4, 2025 21:45
commit-ai
[alias]
# need llm CLI: https://llm.datasette.io/en/stable/
# based on https://gist.github.com/karpathy/1dd0294ef9567971c1e4348a90d69285?permalink_comment_id=5167582#gistcomment-5167582
commit-ai = "!f() { if [ -n \"$(git diff --cached)\" ]; then git commit -m \"$(git diff --cached | llm -m '4o-mini' 'Below is a diff of all staged changes, coming from the command:\\n```\\ngit diff --cached\\n```\\nPlease generate a concise, two-sentence, maximum 100 characteres commit message for these changes. Do not mention project name.')\"; else echo 'No changes to commit'; fi }; f"
@krzyzanowskim
krzyzanowskim / FB14893227.md
Last active August 22, 2024 22:23
FB14893227 UITextView convenience initializer does not call designated initializer since iOS 16

https://mastodon.social/@krzyzanowskim/113007925115490633

UITextView convenience initializer does not call designated initializer since iOS 16. The UITextView.init(usingTextLayoutManager:) that is a "convenience" initializer for the UITextView, does not call UITextView.init(frame:textContainer:) that is a designated initializer. This behavior, when the class interface traslated to Swift interface, breaks the Swift rules of object initialization. I believe that is also incorrect in Objective-C, although the compiler doesn't check that.

code snippet to reproduce:

final class CustomTextView: UITextView {
@krzyzanowskim
krzyzanowskim / FB14700414.md
Created August 8, 2024 21:28
FB14700414: NSTextList doesn't work since macOS 14 (regression)
@krzyzanowskim
krzyzanowskim / FB14165227.md
Last active March 10, 2025 18:09
FB14165227: UlTextView erroneously overrides string attributes when applying spellchecker annotation attributes

UITextView erroneously overrides string attributes when applying spellchecker annotation attributes.

It doesn't need any particular setting. Default UITextView instance with attributed text

let textView = UITextView(usingTextLayoutManager: true)
textView.spellCheckingType = .yes

Once spellcheck attributes get applied, other attributes like foregroundColor gets applied to the mispelled words. This behavior happens only on Mac Catalyst, and started to appear on macOS 14 or newer.

extension StringProtocol {
/// str[NSRange(location:0, length: 9)]
subscript(_ range: NSRange) -> SubSequence {
guard let stringRange = Range<String.Index>(range, in: self) else {
fatalError("String index is out of range")
}
return self[stringRange]
}
@krzyzanowskim
krzyzanowskim / FB13789916.md
Created May 9, 2024 11:49
FB13789916: NSTextInputClient.setMarkedText provide bogus selection range for Chinese keyboard
  1. Open macOS TextEdit application
  2. enter "aaaaaaaaaaaaaaaaaaaa"
  3. move insertion point at location 0
  4. switch system keyboard to Pinyin - Simplified (Chinese)
  5. on the keyboard type letters "abcdefghij"
  6. 💥 observe bug: marked text is "abc " insertion point is at location 11

expected:

@krzyzanowskim
krzyzanowskim / TextKit2.swift
Last active June 9, 2025 00:35
Minimal setup of TextKit2 components to produce PDF document
import Cocoa
let loremIpsum = """
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Pellentesque erat risus, laoreet et eros id, lobortis elementum sem. Nullam nec magna massa. Donec gravida felis at odio tincidunt pretium et in orci. Curabitur suscipit porta purus non faucibus. Cras dapibus felis eu enim rutrum, vel aliquet lacus volutpat. Quisque fermentum vulputate dictum. Morbi mattis orci quis dui posuere, id vulputate turpis tempus. Sed vulputate augue at ullamcorper feugiat. Phasellus nec lacus eget sapien congue lacinia vel et nulla. Phasellus sollicitudin gravida dapibus. Donec molestie ullamcorper lacus eu rutrum. Praesent lacinia dignissim dui eu ultricies. Integer in lacus lobortis, porttitor sem eu, tincidunt dolor. Cras dignissim nisl in maximus ullamcorper.
Proin nec vulputate magna. In interdum leo sit amet arcu consequat facilisis. Fusce ut malesuada ante, nec malesuada nisl. Mauris porta velit quis tortor mollis, in suscipit augue efficitur. Praesent eleifend mollis neque, non sempe
extension StringProtocol {
subscript(_ offset: Int) -> String.Element {
if offset >= 0 {
self[index(startIndex, offsetBy: offset)]
} else {
self[index(endIndex, offsetBy: offset)]
}
}
@krzyzanowskim
krzyzanowskim / LineBreakPropose.swift
Last active October 28, 2023 22:45
"given this many pixels and a string with these attributes, tell me the optimal places to do a line break" https://iosdevelopers.slack.com/archives/C1GPPBMHC/p1697470218613869?thread_ts=1697467894.270959&cid=C1GPPBMHC
import Cocoa
/// "given this many pixels and a string with these attributes, tell me the optimal places to do a line break"
class LineBreakPropose: NSObject, NSTextLayoutManagerDelegate {
var lineBreaks: [NSTextLocation] = []
init(_ attributedString: NSAttributedString, in size: CGSize, by lineBreakMode: NSLineBreakMode = .byWordWrapping) {
super.init()