How to create a colored 1×1 UIImage on the iPhone dynamically?

You can use CGContextSetFillColorWithColor and CGContextFillRect for this: Swift extension UIImage { class func image(with color: UIColor) -> UIImage { let rect = CGRectMake(0.0, 0.0, 1.0, 1.0) UIGraphicsBeginImageContext(rect.size) let context = UIGraphicsGetCurrentContext() CGContextSetFillColorWithColor(context, color.CGColor) CGContextFillRect(context, rect) let image = UIGraphicsGetImageFromCurrentImageContext() UIGraphicsEndImageContext() return image } } Swift3 extension UIImage { class func image(with color: UIColor) -> … Read more

How to convert ASCII character to CGKeyCode?

This is what I ended up using. Much cleaner. #include <CoreFoundation/CoreFoundation.h> #include <Carbon/Carbon.h> /* For kVK_ constants, and TIS functions. */ /* Returns string representation of key, if it is printable. * Ownership follows the Create Rule; that is, it is the caller’s * responsibility to release the returned object. */ CFStringRef createStringForKey(CGKeyCode keyCode) { … Read more

Where can I find a list of Mac virtual key codes?

Below is a list of the common key codes for quick reference, taken from Events.h. If you need to use these keycodes in an application, you should include the Carbon framework: Objective-C: #include <Carbon/Carbon.h> Swift: import Carbon.HIToolbox You can then use the kVK_ANSI_A constants directly. WARNING The key constants reference physical keys on the keyboard. … Read more

iPhone smooth sketch drawing algorithm

CGPoint midPoint(CGPoint p1, CGPoint p2) { return CGPointMake((p1.x + p2.x) * 0.5, (p1.y + p2.y) * 0.5); } -(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { UITouch *touch = [touches anyObject]; previousPoint1 = [touch previousLocationInView:self]; previousPoint2 = [touch previousLocationInView:self]; currentPoint = [touch locationInView:self]; } -(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event { UITouch *touch = [touches anyObject]; previousPoint2 = previousPoint1; previousPoint1 … Read more

Get PDF hyperlinks on iOS with Quartz

heres the basic idea to get to the annots CGPDFDictionary for each page atleast. after that you should be able to figure it out with help from the PDF spec from Adobe. 1.) get the CGPDFDocumentRef. 2.) get each page. 3.) on each page, use CGPDFDictionaryGetArray(pageDictionary, “Annots”, &outputArray) where pageDictionary is the CGPDFDictionary representing the … Read more

NSAttributedString background color and rounded corners

I managed to achieve the above effect, so thought I’d post an answer for the same. If anyone has any suggestions about making this more effective, please feel free to contribute. I’ll be sure to mark your answer as the correct one. 🙂 For doing this, you’ll need to add a “custom attribute” to NSAttributedString. … Read more