Objective C: Extract portion of a string using NSRange

Currently working on an app that requires me to parse the contents of a web page, extract a string from it and make an array. The “make an array part” is simple because the string is separated by commas and NSString has a method for generating an array of items separated by a certain string – componentSeparatedByString:. The part with “getting the string I need” is a bit less straight-forward though. So let’s say that this is the HTML of the web page I have to parse:

1
<html><meta http-equiv="Pragma" content="no-cache"></head><body>1,1,5,100,1,64,foo</body></html>

What I actually need is the bit between the opening and closing <body> tags. I shall paste my solution here and will explain it step by step:

1
2
3
4
5
6
7
NSString *originalString = [[NSString alloc] initWithString:@"<html><meta http-equiv="Pragma" content="no-cache"></head><body>1,1,5,100,1,64,foo</body></html>"];
NSRange startingRange = [originalString rangeOfString:@"<body>" options:NSBackwardsSearch];
NSRange endingRange = [originalString rangeOfString:@"</body>" options:NSBackwardsSearch];
int  myRangeLenght = endingRange.location - startingRange.location;
NSRange myStringRange = NSMakeRange (startingRange.location, myRangeLength);

NSLog (@"My new string is %@", [originalString substringWithRange:myStringRange]);

In the first line I am creating a new string object of type NSString.

Next I am defining two NSRange data structures. NSRange is a Foundation data type reference used to determine ranges in strings or arrays. It has 2 fields: location and length. So in the second line I am basically asking the compiler to give me the range of <body> string located in my originalString. I am then asking the compiler to give me another range for the closing </body> tag. At this point I have 2 ranges that don’t really do much on their own. But having the starting and ending point of a segment I can figure out the length of the segment.

This is exactly what I am doing on the 4th line – substracting startingRange.location from endingRange.location gives me that exact length. And after I have the length I can very well make a new range that defines the portion of the string that I am interested in. Making a new range is easy and it’s done with NSMakeRange.

The NSLog will return <body>1,1,5,100,1,64,foo and this is perfect because I am only interested in the “foo” part of the entire string so I don’t care about the tangling <body> part of it. But it can ofc be refined to whatever fits your needs.

Photoshop: Creating realistic hair with Photoshop standard brushes

I am currently working on a new exciting project and it involves removing background from a lot of pics featuring animals. It’s more or less easy to cut an object with straight edges from a photo but it becomes a little bit tricky when hair is involved. This short tutorial is mostly adressed to people that have more than basic Photoshop knowledge. I won’t be going in too many details regarding use of tools. So here is the original photo of a cat:

First thing I did was to use my pen tool and remove the background. The result was as follows:

As you can probably notice it’s all good except the fact that the cat’s fur doesn’t look too natural. In order to make fur I didn’t use any complex hair brushes. I used a standard ( I guess it’s usually called “grass” brush ):

In the brush editor one can make all kinds of amazing effects from a boring round brush by affecting scatter ratio, size ratio etc. It is also possible to change the brush angle. This is the only “trick” I used for this effect. So on a layer behind the cat I painted on the edge. Brush size is important of course but it depends on the animal you’re trying to retouch. In my case I used a fairly small size. I also used a tablet for this as it makes things a lot easier.

The color of the brush is not really important. I set it to a color closer to the cat’s fur because I wanted to get a feel of the final comp. At this point I had the right fur but I didn’t have the right color. For the color I basically made a clipping mask from the fur layer and on the layer on top I used the clone stamp tool to copy fur features from the actual cat to my new layer:

In the end after a few more adjustments I had a cat with transparent background and natural looking fur. I also added a few adjustment layers because I didn’t like the original color.

Objective C: Navigating between views with dynamically generated buttons

It’s easy enough to navigate from a manually added button to a specific View in the MainStoryboard. But it’s a little less straightforward when such an action is required off dynamically generated controls. The past few days this is what I’ve been crunching on  and here is the solution I finally got to.

First off I created a button:

1
2
3
4
5
6
7
[self makeThumbnail:nil
atPosX:0
atPosY:0
withSizeW:200
withSizeH:50
withTag:0
inView:self];

Inside the method that generates the button I also have a line that adds a specific action to it.

1
[myButton addTarget:self action:@selector(pushToNextView:) forControlEvents:UIControlEventTouchUpInside];

And the called method:

1
2
3
-(void)pushToNextView:(UIButton*)sender {
[self performSegueWithIdentifier:@"loadNextViewSegue" sender:sender];
}

Now pay attention to the “loadNextViewSegue” part. Basically I found that 2 views inside the storyboard can be connected with a segue directly. Ctrl+click the first one and drag to the next one. These kinds of segues do require an identifier. The identifier can be easily set in the Attributes Inspector with the particular segue selected. I called mine loadNextViewSegue.

Going back to the previous segue isn’t as easy as I thought it would be. About this in one of the future posts.

Objective C: Instantiating an UIButton through a Class Method

It wasn’t too long after starting to work with Classes in Objective C that I started to wonder how could I dynamically instantiate an UIButton, UIView ( or any other UI element for that matter ) on the Main view. I am sure this is pretty trivial for an experienced Objective C developer but it’s not that easy to figure out as a n00b.

In first place if you ever read anything about Objective C you have probably noticed that some methods are declared with a – and others are declared with a +:

1
2
-(void) instanceMethod;
+(void) classMethod;

You’d call these in different ways:

1
2
3
4
MyClass *newInst = [[MyClass alloc] init];
[newInst instanceMethod];

[MyClass classMethod];

Let’s hop to our code. In this example I shall make a Class method that created a UIButton. The UIButton properties will be set through parameters.

CreateButton.h

1
2
3
4
5
6
7
8
+(void) makeButton: (UIButton *) myButton
Xcoord: (double) X
Ycoord: (double) Y
withWidth: (double) WIDTH
withHeight: (double) HEIGHT
withTag: (int) btTag
withTitle: (NSString *) btTitle
inView: (UIView *) theView;

CreateButton.m

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
+(void) makeButton: (UIButton *) myButton
Xcoord: (double) X
Ycoord: (double) Y
withWidth: (double) WIDTH
withHeight: (double) HEIGHT
withTag: (int) btTag
withTitle: (NSString *) btTitle
inView: (UIView *) theView {

myButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
myButton.frame = CGRectMake(X, Y, WIDTH, HEIGHT);
[myButton setTitle:btTitle forState:UIControlStateNormal];
[myButton setTag:btTag];
[myButton setBackgroundColor:btColor];
[theView addSubview:myButton];

}

At this point we have a method that creates a UIButton object and places it at specified coords on the stage on the specified view. We had to obviously include the CreateButton.h in our ViewController.h in order for this method to be visible.

ViewController.m

1
2
3
4
5
6
7
8
[CreateButton makeButton:randomButton
Xcoord:0
Ycoord:0
withWidth:200
withHeight:50
withTag:1
withTitle:@"Some title"
inView:self.view];

At this point everything seems ready but the compiler should throw you an error “Use of undeclared identifier randomButton“. This really puzzled me as I didn’t exactly understand what the actual problem was.  But I then understood and it seemed very logical – randomButton has to be declared in ViewController.h like this:

1
@property(nonatomic, weak) UIButton *randomButton;

The result should look something like this:

Bare in mind – this code has been developed in XCode 4.2 with use of .storyboard and ARC ( this is why you won’t see any releases )

Hello world!

Some things should be kept as they are even if it might seem cliché. So here it is – the Hello World!

And if I am to remember the easiest “Hello World!” I ever made it must be the iPhone one. It was a simple label in a .nib.

I am passionate about design and programming. At this point I am digging my way through iPhone development and this place, the blog, will be were I’ll drop some thoughts about what I shall encounter along the way. Of course my design work will make no exception.