Blog Archives

Logging service for Swift – Swell

Swell is a logging utility for Swift and Objective C.


????Turn on logging during development, turn them off when building for the App Store
????Enable or disable logging for specific classes
????Different log levels allow for finer-grained control of logging within a class
????Log to the console, text file, or a custom location
????Log message isn’t computed when logging is disabled (thanks to the @auto_closure feature)

Download here

Objective-C Blocks Cheat Sheet

Blocks are an incredibly powerful addition to Objective-C, introduced in iOS 4. However, their syntax can be maddeningly difficult to remember. Matt Gallagher has an excellent post that breaks down the syntax to help you understand it. If you haven’t read this article, go do it now.
via Feedly/Pocket on September 28, 2013 at 07:36AM


The following methods can be used with NSArray and NSMutableArray instances. With Underscore.m’s array methods can use a functional-style syntax as well as chaining to create powerful expressions. Wraps an array in an USArrayWrapper.
via Feedly/Pocket on September 05, 2013 at 11:39PM


Cocoa is the envy of other standard libraries when it comes to querying and arranging information. With NSPredicate, NSSortDescriptor, and an occasional NSFetchRequest, even the most complex data tasks can be reduced into just a few, extremely-understandable lines of code.
via Feedly/Pocket on July 10, 2013 at 08:58AM

Introduction to Objective-C Modules

Unless you’ve written C in the past, your closest encounter with the preprocessor and its code inclusion is probably the #import statement. But before #import, there was #include. The preprocessor command #include is a clever little trick.
via Feedly/Pocket on June 25, 2013 at 06:21PM

Object Subscripting

Xcode 4.4 quietly introduced a syntactic revolution to Objective-C. Like all revolutions, however, its origins and agitators require some effort to trace: Xcode 4.4 shipped with Apple LLVM Compiler 4.0, which incorporated changes effective in the Clang front-end as of version 3.1.
via Feedly/Pocket on June 24, 2013 at 07:43PM

iPhone Development 101

Code Tips Design Tools for Creating App Mockups User Interface Sizes of iPhone UI ElementsDefault (Launch) Image Sizes for iPhone & iPadView Frames and BoundsThe Status Bar – hiding; setting the colorUIAlertView – pop-up alert messagesUINavigationControllerUITableViewUIView – animating vie
via Feedly/Pocket on March 15, 2013 at 02:17PM

Understanding Objective-C Blocks

The aim of this tutorial is to give a gentle introduction to Objective-C blocks while paying special emphasis to their syntax, as well as exploring the ideas and implementation patterns that blocks make possible. In simpler words, blocks look weird and are used in weird ways.
via Feedly/Pocket on March 15, 2013 at 02:03PM

Autolayout views evenly spaced

Good solution to autolayout evenly spaced components in the view.


[self.view addConstraints:       [NSLayoutConstraint constraintsForEvenDistributionOfItems:@[label1, label2, label3, label4]                                          relativeToCenterOfItem:self.view                                                      vertically:YES]];


@interface NSLayoutConstraint (EvenDistribution)    /**   * Returns constraints that will cause a set of views to be evenly distributed horizontally   * or vertically relative to the center of another item. This is used to maintain an even   * distribution of subviews even when the superview is resized.   */  + (NSArray *) constraintsForEvenDistributionOfItems:(NSArray *)views                               relativeToCenterOfItem:(id)toView                                           vertically:(BOOL)vertically;    @end


@implementation NSLayoutConstraint (EvenDistribution)    +(NSArray *)constraintsForEvenDistributionOfItems:(NSArray *)views                             relativeToCenterOfItem:(id)toView vertically:(BOOL)vertically  {      NSMutableArray *constraints = [NSMutableArray new];      NSLayoutAttribute attr = vertically ? NSLayoutAttributeCenterY : NSLayoutAttributeCenterX;        for (NSUInteger i = 0; i < [views count]; i++) {          id view = views[i];          CGFloat multiplier = (2*i + 2) / (CGFloat)([views count] + 1);          NSLayoutConstraint *constraint = [NSLayoutConstraint constraintWithItem:view                                                                        attribute:attr                                                                        relatedBy:NSLayoutRelationEqual                                                                           toItem:toView                                                                        attribute:attr                                                                       multiplier:multiplier                                                                         constant:0];          [constraints addObject:constraint];      }        return constraints;  }  @end  

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!

WP Like Button Plugin by Free WordPress Templates