Posts

Showing posts with the label iOS

Swift: Understanding Mutating Functions

 classes are reference type whereas structures and enumerations are value types. The properties of value types cannot be modified within its instance methods by default. In order to modify the properties of a value type, you have to use the mutating keyword in the instance method. With this keyword, your method can then have the ability to mutate the values of the properties and write it back to the original structure when the method implementation ends. Below is a simple implementation of Stack in Swift that illustrates the use of mutating functions.     struct Stack { public private ( set ) var items = [ Int ] ( ) // Empty items array mutating func push ( _ item : Int ) { items . append ( item ) } mutating func pop ( ) - > Int ? { if ! items . isEmpty { return items . removeLast ( ) } return nil } } var stack = Stack ( ) stack . push ( 4 ) stack . push ( 78 ) stack . ...

Creating Custom URL schemes in iOS/iPhone

Image
iPhone apps can also specify their own custom URL scheme (for example, myapp://doStuff). When might you want to use a custom URL scheme for your app? To transfer data from your app to your  another app To allow other apps (or even web pages) to call your app (and send data to it) To handle callbacks for custom authentication (such as OAuth ) and third party API's Implementing a Custom URL Scheme   The first step is to create a custom URL scheme – start by locating and clicking on the project info.plist in the Xcode Project Navigator. With the plist displayed in the right pane, right click on the list and select Add Row : From the list presented scroll down and select URL types .  Select URL Types for the new item. Once that's added, click the grey arrow next to "URL Types" to show "Item 0". Set your URL identifier to a unique string - something like com.yourcompany.yourappname.    After you've set the URL identifier, sele...

Add shadow to text in UITextView

Image
    NSShadow * shadow = [[NSShadow alloc] init];     shadow.shadowColor = [UIColor blackColor];     shadow.shadowOffset = CGSizeMake(1, 2);        NSDictionary * textAttributes =  @{                                        NSForegroundColorAttributeName : [UIColor blueColor],                                        NSShadowAttributeName          : shadow,                       ...

add small image after text in UILabel

NSTextAttachment * attachment = [[ NSTextAttachment alloc ] init ]; attachment . image = [ UIImage imageNamed :@ "myimage.png" ]; NSAttributedString * attachmentString = [ NSAttributedString attributedStringWithAttachment : attachment ]; NSMutableAttributedString * myString = [[ NSMutableAttributedString alloc ] initWithString :@ "My label text" ]; [ myString appendAttributedString : attachmentString ]; myLabel . attributedText = myString ;

Struck through text in UILabel

In iOS 6.0 and up UILabel supports NSAttributedString NSMutableAttributedString * attributeString = [[ NSMutableAttributedString alloc ] initWithString :@ "Your String here" ]; [ attributeString addAttribute : NSStrikethroughStyleAttributeName value :@ 2 range : NSMakeRange ( 0 , [ attributeString length ])]; Definition : - ( void ) addAttribute :( NSString *) name value :( id ) value range :( NSRange ) aRange Parameters List: name : A string specifying the attribute name. Attribute keys can be supplied by another framework or can be custom ones you define. For information about where to find the system-supplied attribute keys, see the overview section in NSAttributedString Class Reference. value : The attribute value associated with name. aRange : The range of characters to which the specified attribute/value pair applies. Then yourLabel . attributedText = attributeString ;

Change Status Bar text color in iPhone

Image
Go to --> Info.plist :   1) View controller-based status bar appearance : NO   2) Status bar style : Opaque black style

Scroll UITextField above Keyboard in a UITableView OR UIScrollView in Swift and Objective C

In Objective C -(BOOL)textFieldShouldBeginEditing:(UITextField *)textField {     CGPoint pointInTable = [textField.superview convertPoint:textField.frame.origin toView:_tableview];     CGPoint contentOffset = _tableview.contentOffset;         contentOffset.y = (pointInTable.y - textField.inputAccessoryView.frame.size.height);         NSLog(@"contentOffset is: %@", NSStringFromCGPoint(contentOffset));        [_tableview setContentOffset:contentOffset animated:YES];        return YES; } -(BOOL)textFieldShouldEndEditing:(UITextField *)textField {     [textField resignFirstResponder];        if ([textField.superview.superview isKindOfClass:[UITableViewCell class]])     {         CGPoint buttonPosition = [textField convertPoint:CGPointZero  ...

Check for an active Internet Connection on iPhone SDK

METHOD 1: Use a simple (ARC and GCD compatible) class to do it 1) Add SystemConfiguration framework to the project but don't worry about including it anywhere 2) Add Tony Million's version of Reachability.h and Reachability.m to the project (For Reachability download) 3) Update the interface section     #import "Reachability.h"     // Add this to the interface in the .m file of your view controller     @interface MyViewController ()     {         Reachability *internetReachableFoo;     }     @end 4) Then implement this method in the .m file of your view controller which you can call     // Checks if we have an internet connection or not     - (void)testInternetConnection     {           internetReachableFoo = [Reachability reachabilityWithHostname:@"...

CGRectIntegral

Image
CGRectIntegral : Returns the smallest rectangle that results from converting the source rectangle values to integers. It's important that CGRect values all are rounded to the nearest whole point. Fractional values cause the frame to be drawn on a pixel boundary . Because pixels are atomic units (cannot be subdivided) a fractional value will cause the drawing to be averaged over the neighboring pixels, which looks blurry. CGRectIntegral will floor each origin value, and ceil each size value, which will ensure that your drawing code will crisply align on pixel boundaries. As a rule of thumb, if you are performing any operations that could result in fractional point values (e.g. division, CGRectGetMid[X|Y] , or CGRectDivide ), use CGRectIntegral to normalize rectangles to be set as a view frame.  Technically, since the coordinate system operates in terms of points, Retina screens, which have 4 pixels for every point, can draw ± 0.5f point values on odd pixel...

typedef enum in Objective-C

Three things are being declared here: an anonymous enumerated type is declared, ShapeType is being declared a typedef for that anonymous enumeration, and the three names kCircle , kRectangle , and kOblateSpheroid are being declared as integral constants. Let's break that down.  In the simplest case, an enumeration can be declared as     enum tagname { ... }; This declares an enumeration with the tag tagname .  In C and Objective-C (but not C++), any references to this must be preceded with the enum keyword.  For example:     enum tagname x;  // declare x of type 'enum tagname'     tagname x;  // ERROR in C/Objective-C, OK in C++ In order to avoid having to use the enum keyword everywhere, a typedef can be created:     enum tagname { ... };     typedef enum tagname tagname;  // declare 'tagname' as a typedef for 'enum tagname' This can be simplified into one ...

Change the name of an iOS app

Image
Go to Targets in Xcode "Build Settings" on your project's targe Search for "Product Name" under "Packaging". Change the value of that what you want the new program name is going to be.

Disable ARC for a single file in a project

Image
It is possible to disable ARC for individual files by adding the -fno-objc-arc compiler flag for those files. You add compiler flags in Targets -> Build Phases -> Compile Sources . You have to double click on the right column of the row under Compiler Flags . You can also add it to multiple files by holding the cmd button to select the files and then pressing enter to bring up the flag edit box.

Custom fonts in iPhone

Image
Copy your font file into resources Add a key to your Info.plist file called UIAppFonts. ("Fonts provided by application) Make this key an array For each font you have, enter the full name of your font file (including the extension) as items to the UIAppFonts array Save Info.plist Now in your application you can simply call [UIFont fontWithName:@"CustomFontName" size:15] to get the custom font to use with your UILabels and UITextViews , etc…