Using NSExpression to evaluate arithmetic

Today I found something cool in the form of NSExpression.

I wanted to be able to evaluate arithmetic represented as an NSString without having to write too much code. Using NSExpression it's a two step operation.

stackoverflow answer

NSExpression *expression = [NSExpression expressionWithFormat:@"9+4"];
// result is a NSNumber
id result = [expression expressionValueWithObject:nil context:nil]; 
Posted in iOS, programming | Tagged | Leave a comment

JLPeekModal

I wanted to have a go at creating a half modal slide transition. The intention for this is if you want the user to be able to see the contents of the previous view controller and a partial amount of the one you've just pushed on.

Check out JLPeekModal on Github.

There are a few options which I need to refactor to allow for more obvious customisation. I'm hoping the code is readable enough for that already :)

Posted in iOS, my life, programming | Comments closed

UIBarButton background image customisation

If you've changed the tint colour of your UINavigationBar or you just want to get away from Apple's default UIBarButtonItem styles then UIAppearance can help you out again.

Please note that this is for iOS 6.0+. In iOS 5.0 you can only customise the Back button without having to create a subclass

UIImage* plainBtn = [[UIImage imageNamed:@"btn"] resizableImageWithCapInsets:UIEdgeInsetsMake(12, 12, 12, 12)];
UIImage* plainBtnHighlight = [[UIImage imageNamed:@"btn-high"] resizableImageWithCapInsets:UIEdgeInsetsMake(12, 12, 12, 12)];

UIImage* doneBtn = [[UIImage imageNamed:@"btn-done"] resizableImageWithCapInsets:UIEdgeInsetsMake(12, 12, 12, 12)];
UIImage* doneBtnHighlight = [[UIImage imageNamed:@"btn-done-high"] resizableImageWithCapInsets:UIEdgeInsetsMake(12, 12, 12, 12)];

// Set the background images for all plain style buttons
[[UIBarButtonItem appearance] setBackgroundImage:plainBtn forState:UIControlStateNormal style:UIBarButtonItemStylePlain barMetrics:UIBarMetricsDefault];
[[UIBarButtonItem appearance] setBackgroundImage:plainBtnHighlight forState:UIControlStateHighlighted style:UIBarButtonItemStylePlain barMetrics:UIBarMetricsDefault];

// Set the background images for all of our done style buttons
[[UIBarButtonItem appearance] setBackgroundImage:doneBtn forState:UIControlStateNormal style:UIBarButtonItemStyleDone barMetrics:UIBarMetricsDefault];
[[UIBarButtonItem appearance] setBackgroundImage:doneBtnHighlight forState:UIControlStateHighlighted style:UIBarButtonItemStyleDone barMetrics:UIBarMetricsDefault];

Documentation for this method is here: UIBarButtonItem - setBackgroundImage:forState:style:barMetrics:

Something to watch out for if you're doing custom text attributes for your UIBarButtonItem's. E.g.

[[UIBarButtonItem appearance] setTitleTextAttributes:attrsNormal forState:UIControlStateNormal];
[[UIBarButtonItem appearance] setTitleTextAttributes:attrsHighlighted forState:UIControlStateHighlighted];

This goes for all of your UIBarButtonItem's in your app. What this means is if you had a white plainBtn image and a very dark doneBtn image you may have to watch out what UITextAttributeTextColor you set. E.g. if you set a black colour then it might not be seen on the Done buttons.

One way I found to get around this is making your root view a UINavigationController and setting itself as a UINavigationControllerDelegate.

You can then implement the delegate method navigationController:willShowViewController:animated::

#pragma mark - UINavigationControllerDelegate
- (void)navigationController:(UINavigationController *)navigationController willShowViewController:(UIViewController *)viewController animated:(BOOL)animated
{
    NSDictionary* attrs = @{UITextAttributeTextColor: [UIColor whiteColor]};
    for (UIBarButtonItem* rightBarButton in viewController.navigationItem.rightBarButtonItems)
    {
        if (rightBarButton.style == UIBarButtonItemStylePlain || rightBarButton.style == UIBarButtonItemStyleDone)
        {
            [rightBarButton setTitleTextAttributes:attrs forState:UIControlStateNormal];
            [rightBarButton setTitleTextAttributes:attrs forState:UIControlStateHighlighted];
        }
    }
}

It's kind of a cheat; but I haven't found a better solution to making sure your Plain and Done buttons have different title text attributes.

Posted in iOS, programming | Tagged , | Comments closed

iOS development – UINavigationBar changing the text title attributes

For a recent project I wanted to change all titles in my UINavigationController stack to a custom font.

With the UIAppearance protocol this is really quite simple:

[[UINavigationBar appearance] setTitleTextAttributes:attrs];

attrs is an NSDictionary that is made up of Keys For Text Attribute Dictionaries

NSString *const UITextAttributeFont;
NSString *const UITextAttributeTextColor;
NSString *const UITextAttributeTextShadowColor;
NSString *const UITextAttributeTextShadowOffset;

E.g. To change all the titles to blue.

NSDictionary* attrs = @{ UITextAttributeTextColor: [UIColor blueColor] };
[[UINavigationBar appearance] setTitleTextAttributes:attrs];

Something to note when setting a custom font; the base line may be higher than a system font. I think this partly due to how the font is created but don't hold me to that one. In order to adjust the base line height you just need to do this:

[[UINavigationBar appearance] setTitleVerticalPositionAdjustment:3.5f forBarMetrics:UIBarMetricsDefault];

Play around with the vertical adjustment value to get it looking correct again.

Posted in iOS, programming | Tagged , | Comments closed

iOS development – Saving UILabel as UIImage

Recently I needed a way to use a custom icon font (e.g. Font Awesome and save the results out as an image. It turns out this isn't too hard (with some help from Stackoverflow).

I've written a gist to save out a UILabel as a UIImage.

This category makes a few assumptions:

  • You're only creating an image for a single letter from an icon font
  • You want to save out the image to file and load from that

Once an image is created it will be loaded each and every time. If you're going to be actively changing the icon font I'd recommend commenting out the saving code. I'd also recommend putting in a job on app load to clear out the PNG files created every couple of weeks in case of updates.

The creation of a PNG file for loading later isn't really required as the creation of a UIImage is very fast.

Read More »

Posted in iOS, programming | Comments closed

JLIntervalTimer

I'm currently working on an Interval timer app. In order to make it easier on myself I wrote a timer that suited my needs.

I've taken out that timer and put it on Github.

Hopefully it may come across as useful to someone besides myself!

Basic usage:

# Implement the protocol
@interface YourClass <IntervalTimerDelegate>
@end

# Create a timer and start
self.timer = [[IntervalTimer alloc] initWithTimeInterval:[interval.time doubleValue] delegate:self];
[self.timer start];

# Use the delegate method
- (void)didPassSecond:(NSTimer*)theTimer
{
}
Posted in iOS, my life, programming | Tagged | Comments closed

iOS development – Create Categories for your NSManagedObjects

This is a simple one that I didn't even think about until I started my new job.

When using Core Data and generating your object models make sure to create a category for them. This allows you to create all your extra methods in a file that won't be replaced if you have to generate your object model again.

e.g. If I have a model called Workout

#import "Workout.h"

@interface Workout (Extra)
- (id)createWithDefaults;
@end 

I find it's a really nice pattern to use as I've found when I'm developing something new my data models tend to change quite frequently. This saves trying to copy and paste my changes over and over again.

Posted in iOS, programming | Comments closed