0

How to send In-App SMS

-

Officially, iPhone OS 4 is out of NDA and I can’t write a post on this. If you have been reading my blogs, you might already know how to send a in-app email Sending a in-app SMS is very similar to this, but with subtle differences.

Prior to iPhone OS 4, developers have to depend on
<br />

 

  1. [[UIApplication sharedApplication] openURL: @"sms:12345678"];
[[UIApplication sharedApplication] openURL: @"sms:12345678"];

 

 

 The problem with this is not just that it closes your app, but there is no way to specify the body content of the SMS. Secondly, you are restricted to send the SMS to only one person. However, with the new MessageUI SMS controller, you can send SMS to multiple people at the same time. You can also pre-populate the SMS body field.

Developers of famous apps like Whatsapp Messenger, copy the SMS text content to clipboard and open the SMS app to allow users to paste the content. But with this newly allowed In-App SMS sheet, users can send SMS without quitting the app.
So, Let’s get started.
Step 1:
Import the MessageUI Framework into your project and #import the header file into the “.h” file of your controller where you want to open the In-App SMS sheet.
Step 2:
You might already have a button handler IBAction where you want to send the SMS. If not create a Button on your XIB file and write IBActions for it.
Step 3:
The real code
<br />
 

  1. -(IBAction) sendInAppSMS:(id) sender
  2.  
  3. {
  4. MFMessageComposeViewController *controller = [[[MFMessageComposeViewController alloc] init] autorelease];
  5. if([MFMessageComposeViewController canSendText])
  6. {
  7. controller.body = @"Hello from Mugunth";
  8. controller.recipients = [NSArray arrayWithObjects:@"12345678", @"87654321", nil];
  9. controller.messageComposeDelegate = self;
  10. [self presentModalViewController:controller animated:YES];
  11. }
  12. }
-(IBAction) sendInAppSMS:(id) sender

{
MFMessageComposeViewController *controller = [[[MFMessageComposeViewController alloc] init] autorelease];
if([MFMessageComposeViewController canSendText])
{
controller.body = @"Hello from Mugunth";
controller.recipients = [NSArray arrayWithObjects:@"12345678", @"87654321", nil];
controller.messageComposeDelegate = self;
[self presentModalViewController:controller animated:YES];
}
}

The most important part here is the line
<br />

[MFMessageComposeViewController canSendText].
When sending a in-app email, you can choose to ignore this (atleast as of now) because most of the devices would have upgraded to iPhone OS 3 and all those devices would have the ability to send in-app email. However, the same doesn’t apply to SMS. Remember that even if a device is running iPhone OS 4, if it’s an iPod touch, it will never be abel to send SMS within app.
In this case, I have used a if condition to send the SMS. Practically speaking, you should enable/disable the button the user taps to send the sms based on this. You can add the code that does this in your viewDidLoad method.

Secondly, you have to set the messageComposeDelegate to self and not delegate. If you set the controller.delegate to self, you will not get the didFinishWithResult callback and the In-App SMS sheet will not close.
Step 4:
Implement Delegate Callbacks.
In your header file, implement the callbacks, MFMessageComposeViewControllerDelegate and UINavigationControllerDelegate. If you don’t you will get a warning at the line,
<br />

controller.delegate = self;
You have to handle a callback method of MFMessageComposeViewControllerDelegate so as to dismiss the modal view controller.
<br />

  1. - (void)messageComposeViewController:(MFMessageComposeViewController *)controller didFinishWithResult:(MessageComposeResult)result
  2.  
  3. {
  4. switch (result) {
  5. case MessageComposeResultCancelled:
  6. NSLog(@"Cancelled");
  7. break;
  8. case MessageComposeResultFailed:
  9. UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"MyApp" message:@"Unknown Error"
  10. delegate:self cancelButtonTitle:@”OK” otherButtonTitles: nil];
  11. [alert show];
  12. [alert release];
  13. break;
  14. case MessageComposeResultSent:
  15.  
  16. break;
  17. default:
  18. break;
  19. }
  20.  
  21. [self dismissModalViewControllerAnimated:YES];
  22. }
- (void)messageComposeViewController:(MFMessageComposeViewController *)controller didFinishWithResult:(MessageComposeResult)result

{
switch (result) {
case MessageComposeResultCancelled:
NSLog(@"Cancelled");
break;
case MessageComposeResultFailed:
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"MyApp" message:@"Unknown Error"
delegate:self cancelButtonTitle:@”OK” otherButtonTitles: nil];
[alert show];
[alert release];
break;
case MessageComposeResultSent:

break;
default:
break;
}

[self dismissModalViewControllerAnimated:YES];
}

That’s it. Your app should now be able to send SMS using the new Message UI sheet.

Where the heck is MMS in this tutorial?
As on date, the MFMessageComposeViewController doesn’t support sending MMS. The controller.body is a NSString and setting a NSData pointer obviously crashes the app. Hopefully, one day, Apple will allow sending In-App MMS and I’ll probably blog about that too…

0

Fun With UIButtons and Core Animation Layers

-

 

 

Upon first glance, the UIButton class doesn’t seem to provide what you might expect in terms of customization. This often causes developers to resort to creating buttons in an image editor and then specifying that in the Background field in Interface Builder. This is a fine solution and will likely give you what you need, but with Core Animation layers there is a simpler way to achieve the look you want without having to create an image. This post will demonstrate how.

Setting a Background Color

Solid Background Buttons

In Interface Builder, you can specify a background color for your button if you are using the ‘Custom’ button setting, but when you run the application, the button will display as a block with no rounded corners. This is because custom buttons don’t really have any default attributes defined. It’s completely up to you to define them. Core Animation layers can help.

Note: Before I get into the source code, I want to remind you that you’ll need to add the QuartzCore framework to your project and #import <QuartzCore/QuartzCore.h> into one of your header files to have Core Animation layer support. I normally add this import statement to my precompiled header (.pch) file.

What Interface Builder doesn’t give you, you can still take advantage of by writing a little bit of code. For example, if you want your custom button to have a red background color with rounded corners and a border, you need to define an outlet in your view controller where the button will be referenced and set the following attributes on the UIButton’s layer.

  1. [[button1 layer] setCornerRadius:8.0f];
  2. [[button1 layer] setMasksToBounds:YES];
  3. [[button1 layer] setBorderWidth:1.0f];
[[button1 layer] setCornerRadius:8.0f];
[[button1 layer] setMasksToBounds:YES];
[[button1 layer] setBorderWidth:1.0f];

With this code the layer gets a corner radius of 8.0 and the -setMasksToBounds: tells the button’s layer to mask any layer content that comes below it in the layer tree. This is necessary in order for the layer to mask off the rounded corners.

Finally, set the border width to 1.0 to display an outline around the button. The default color for this border is black. You can change it to anything you like using -setBorderColor: on the layer passing it a CGColorRef (e.g. [[UIColor greenColor] CGColor] would give you a green border).

iPhone Development Protip: Rounding corners is possible on any UIView based view. All UIViews have a root layer. You simply call -setCornerRadius: and -setMasksToBounds: on the view’s layer and your corners will be rounded. It is that simple.

You can set the background color in Interface Builder or in code–whichever you prefer. There are two ways to do this in code. One using the layer and one using the UIView call to -setBackgroundColor:.

  1. // Core Animation way
  2. [[button1 layer] setBackgroundColor:[[UIColor redColor] CGColor]];
  3. // UIView way
  4. [button1 setBackgroundColor:[UIColor redColor]];
// Core Animation way
[[button1 layer] setBackgroundColor:[[UIColor redColor] CGColor]];
// UIView way
[button1 setBackgroundColor:[UIColor redColor]];

The main difference between the two is that the layer works with a CGColorRef while the UIView works with a UIColor. It’s a subtle difference, but one that you should know.

Gradient Buttons Rule

Colorful Buttons App

The demo app uses some ultra-bright gaudy looking color gradients just for effect. I suggest you don’t use something so loud and obnoxious. A more subtle difference between colors will look better. Of course this is subjective, so do what you like.

To achieve this gradient look, I use a CAGradientLayer and add it to the root of the button’s layer tree. In fact, for the demo application, I created a UIButton derived class that encapsulates the gradient layer creation and drawing. Here is its implementation:

  1. #import "ColorfulButton.h"
  2.  
  3. @implementation ColorfulButton
  4.  
  5. @synthesize _highColor;
  6. @synthesize _lowColor;
  7. @synthesize gradientLayer;
  8.  
  9. - (void)awakeFromNib;
  10. {
  11.     // Initialize the gradient layer
  12.     gradientLayer = [[CAGradientLayer alloc] init];
  13.     // Set its bounds to be the same of its parent
  14.     [gradientLayer setBounds:[self bounds]];
  15.     // Center the layer inside the parent layer
  16.     [gradientLayer setPosition:
  17.                 CGPointMake([self bounds].size.width/2,
  18.                        [self bounds].size.height/2)];
  19.  
  20.     // Insert the layer at position zero to make sure the
  21.     // text of the button is not obscured
  22.     [[self layer] insertSublayer:gradientLayer atIndex:0];
  23.  
  24.     // Set the layer's corner radius
  25.     [[self layer] setCornerRadius:8.0f];
  26.     // Turn on masking
  27.     [[self layer] setMasksToBounds:YES];
  28.     // Display a border around the button
  29.     // with a 1.0 pixel width
  30.     [[self layer] setBorderWidth:1.0f];
  31.  
  32. }
  33.  
  34. - (void)drawRect:(CGRect)rect;
  35. {
  36.     if (_highColor && _lowColor)
  37.     {
  38.         // Set the colors for the gradient to the
  39.         // two colors specified for high and low
  40.         [gradientLayer setColors:
  41.                      [NSArray arrayWithObjects:
  42.                             (id)[_highColor CGColor],
  43.                             (id)[_lowColor CGColor], nil]];
  44.     }
  45.     [super drawRect:rect];
  46. }
  47.  
  48. - (void)setHighColor:(UIColor*)color;
  49. {
  50.     // Set the high color and repaint
  51.     [self set_highColor:color];
  52.     [[self layer] setNeedsDisplay];
  53. }
  54.  
  55. - (void)setLowColor:(UIColor*)color;
  56. {
  57.     // Set the low color and repaint
  58.     [self set_lowColor:color];
  59.     [[self layer] setNeedsDisplay];
  60. }
  61.  
  62. - (void)dealloc {
  63.     // Release our gradient layer
  64.     [gradientLayer release];
  65.     [super dealloc];
  66. }
  67. @end
#import "ColorfulButton.h"

@implementation ColorfulButton

@synthesize _highColor;
@synthesize _lowColor;
@synthesize gradientLayer;

- (void)awakeFromNib;
{
    // Initialize the gradient layer
    gradientLayer = [[CAGradientLayer alloc] init];
    // Set its bounds to be the same of its parent
    [gradientLayer setBounds:[self bounds]];
    // Center the layer inside the parent layer
    [gradientLayer setPosition:
                CGPointMake([self bounds].size.width/2,
                       [self bounds].size.height/2)];

    // Insert the layer at position zero to make sure the
    // text of the button is not obscured
    [[self layer] insertSublayer:gradientLayer atIndex:0];

    // Set the layer's corner radius
    [[self layer] setCornerRadius:8.0f];
    // Turn on masking
    [[self layer] setMasksToBounds:YES];
    // Display a border around the button
    // with a 1.0 pixel width
    [[self layer] setBorderWidth:1.0f];

}

- (void)drawRect:(CGRect)rect;
{
    if (_highColor && _lowColor)
    {
        // Set the colors for the gradient to the
        // two colors specified for high and low
        [gradientLayer setColors:
                     [NSArray arrayWithObjects:
                            (id)[_highColor CGColor],
                            (id)[_lowColor CGColor], nil]];
    }
    [super drawRect:rect];
}

- (void)setHighColor:(UIColor*)color;
{
    // Set the high color and repaint
    [self set_highColor:color];
    [[self layer] setNeedsDisplay];
}

- (void)setLowColor:(UIColor*)color;
{
    // Set the low color and repaint
    [self set_lowColor:color];
    [[self layer] setNeedsDisplay];
}

- (void)dealloc {
    // Release our gradient layer
    [gradientLayer release];
    [super dealloc];
}
@end


Now, when I’m creating a button in Interface Builder, I can make it of classColorfulButton and then set an outlet for it in my view controller.

If I don’t set colors for a gradient, it will just render the button using the background color I specify for it in Interface Builder. If, however, I want to take advantage of the gradient capability, I set the gradient colors in my view controller as shown in the following code snippet:

  1. - (void)viewDidLoad {
  2.     [super viewDidLoad];
  3.  
  4.     [button1 setHighColor:[UIColor redColor]];
  5.     [button1 setLowColor:[UIColor orangeColor]];
  6.  
  7.     [button2 setHighColor:[UIColor blueColor]];
  8.     [button2 setLowColor:[UIColor lightGrayColor]];
  9.  
  10.     [button3 setHighColor:[UIColor yellowColor]];
  11.     [button3 setLowColor:[UIColor purpleColor]];
  12.  
  13.     [button4 setHighColor:[UIColor cyanColor]];
  14.     [button4 setLowColor:[UIColor magentaColor]];
  15. }
- (void)viewDidLoad {
    [super viewDidLoad];

    [button1 setHighColor:[UIColor redColor]];
    [button1 setLowColor:[UIColor orangeColor]];

    [button2 setHighColor:[UIColor blueColor]];
    [button2 setLowColor:[UIColor lightGrayColor]];

    [button3 setHighColor:[UIColor yellowColor]];
    [button3 setLowColor:[UIColor purpleColor]];

    [button4 setHighColor:[UIColor cyanColor]];
    [button4 setLowColor:[UIColor magentaColor]];
}


These are the first four buttons you see in the demo app screenshot above. The buttons are declared in the view controller header as show in the following snippet:

  1. #import <UIKit/UIKit.h>
  2. #import "ColorfulButton.h"
  3.  
  4. @interface ColorfulButtonsViewController : UIViewController {
  5.     IBOutlet ColorfulButton *button1;
  6.     IBOutlet ColorfulButton *button2;
  7.     IBOutlet ColorfulButton *button3;
  8.     IBOutlet ColorfulButton *button4;
  9. }
  10. @end
#import <UIKit/UIKit.h>
#import "ColorfulButton.h"

@interface ColorfulButtonsViewController : UIViewController {
    IBOutlet ColorfulButton *button1;
    IBOutlet ColorfulButton *button2;
    IBOutlet ColorfulButton *button3;
    IBOutlet ColorfulButton *button4;
}
@end


The CAGradientLayer supports adding an array of colors and will automatically render them linearly with equal distribution. It will also allow you to specify the distribution pattern, however, to keep it simple here I just use two colors represented by the upper color called highColor and the lower color called lowColor. If you want to add more complex gradients, you could modify my ColorfulButtonclass to take an array of colors as well. I leave this as an exercise for the reader.

Conclusion

Next time you need to customize a button, consider using Core Animation layers first. You may be surprised at what all you can achieve without having to resort to creating images to use as the button background. I have included the source code for this blog post below. Let me know if you have any thoughts or questions about it in the comments section. Until next time.

 

Cocoa Is My Girlfriend » Fun With UIButtons and Core Animation Layers.

1

Small feature with UIlabel

-

Add framework QuartzCore

  1. #import <QuartzCore/QuartzCore.h>
  2.  
  3. UILabel *label = [[UILabel alloc] init];
  4. [label setTextColor:[UIColor whiteColor]];
  5. [label setBackgroundColor:[UIColor darkGrayColor]];
  6.  
  7. [[label layer] setBorderWidth:2];
  8. [[label layer] setCornerRadius:15];
  9. [[label layer] setBorderColor:[UIColor whiteColor].CGColor];
  10.  
  11. [label setAlpha:0.8];
  12. [label setTextAlignment:UITextAlignmentCenter];
  13. [label setFrame:CGRectMake(0, 0, 220, 50)];
  14. [label setText:@"Loading..."];
  15. [label setCenter:window.center];
  16. [window addSubview:label];
#import <QuartzCore/QuartzCore.h>

UILabel *label = [[UILabel alloc] init];
[label setTextColor:[UIColor whiteColor]];
[label setBackgroundColor:[UIColor darkGrayColor]];

[[label layer] setBorderWidth:2];
[[label layer] setCornerRadius:15];
[[label layer] setBorderColor:[UIColor whiteColor].CGColor];

[label setAlpha:0.8];
[label setTextAlignment:UITextAlignmentCenter];
[label setFrame:CGRectMake(0, 0, 220, 50)];
[label setText:@"Loading..."];
[label setCenter:window.center];
[window addSubview:label];

Result:

0

How to include ttf fonts to iOS app

-

Up till now there hasn’t been an easy way to add custom fonts to your iPhone applications. As of iOS 4 it has become very easy to do. Here is what you need to do in order to add custom fonts:

  1. Add your custom font files into your project using XCode as a resource
  2. Add a key to your info.plist file called UIAppFonts.
  3. Make this key an array
  4. For each font you have, enter the full name of your font file (including the extension) as items to the UIAppFonts array
  5. Save info.plist
  6. Now in your application you can simply call [UIFont fontWithName:@"CustomFontName" size:12] to get the custom font to use with your UILabels and UITextViews, etc…

It’s that simple!

How to include ttf fonts to iOS app - Notes of a Developer.

0

Setting the text color of an NSButton

-

Here’s a simple little category on NSButton which adds -textColor and -setTextColor: methods to the class.

Header:

  1. #import <Cocoa/Cocoa.h>
  2. @interface NSButton (TextColor)
  3.  
  4. - (NSColor *)textColor;
  5. - (void)setTextColor:(NSColor *)textColor;
  6.  
  7. @end
#import <Cocoa/Cocoa.h>
@interface NSButton (TextColor)

- (NSColor *)textColor;
- (void)setTextColor:(NSColor *)textColor;

@end


Implementation:

  1. #import "NSButton+TextColor.h"
  2. @implementation NSButton (TextColor)
  3.  
  4. - (NSColor *)textColor
  5. {
  6.     NSAttributedString *attrTitle = [self attributedTitle];
  7.     int len = [attrTitle length];
  8.     NSRange range = NSMakeRange(0, MIN(len, 1)); // take color from first char
  9.     NSDictionary *attrs = [attrTitle fontAttributesInRange:range];
  10.     NSColor *textColor = [NSColor controlTextColor];
  11.     if (attrs) {
  12.         textColor = [attrs objectForKey:NSForegroundColorAttributeName];
  13.     }
  14.     return textColor;
  15. }
  16.  
  17. - (void)setTextColor:(NSColor *)textColor
  18. {
  19.     NSMutableAttributedString *attrTitle = [[NSMutableAttributedString alloc]
  20.                                  initWithAttributedString:[self attributedTitle]];
  21.     int len = [attrTitle length];
  22.     NSRange range = NSMakeRange(0, len);
  23.     [attrTitle addAttribute:NSForegroundColorAttributeName
  24.                       value:textColor
  25.                       range:range];
  26.     [attrTitle fixAttributesInRange:range];
  27.     [self setAttributedTitle:attrTitle];
  28.     [attrTitle release];
  29. }
  30.  
  31. @end
#import "NSButton+TextColor.h"
@implementation NSButton (TextColor)

- (NSColor *)textColor
{
    NSAttributedString *attrTitle = [self attributedTitle];
    int len = [attrTitle length];
    NSRange range = NSMakeRange(0, MIN(len, 1)); // take color from first char
    NSDictionary *attrs = [attrTitle fontAttributesInRange:range];
    NSColor *textColor = [NSColor controlTextColor];
    if (attrs) {
        textColor = [attrs objectForKey:NSForegroundColorAttributeName];
    }
    return textColor;
}

- (void)setTextColor:(NSColor *)textColor
{
    NSMutableAttributedString *attrTitle = [[NSMutableAttributedString alloc] 
                                 initWithAttributedString:[self attributedTitle]];
    int len = [attrTitle length];
    NSRange range = NSMakeRange(0, len);
    [attrTitle addAttribute:NSForegroundColorAttributeName 
                      value:textColor 
                      range:range];
    [attrTitle fixAttributesInRange:range];
    [self setAttributedTitle:attrTitle];
    [attrTitle release];
}

@end


source

Notes of a Developer - Develop on Objective-C, UIKit, C, OpenGL for iPhone, iPad, Mac OS X.

0

UITextField – A Complete API Overview

-

The UITextField is probably one of the most commonly used UI controls on the iPhone. It is the primary method of user input via the keyboard and provides a great deal of additional functionality.

With the success of our las API tutorial on NSArray, I thought I would do another walkthrough, this time on UITextField. I will be explaining all of the properties for it as well as bringing up some functionality that you may not have known about.

Text Attributes

The attributes have to do with the actual text inside of the UITextField.

text The text displayed in the UITextField
placeholder The text that gets displayed prior to the user entering in anything. This text is usually a lighter color than the primary text to denote that it will be replaced.
font The font of the text to be displayed. You can set it like this
textColor The color of the text that is displayed
textAlignment How the text is aligned in the UITextField. The possible values for this are UITextAlignmentLeft, UITextAlignmentRight, UITextAlignmentCenter

Here are some examples of using these properties.

// Setting the text
[myTextField setText:@"This is some text!"];

// Setting the placeholder
[myTextField setPlaceholder:@"Type text here"];

// Setting the font.
[myTextField setFont:[UIFont fontWithName:@"Times New Roman" size:14]];

// Setting the text color
[myTextField setTextColor:[UIColor blueColor]];

// Setting the text alignment
[myTextField setTextAlignment:UITextAlignmentCenter];

Here is what the UITextField would look like after we update these properties.

Adjusting the size of the text in the UITextField

The text displayed in our UITextField can be dynamically sized based on the width of the UITextField. The benefit of this is all of the text being typed will be visible on the screen. It will shrink the text down until it reaches the default font size of 17. So, for this to make sense, you must set the font size of the UITextField to something larger than 17.

adjustsFontSizeToFitWidth Boolean value denoting whether to fit the font size to the width of the UITextField.

Here is an example of using these properties.

[myTextField setFont:[UIFont fontWithName:@"Times New Roman" size:30]];
[myTextField setAdjustsFontSizeToFitWidth:YES];

Here are some screenshots of the text shrinking when typing in the UITextField.

Managing the editor’s behavior

These two properties are pretty straight forward.

editing Read-only boolean value letting you know if the user is currently editing the UITextField
clearsOnBeginEditing Clears the text in the field every time the user begins to edit it.

Not very exciting and probably doesn’t even deserve an example…

Setting the view’s background appearance

This group of properties defines how the UITextField will look. If you have ever seen a fancy input box, this is how they are doing it.

borderStyle Defines the type of border for the UITextField. Possible choices are UITextBorderStyleNone, UITextBorderStyleLine, UITextBorderStyleBezel, and UITextBorderStyleRoundedRect. The default is UITextBorderStyleNone.
background A UIImage representing the background image of the UITextField when it’s enabled. If this field is altered the borderStyle property is ignored.
backgroundDisabled A UIImage representing the background image of the UITextField when it’s disabled.

Here is are some example of using each of the border styles

// Border Style None
[myTextField setBorderStyle:UITextBorderStyleNone];

// Border Style Line
[myTextField setBorderStyle:UITextBorderStyleLine];

// Border Style Bezel
[myTextField setBorderStyle:UITextBorderStyleBezel];

// Border Style Rounded Rect
[myTextField setBorderStyle:UITextBorderStyleRoundedRect];

The border style is not terribly exciting. However, you can really spruce up your UITextFields using the background property. Here is an example of setting the background property to this image.

myTextField.textAlignment = UITextAlignmentCenter;
myTextField.textColor = [UIColor whiteColor];
myTextField.borderStyle = UITextBorderStyleNone;
myTextField.background = [UIImage imageNamed:@"bg.png"];

and the result… Looks pretty good ehh? One GOTCHA that I want to point out here is, to get the background property to work correctly, you must set the boderStyle to anything other than UITextBorderStyleRoundedRect. Otherwise, the default UITextField will be displayed.
Setting the view’s background appearance

Managing Overlay Views

Another interesting way of customizing your UITextFields is to use an overlay. UITextField offers a left and right overlay for your UITextFields. Here are the properties:

clearButtonMode The circled X that gets displayed when typing. Used to clear out the text. Possible values: UITextFieldViewModeNever, UITextFieldViewModeWhileEditing, UITextFieldViewModeUnlessEditing, UITextFieldViewModeAlways
leftView The view that appears to the left inside a UITextField. This could be something like a magnifying glass for search.
leftViewMode Works like clearButtonMode, but toggles the leftView.
rightView Same as leftView, except it aligns to the right.
rightViewMode Same as leftViewMode but controls the rightView

Let’s take a look at how adjusting the leftView works:

UIImageView * myView = [[ UIImageView  alloc ]  initWithImage :
		[UIImage  imageNamed : @"wordpress.png" ]];
[myTextField  setLeftView :myView];
[ myTextField   setLeftViewMode: UITextFieldViewModeAlways];
[myView release ];

As you can see, the text aligns after the image. This is a very simple way to really spruce up your UITextFields.

The last thing we are going to discuss is showing and hiding the keyboard.

Showing and Hiding The Keyboard

To show the keyboard:

[myTextField becomeFirstResponder];

To hide the keyboard

[myTextField resignFirstResponder];

Well, I hope you have enjoyed this tutorial on the UITextField.  I would love to see links to some interesting custom UITextFields in the comments, so please post them.  Thanks for reading and happy iCoding!

 

UITextField – A Complete API Overview | iPhone Programming Tutorials.

« 1 2 3 4 5 6 7 8 9 10 »