Sunday, July 10, 2011

Find User's Language in iPhone

If you are writing an application where you want to know what language is set in user's setting, then you can use following code snippet. This will give you an idea how to check for any other language, this code only checks for German and English.

              NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
NSArray *languages = [defaults objectForKey:@"AppleLanguages"];
NSString *currentLanguage = [languages objectAtIndex:0];
    
    if([currentLanguage compare:@"de"] == NSOrderedSame
        NSLog(@"Its German");
    else if([currentLanguage compare:@"en"] == NSOrderedSame
        NSLog(@"Its English");

Friday, July 8, 2011

Draw Radial Gradient in Quartz

Here is the code snippet for that to be written inside - (void)drawRect:(CGRect)rect of UIView




    CGContextRef ctx = UIGraphicsGetCurrentContext();

    
    
    //Draw the gray Gradient
    
    CGFloat BGLocations[2] = { 0.0, 1.0 };
    CGFloat BgComponents[8] = { 0.25, 0.25, 0.25 , 1.0// Start color
       0.11, 0.11, 0.11, 1.0 }; // Mid color and End color
    CGColorSpaceRef BgRGBColorspace = CGColorSpaceCreateDeviceRGB();
    CGGradientRef bgRadialGradient = CGGradientCreateWithColorComponents(BgRGBColorspace, BgComponents, BGLocations, 2);
    
    
    CGPoint startBg = CGPointMake(250, 250); 
    CGFloat endRadius= 250;
    
    
    CGContextDrawRadialGradient(ctx, bgRadialGradient, startBg, 0, startBg, endRadius, kCGGradientDrawsAfterEndLocation);
    CGColorSpaceRelease(BgRGBColorspace);
    CGGradientRelease(bgRadialGradient);
    







Screenshot of the output

Monday, July 4, 2011

3D shapes and Rotation in Opengl ES 2.0

It will be helpful to go through Previous tutorial before reading this one. LINK

Now, we will change the structure of Vertex a little. Position array is now of size 3 to handle x, y as well as z.


struct Vertex {
    float Position[3];
    float Color[4];
};



Our rotation matrix
void ApplyRotationX(float degrees)
{
    float radians = degrees * 3.14159f / 180.0f;
    float s = std::sin(radians);
    float c = std::cos(radians);
    float zRotation[16] = {
        1, 0, 0, 0,
        0, c, s, 0,
        0, -s, c, 0,
        0, 0, 0, 1
    };
    
    GLint modelviewUniform = glGetUniformLocation(buildProgram, "Modelview");
    glUniformMatrix4fv(modelviewUniform, 1, 0, &zRotation[0]);
}

void ApplyRotationY(float degrees)
{
    float radians = degrees * 3.14159f / 180.0f;
    float s = std::sin(radians);
    float c = std::cos(radians);
    float zRotation[16] = {
        s, 0, c, 0,
        0, 1, 0, 0,
        c, 0, -s, 0,
        0, 0, 0, 1
    };
    
    GLint modelviewUniform = glGetUniformLocation(buildProgram, "Modelview");
    glUniformMatrix4fv(modelviewUniform, 1, 0, &zRotation[0]);
}

void ApplyRotationXY(float degrees)
{
    float radians = degrees * 3.14159f / 180.0f;
    float s = std::sin(radians);
    float c = std::cos(radians);
    float zRotation[16] = {
        s, 0, c, 0,
        s*c, c, -s* s, 0,
        c*c, -s, -c * s, 0,
        0, 0, 0, 1
    };
    
    GLint modelviewUniform = glGetUniformLocation(buildProgram, "Modelview");
    glUniformMatrix4fv(modelviewUniform, 1, 0, &zRotation[0]);
}




Arrays of vertex and indices

// Define the positions and colors(R,G,B,A) of 8 vertices of square.
const Vertex Vertices[] = {
    {{0, 0, 0}, {1, 0, 0, 1}},
    {{0, 0.5, 0},  {0, 1, 0, 1}},
    {{0.5, 0.5, 0},  {1, 1, 0, 1}},
    {{0.5, 0, 0}, {1, 0, 1, 1}},
    {{0, 0, 0.5}, {0, 1, 1, 1}},
    {{0, 0.5, 0.5},  {0, 0.5, 0, 1}},
    {{0.5, 0.5, 0.5},  {0.5, 0.5, 0, 1}},
    {{0.5, 0, 0.5}, {0.5, 0, 0.5, 1}},
  
};

//Define the order of vertices for 12 triangles/
//0,1,2 forms first triangle
//2,3,0 form second triangle.
const GLubyte Indices[] = {
    0, 1, 2,
    2, 3, 0,
    4,5,6,
    6,7,4,
    0,1,4,
    1,4,5,
    1,2,6,
    1,5,6,
    2,3,6,
    3,6,7,
    0,3,4,
    3,4,7,
    
};


And finally drawing

       // Drawing code
    glClearColor(1.0f, 1.0f, 1.0f, 1);
    glClear(GL_COLOR_BUFFER_BIT);
    
    switch (rotateAlong) {
        case 1:
            ApplyRotationX(degreesX);
            break;
        case 2:ApplyRotationY(degreesY);
            break;
        default:ApplyRotation(degreesZ);
            break;
    }
    
    
    
    
    glEnableVertexAttribArray(_positionSlot);
    glEnableVertexAttribArray(_colorSlot);
    
    
    //Lets give these functions pointer to head of vertex array.
    GLsizei stride = sizeof(Vertex);
    const GLvoid* pCoords = &Vertices[0].Position[0];
    const GLvoid* pColors = &Vertices[0].Color[0];
    
   //Attribute changed from 2 (when we had drawn square) to 3 (dealing with cube x,y and z)
    glVertexAttribPointer(_positionSlot, 3, GL_FLOAT, GL_FALSE, stride, pCoords);
    glVertexAttribPointer(_colorSlot, 4, GL_FLOAT, GL_FALSE, stride, pColors);
    
    //Draw the 12 triangles and (12*3=)36 indices.
    const GLvoid* bodyIndices = &Indices[0];
    glDrawElements(GL_TRIANGLES,12* 3, GL_UNSIGNED_BYTE, bodyIndices);
    
    glDisableVertexAttribArray(_positionSlot);
    glDisableVertexAttribArray(_colorSlot);    
    
    [mimContext presentRenderbuffer:GL_RENDERBUFFER];


In order to know about rotation matrix, read this LINK
It tells you about matrix used for rotation around x, y and z axis. For rotation around X as well Y (mixed rotation) we just multiply rotation matrix of X and Y axis.

There are 3 sliders with which  you can rotate the cube along X, Y and Z axis.

     //Solo Rotation along either of X,Y or Z.
    switch (rotateAlong) {
        case 1:
            ApplyRotationX(degreesX);
            break;
        case 2:ApplyRotationY(degreesY);
            break;
        default:ApplyRotation(degreesZ);
            break;
    }


    //Comment above lines in switch in code
    //Uncomment following lines to see mixed rotation.
    //Mixed Rotation along X and Y
    ApplyRotationXY(40);
    

Check out screenshot:
















You can download the code HERE

Localization of nib file.

Ok. In previous we post we talked about how to get the localized string values on your .m files. LINK.

In this post, we will talk about how to localize the strings present in the nib file.

One way would be to make the connections of all the elements in the view and then set their title text with NSLocalizedString(@"key", @""); .Sometimes this is the only way to do it when the text in the nib files are dynamic.

Next way(For static strings in the nib files), recommended by Apple is following. We have created a nib file for us. And it looks like following.



























Because it is in english, we will put in en.lproj directory.

















 Now we open Terminal to write some commands to extract the english strings from our nib file into a new string file. By using cd I am inside the Localization folder. Following is my dir structure seen on Finder window.
















I write following command in Terminal window to extract strings file.





ibtool --generate-strings-file en.lproj/HomeController.strings en.lproj/HomeController.xib





(Note: the command above will generate the HomeController.strings file in en.lproj  folder, you will have to add it in your Xcode yourself manually ) HomeController.strings  string file which looks like following:












We create the copy of  HomeController.strings in de.lproj folder and replace the english strings with german strings. Manually add it in Xcode. 
HomeController.strings  string file which looks like following:



































Now we will create the  german version of the nib file by merging the german string file (I mean : de.lproj> HomeController.strings) with original nib file by using following command


ibtool --strings-file de.lproj/HomeController.strings --write de.lproj/HomeController.xib en.lproj/HomeController.xib

This  will generate german translated nib file in de.lproj folder. You add it manually in your Xcode. It looks like following:
Now we just add the HomeController in our window and run the app. You will english version of nib file when language of device is english and german when language of device is set as german.


Download the code HERE

In case, you want to know How to change Language in Simulator. LINK