Skip to content

Instantly share code, notes, and snippets.

View krzyzanowskim's full-sized avatar

Marcin Krzyzanowski krzyzanowskim

View GitHub Profile
@krzyzanowskim
krzyzanowskim / TextKit2.swift
Last active April 7, 2024 13:10
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()
@krzyzanowskim
krzyzanowskim / FB13291926.md
Created October 20, 2023 21:00
FB13291926: NSTextLayoutManager.usageBoundsForTextContainer observer is never trigerred

I noticed that NSTextLayoutManager.usageBoundsForTextContainer is no longer KVO observable, despite being documented as KVO-compliant.. I believe it worked around macOS 13.

var usageBoundsForTextContainerObserver: NSKeyValueObservation?

usageBoundsForTextContainerObserver = textLayoutManager.observe(\.usageBoundsForTextContainer, options: [.new]) { textLayoutManager, change in
    print("never called")
}
@krzyzanowskim
krzyzanowskim / FB13290979.md
Last active October 21, 2023 16:57
NSTextContainer.lineFragmentPadding does not affect end of the fragment usageBoundsForTextContainer rectangle

I noticed that NSTextContainer.lineFragmentPadding value does not affect the NSTextLayoutManager.usageBoundsForTextContainer rectangle on both sizes.

Documentation of lineFragmentPadding says: "The padding appears at the beginning and end of the line fragment rectangles.", however the value of the usageBoundsForTextContainer does not resize appropriately.

Let's see how a different value of lineFragmentPadding affect the text container rectangle:

for lineFragmentPadding = 5 usageBoundsForTextContainer = (x: 5.0, y: 0.0, width: 66.73828125, height: 14.0)

for lineFragmentPadding = 0

// Documentation says:
//
// > Executes the specified block for each range of a particular attribute in the attributed string.
//
// documentation is not correct (or implementation, hard to tell). The block is executed at least once, not matter if particular attribute is or is not present in the attributed string.
import Foundation
import AppKit
/// Attributed string without any attribute, in particular no font attribute set
@krzyzanowskim
krzyzanowskim / reversible-formatstyle.swift
Last active July 21, 2023 14:21
ReversibleFormatStyle - like FormatStyle but reversible, more like Formatter
import Foundation
// like FormatStyle but reversible, more like Formatter
protocol ReversibleFormatStyle<FormatOutput>: FormatStyle {
func format(_ value: FormatOutput) -> FormatInput
}
// Implementation
struct ReversibleNumFormatStyle: ReversibleFormatStyle {
@krzyzanowskim
krzyzanowskim / box.swift
Created June 3, 2023 10:36
A SwiftUI wrapper around NSBox
/// A SwiftUI wrapper around `NSBox`.
public struct Box<Content>: View where Content: View {
private let title: String?
private let content: () -> Content
public init(_ title: String? = nil, @ViewBuilder content: @escaping () -> Content) {
self.title = title
self.content = content
}
@krzyzanowskim
krzyzanowskim / blogpost.txt
Last active May 4, 2023 08:25
Designing a Stunning macOS Icon for Your Text Editor: A Step-by-Step Guide https://twitter.com/krzyzanowskim/status/1653742801014726658
Title: Designing a Stunning macOS Icon for Your Text Editor: A Step-by-Step Guide
Introduction:
In the world of macOS applications, an eye-catching icon plays a crucial role in attracting users and conveying the essence of your software. When it comes to a text editor, your macOS icon should reflect the simplicity, elegance, and functionality of your application. In this blog post, we will explore the art of designing a stunning macOS icon for your text editor, step by step. Let's dive in!
Step 1: Understand Your Brand and Target Audience:
Before starting the icon design process, it's important to have a clear understanding of your brand and target audience. Ask yourself questions like: What are the unique features of your text editor? What emotions or feelings do you want your icon to evoke? Understanding your brand and target audience will help you create an icon that resonates with your users.
Step 2: Research and Inspiration:
Take some time to research existing macOS icons, especially those in the text
@krzyzanowskim
krzyzanowskim / pitch.md
Last active March 17, 2023 18:13
Swift Studio Investment Pitch

Dear [Name] Investments,

I am excited to introduce Swift Studio, a cutting-edge platform for Swift development, specifically tailored to Swift on Server. As you know, Swift is quickly gaining popularity as a language for developing server-side applications, and Swift Studio is the perfect tool for developers looking to take advantage of this trend.

Swift Studio is a comprehensive development environment that streamlines the development process by providing an intuitive user interface and powerful features. With Swift Studio, developers can easily create, test, and deploy Swift applications for the server, all in one place.

One of the key features of Swift Studio is its real-time collaboration functionality, which allows multiple developers to work on the same project simultaneously. This feature enhances productivity and promotes teamwork, making Swift Studio an ideal solution for larger development teams.

Additionally, Swift Studio integrates seamlessly with other popular development tools and technologi