Skip to content

Instantly share code, notes, and snippets.

@alexpersian
alexpersian / BalancedParanthesesGenerator.swift
Last active January 11, 2020 21:31
Generate balanced parentheses using Heap's Algorithm
import Foundation
final class Solution {
// Using a Set for result storage prevents unintended duplicates from being added.
private var result: Set<[Character]> = Set()
func generateParans(_ num: Int) {
// Build the seed array for sending into Heap's Algorithm gen function.
// We create an array of `num` opening parans and combine it with an
// array of `num` closing parans.
// It's possible that the text is truncated not because of the line break mode,
// but because the text is outside the drawable bounds
if isTruncating == false {
// This gives us the range of all the characters that have been already laid out
// into the text container by the layout manager.
let glyphRange = layoutManager.glyphRange(for: textContainer)
// This provides the range of glyphs (not characters) that have been generated for the text container.
// Since a glyph is a graphical representation of a character, and more than one glyph can be used
// to represent a single character, this should always be equal or greater than the number of
var isTruncating = false
// Make a range that encompases the entire text length.
let maxRange = NSRange(location: 0, length: Int.max)
// Use the layout manager to go through all of its line fragments within the range (entire text because of max range).
// More in-depth information is available within the NSLayoutManager docs.
layoutManager.enumerateLineFragments(forGlyphRange: maxRange) { _, _, _, glyphRange, stop in
// Ask the layout manager to find the range of the truncation glyph (...) within this line fragment
let truncatedRange = self.layoutManager.truncatedGlyphRange(inLineFragmentForGlyphAt: glyphRange.lowerBound)
// If the truncatedRange has a valid location that means the layout manager has detected the truncation glyph.
if truncatedRange.location != NSNotFound {
@alexpersian
alexpersian / UITextView+Truncated.swift
Last active August 21, 2023 03:22
Extension on UITextView that allows for the detection of truncation.
extension UITextView {
// Massive credit to Dave Delong for his extensive help with this solution.
/// Returns whether or not the `UITextView` is displaying truncated text. This includes text
/// that is visually truncated with an ellipsis (...), and text that is simply cut off through
/// word wrapping.
///
/// - Important:
/// This only works properly when the `NSLineBreakMode` is set to `.byTruncatingTail` or `.byWordWrapping`.
///
/// - Remark:
@alexpersian
alexpersian / DepthFirstSearch.swift
Created December 9, 2019 16:45
Graph + DepthFirstSearch
import Foundation
final class DepthFirstSearch<T: Hashable> {
private let graph: Graph<T>
private var searchStack: [T] = []
private var visited: Set<T> = Set()
init(graph: Graph<T>) {
self.graph = graph
// Update is called once per frame
public override func update(_ currentTime: TimeInterval) {
// Collect a reference frame for the node's current position
let currentFrame = dvdNode.calculateAccumulatedFrame()
// Top bound
if currentFrame.maxY >= self.frame.maxY {
moveTransform.ty = -1.0
dvdNode.run(changeColor)
}
// ...
private var changeColor: SKAction {
return SKAction.colorize(with: UIColor.generateRandomColor(), colorBlendFactor: 1.0, duration: 0)
}
// ...
private func setup() {
addChild(dvdNode)
extension UIColor {
static func generateRandomColor() -> UIColor {
let hue = CGFloat(arc4random() % 256) / 256 // use 256 to get full range from 0.0 to 1.0
return UIColor(hue: hue, saturation: 1, brightness: 1, alpha: 1) // Max saturation, max brightness
}
}
import SpriteKit
public class DVDScene: SKScene {
private let dvdNode: SKNode
public init(node: SKNode, size: CGSize) {
self.dvdNode = node
super.init(size: size)
setup()
import SpriteKit
import PlaygroundSupport
let viewFrame = CGRect(x: 0, y: 0, width: 400, height: 400)
let view = SKView(frame: viewFrame)
let dvdNode = SKSpriteNode(imageNamed: "dvd-logo.png")
dvdNode.size = CGSize(width: 100, height: 47)
let mainScene = SKScene(size: view.frame.size)