Store UIImage in CoreData Without Writing Any Code

There is an easy but little known way to store many kinds of UIKit objects in CoreData without writing any code. It works for UIImage, UIColor, UIBezierPath, MKPlaceMark, NSDate, and any other class that conforms to the NSCoding protocol.

What you need to do is set the attribute type to Transformable, and the the transformable name to NSUnarchiveFromDataTransformerName.

That’s it!

Now can set UIImage objects directly into your NSManagedObject objects:

person.thumbnailImage = [UIImage imageNamed:@"defaultPortrait"];
anImageView.image = person.thumbnailImage;
Posted in Uncategorized | Tagged , , | Leave a comment

Programmatically Set Insertion Point in a UITextField

The UX guy said that the email field needs to have a default domain already filled, so that most users will only have to type their username:

@gmail.com

Of course, the insertion point should be at the beginning of the UITextField, otherwise the poor users will have to tap and hold until they get it just to right spot.

Sadly, UITextField does not allow setting the position of the insertion point.
Happily, UITextView does.

So here is what I did:
I put a one-line UITextView instead of the UITextField. When the view loads, or when the UITextView becomes first responder, I simply put the insertion point in the beginning:

oneLineTextView.selectedRange = NSMakeRange(0,0);

To make sure the text is only one line, I captured the event of typing “\n” by implementing this UITextViewDelegate method:

- (BOOL)textView:(UITextView *)textView
        shouldChangeTextInRange:(NSRange)range
        replacementText:(NSString *)text
{
    if ([text hasPrefix:@"\n"]) {
        // leave field and do something with its value
        return NO;
    }
    return YES;
}

Voila!

Posted in Uncategorized | Tagged , | Leave a comment