Posts

Showing posts with the label Objective-C

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 ;

Add one minutes in NSDate

You can use dateByAddingTimeInterval. NSDate *currentDate = [NSDate date]; NSDate *date_plus_one_minute = [currentDate dateByAddingTimeInterval:60]; //60 seconds

Decode a UTF8 encoded NSString

While programming in iOS sometime we have to hit certain URLs which in turn returns Encoded string.Which we have to decode to get the actual string. NSString *currentEncodedString =@"%3CTom%26Jerry%3E"; //Received Encoded UTF8 String NSString *currentDecodedString = [currentEncodedString stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding]; NSLog(@"My Current Decoded String: %@",currentDecodedString);

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:@"...

Check if a string contains another string in iOS 8

NSString * string = @ "hello bla blah" ; if ([ string containsString :@ "bla" ]) { NSLog (@ "string contains bla!" ); } else { NSLog (@ "string does not contain bla" ); }

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 ...

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…