Skip to content

Instantly share code, notes, and snippets.

View carson-katri's full-sized avatar

Carson Katri carson-katri

View GitHub Profile
extension String {
func foregroundColor(_ color: UIColor) -> NSAttributedString {
NSAttributedString(string: self, attributes: [.foregroundColor : color])
}
func background(_ color: UIColor) -> NSAttributedString {
NSAttributedString(string: self, attributes: [.backgroundColor: color])
}
func underline(_ color: UIColor, style: NSUnderlineStyle = .single) -> NSAttributedString {
NSAttributedString(string: self, attributes: [.underlineColor: color, .underlineStyle: style.rawValue])
}
NSAttributedString {
"Hello "
.foregroundColor(.red)
.font(UIFont.systemFont(ofSize: 10.0))
"World"
.foregroundColor(.green)
.underline(.orange, style: .thick)
}
HStack {
if greeting != nil {
Text("\(greeting!) ")
}
Text("World")
}
@_functionBuilder
struct AttributedStringBuilder {
...
static func buildIf(_ segment: NSAttributedString?) -> NSAttributedString {
segment ?? NSAttributedString()
}
}
@carson-katri
carson-katri / playgrounds.json
Last active October 5, 2019 20:15
My Playground Book Subscription
{
"title": "Carson Katri's Blog",
"subtitle": "Playground Books based on the articles on \"carsonkatri.com\"",
"publisherName": "Carson Katri",
"feedIdentifier": "com.carsonkatri.playgrounds",
"contactURL": "mailto:carson.katri@gmail.com",
"formatVersion": "1.0",
"documents": [
{
"title": "Make a Function Builder",
struct CardHeading: View {
@State private var touchZoom: Bool = false
var body: some View {
VStack(spacing: 0) {
Image("banner")
.resizable()
.aspectRatio(contentMode: ContentMode.fill)
.frame(minWidth: 0, maxWidth: .infinity, maxHeight: 400)
.clipped()

Copyright <YEAR> <COPYRIGHT HOLDER>

Permission is hereby granted, free of charge, to any person obtaining a copy of this bad software and associated poor documentation files (the "Bad Software"), to deal in the Bad Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Bad Software, and to permit persons to whom the Bad Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Bad Software.

THE BAD SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES, CODE QUALITY OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,

import SwiftUI
import Combine
import PlaygroundSupport
struct SpringSolver {
let ƛ: CGFloat
let w0: CGFloat
let wd: CGFloat
/// Initial velocity
let v0: CGFloat
@carson-katri
carson-katri / View+badge.swift
Created January 21, 2021 16:21
Adds an `overlay` to a `View` that aligns the middle of the overlay with the edge of the parent, like a badge.
extension View {
func badge<Badge: View>(_ badge: Badge, alignment: Alignment = .topTrailing) -> some View {
overlay(
badge
.alignmentGuide(.leading, computeValue: splitDimension(\.width))
.alignmentGuide(HorizontalAlignment.center, computeValue: splitDimension(\.width))
.alignmentGuide(.trailing, computeValue: splitDimension(\.width))
.alignmentGuide(.top, computeValue: splitDimension(\.height))
.alignmentGuide(VerticalAlignment.center, computeValue: splitDimension(\.height))
.alignmentGuide(.bottom, computeValue: splitDimension(\.height)),
@carson-katri
carson-katri / wrapper_types.ts
Last active August 16, 2021 20:09
Wrapper types experiment in Wren
import "mirror" for Mirror, ClassMirror, MethodMirror
import "meta" for Meta
class Wrapper {
static operators { ["-","!","~","*","/","+","..","...","<<",">>","&","^","|","<","<=",">",">=","is","==","!="] }
static generate(wrappedType, innerBody) {
var s = "class %(wrappedType)_ {\n"
s = s + " wrappedValue { _wrappedValue }\n"
s = s + " construct wrap(wrappedValue) {\n"
s = s + " _wrappedValue = wrappedValue\n"