Skip to content

Instantly share code, notes, and snippets.

@keighl
keighl / NSString+Extras.m
Created December 19, 2012 13:37
Secure API Request From iOS to Rails
#import <Foundation/Foundation.h>
#import <CommonCrypto/CommonDigest.h>
#import <CommonCrypto/CommonHMAC.h>
@interface NSString (Extras)
+ (NSString *)authSignatureWithToken:(NSString *)token;
@end
@implementation NSString (Extras)
@keighl
keighl / foooont.sh
Last active December 9, 2021 14:00
Rename a font to have the same Family/Fullname/Postscript name. This prevents IE from choking in @font-face
# FontForge command
fontforge -script 2ttf.pe ProprietaryFontCondensedBold.otf
# 2ttf.pe
# FontForge Script
# http://fontforge.org/scripting.html
#
# * Opens the font file
# * Sets the family/fullname/postscript name be the same as the filename (without ext)
@IBAction func fontFaceChanged(sender: NSPopUpButton) {
let newIDX = sender.indexOfSelectedItem
if let currentFont = textView?.typingAttributes[NSFontAttributeName] as? NSFont {
let newFaceName = fontFaceNames[newIDX]
if let newFont = NSFontManager.sharedFontManager().convertFont(currentFont, toFace: newFaceName) {
applyNewAttributes([NSFontAttributeName: newFont])
}
}
}
@keighl
keighl / application.rb
Created December 19, 2012 16:42
Faster Rails asset precompilation via Capistrano .. just do it locally!
# Speed things up by not loading Rails env
config.assets.initialize_on_precompile = false
@keighl
keighl / CBGameController.m
Last active September 1, 2018 17:24
SDWebImage in a UITableViewCell - cancel any download operation before reusing the cell. Otherwise, you might see previous images loading in before the correct one is finished downloading.
- (UITableViewCell *)tableView:(UITableView *)tableView
cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *identifier = @"MoveViewCell";
CBMoveView *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
if (!cell)
cell = [[CBMoveView alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
cell.move = (CBMove *)[self.moves objectAtIndex:indexPath.row];
if var font = textView?.typingAttributes[NSFontAttributeName] as? NSFont {
font = NSFontManager.sharedFontManager().convertFont(font, toHaveTrait: .BoldFontMask)
// or ...
font = NSFontManager.sharedFontManager().convertFont(font, toNotHaveTrait: .BoldFontMask)
applyNewAttributes([NSFontAttributeName: font])
}
dynamic let fontFamilyNames = NSFontManager.sharedFontManager().availableFontFamilies
fontFamilyName!.bind("content", toObject: self, withKeyPath: "fontFamilyNames", options: nil)
@IBAction func fontFamilyChanged(sender: NSPopUpButton) {
let newIDX = sender.indexOfSelectedItem
if var font = textView?.typingAttributes[NSFontAttributeName] as? NSFont {
font = NSFontManager.sharedFontManager().convertFont(font, toFamily: fontFamilyNames[newIDX])
applyNewAttributes([NSFontAttributeName: font])
}
func textViewDidChangeTypingAttributes(notification: NSNotification) {
if let font = textView?.typingAttributes[NSFontAttributeName] as? NSFont {
if let faces = NSFontManager.sharedFontManager().availableMembersOfFontFamily(font.familyName) {
// faces is an array of arrays like this:
// [[Helvetica, Regular, 5, 0], [Helvetica-Light, Light, 3, 0], [Helvetica-Oblique, Oblique, 5, 1], [Helvetica-LightOblique, Light Oblique, 3, 1], [Helvetica-Bold, Bold, 9, 2], [Helvetica-BoldOblique, Bold Oblique, 9, 3]]
fontFacesLabels = faces.map({face in (face[1] as! String)})
}
}
}
class Document: NSDocument {
@IBAction func fontSizeChanged(sender: NSControl) {
var newSize = CGFloat(sender.floatValue)
if var font = textView?.typingAttributes[NSFontAttributeName] as? NSFont {
font = NSFontManager.sharedFontManager().convertFont(font, toSize: newSize)
applyNewAttributes([NSFontAttributeName: font])
}
}
func applyNewAttributes(attributes: [String: AnyObject]) {
class Document: NSDocument {
@IBOutlet var fontSizeField: NSTextField?
dynamic var currentFontSize: CGFloat = 0
override func windowControllerDidLoadNib(aController: NSWindowController) {
super.windowControllerDidLoadNib(aController)
fontSizeField.bind("value",
toObject: self,
withKeyPath: "currentFontSize",