Skip to main content

Tech

Frame debugging on a device

2 min read

There's this handy feature, Debug -> View Debugging -> Show View Frames, which, if turned on, draws borders around views. The sad part is that it only works on the simulator. But we can (somewhat) easily simulate its behavior.

First, we create a helper method that does the coloring:

func activateFramesDebug() {
  guard debugFrames else { return }
 
  layer.borderWidth = 1
  layer.borderColor = UIColor(
    hue: CGFloat(arc4random_uniform(100_000)) / 100_000,
    saturation: CGFloat(arc4random_uniform(100_000)) / 100_000,
    brightness: 0.5 + CGFloat(arc4random_uniform(50)) / 100,
    alpha: 1.0).CGColor
}
Continue reading →

Improving git log

2 min read

We can already use --graph and --decorate to get a pretty, colored commit tree, but we can create a function for less typing, more flexibility and more info:

git_branch() {
  git branch 2> /dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/\1/'
}
Continue reading →

Working easier with tags

2 min read

One usual approach is to create an enum, so your tags are more expressive by having a name:

enum ViewTag: Int {
  case none
  case titleLabel
  case loginButton
}

Then tagging and retrieving views by tag will be safer, and easier to remember:

Continue reading →

Creating a theme helper

2 min read

The standard approach for this would be something like this:

struct Theme {
  enum Color {
    case title
    case subtitle
    // [...]
    var color: UIColor {
      switch self {
      case .title: return UIColor.red
      case .subtitle: return UIColor.blue
      ...
      }
    }
  }
  
  enum Font {
  }
}

And using it throughout the app would look like this:

Continue reading →

LightPaper

45 sec read

I don't know why, but I've always seemed to be looking for the "perfect" Markdown editor - although I used a lot of really great ones, none really clicked with me.

Come in, LightPaper. Easy file access via the sidebar, multi-tabs, script assets, Jekyll Rendering, Math Rendering, MultiMarkdown or GitHub Flavored Markdown, a preference tab where you write CSS for extra customization on top of the usual CSS-based themes, and much more.

Continue reading →

PlugRocket

2 min read

It all started with this post by Joe for updating a plug-in from the terminal, which lead to this script for automating the process of updating all plug-ins.

Even though I could have created an Alfred workflow to easily run the script with a single command, I always wanted to start working on Mac apps, so this was a good opportunity to get my feet wet.

Continue reading →

Manipulating files outside of sandbox

2 min read

First, we need to ask for the user's permission to access the folder, but for that, we need an NSSavePanel:

let op = NSSavePanel()
op.message = "Descriptive message here"
op.canCreateDirectories    = false
op.canChooseDirectories    = false
op.showsHiddenFiles        = false
op.prompt                  = "Allow"
op.title                   = "Allow access"
op.isExtensionHidden         = true
op.directoryURL            = URL(string: "/path/to/folder")
 
// Depending on your purpose, you might need these to true
op.allowsMultipleSelection = false
op.canChooseFiles          = false
Continue reading →

Running a script with NSTask and NSPipe

2 min read

Say you have a Mac app and you want to run a script, either to perform some action, or to return something, here's one way to do it.

First, we create an NSTask, set the launch path of the handler, in our case ruby (we will use the default one, to be sure it exists), and the required parameters, if required:

let task = Process()
task.launchPath = "/usr/bin/ruby"
task.arguments = [
  Bundle.main.path(forResource: myScript, ofType: "rb")!,
  parameter1
]

This would be the same as running in the terminal:

ruby /path/to/myScript parameter1

All good, but what if the script returns something and we want to use that? NSPipe to the rescue:

Continue reading →

Different fonts for the same label

2 min read

Been slacking lately, but I hope I can make it up with this one. Let's say you need to display a price, and the currency, but the currency code should have a different font. You could have two labels, but that just complicates code and brings unwanted overhead, so let's use the same label.

This part will be common to all examples:

Continue reading →