21 post tagged

iOS

Later Ctrl + ↑

NSString+Ordinal

I put up this little category on GitHub. Feel free to use it — if you find any use for it. Below is a copy of the README file.

NSString+Ordinal

NSString+Ordinal is an NSString category for spelling numbers as words. Specifically, it’s intended for Russian ordinal numbers, for which, as far as I know, there is no direct iOS support. The category is tailored to work with numbers 0–200 in Russian and provides sensible defaults for any other number and locale.

What it does is it spells 1, 2, 3 as первый, второй, третий (first, second, third) in Russian and one, two, three in English, which is by design to be used in a phrase like «день первый» (the first day) in Russian and “day one” in English.

The category is a single method which accepts an integer:

+ (NSString *)ordinalRepresentationWithNumber:(NSInteger)ordinal;

This is probably reinventing the wheel, but I strongly believe in bootstrapped, self-written, simple solutions first. It works well for me because I know exactly how it works.

 No comments   2014   iOS

Non-breaking space in NSString

A recent project I worked on had a focus on typography, so kerning, line spacing and spacing were all important. One of the necessary steps to achieve this was to use non-breaking spaces in strings.

Turns out, it’s pretty easy, just use the Unicode representation \u00a0, which looks like this in Objective-C code:

@"This is a non-breaking\u00a0space"

Kerning, line spacing, hyphenation and other typographic features are also really interesting to implement, but they demand a longer post and illustrations, so I’ll write about them later.

 No comments   2014   iOS

Pattern with image: UIKit and Core Graphics

This is fun.

The other day I’ve been working on a project that uses controls with patterned textures. I’ve used the texture on one button, then another, and finally on a different control. When I started the app, for some reason the texture on the different control was backwards.

Have a look. The first button’s background is set as:

[view setBackgroundColor:[UIColor colorWithPatternImage:[UIImage imageNamed:@"some-image.png"]]];

And the second:

[view.layer setBackgroundColor:[UIColor colorWithPatternImage:[UIImage imageNamed:@"some-image.png"]].CGColor];

And here’s how both of these actually look like. The UIKit one represents the “correct” orientation of the background texture.

Notice the difference: in the latter case I’m using the view’s layer to set the background color, and I’m using a CGColor reference for that — where CG stands for Core Graphics. UIKit, of course, uses a coordinate system where (0,0) is at the top left. Not so for Core Graphics:

Some iOS technologies define default coordinate systems whose origin point and orientation differ from those used by UIKit. For example, Core Graphics and OpenGL ES use a coordinate system whose origin lies in the lower-left corner of the view or window and whose y-axis points upward relative to the screen.

If we look at things this way, then the texture in the latter case is positioned exactly right, if we mentally rotate it 90 degrees counter-clockwise.

There’s a function to flip the coordinate system used by Core Graphics for use within UIKit, but it’s usually applied only to the current context, which is not always convenient, as in this case. So bear this in mind every time you deal with Core Graphics within UIKit.

 No comments   2014   iOS
Earlier Ctrl + ↓