Some little tricks.

Source: Internet
Author: User
Tags float number time zones

1. If you want to process a picture in the program (get part of a picture), you can use the following code:

UIImage *image = [UIImage imagenamed:filename];

Cgimageref imageref = image. Cgimage;

CGRect rect = CGRectMake (origin.x, ORIGIN.Y, Size.width, size.height);

Cgimageref imagerefrect = Cgimagecreatewithimageinrect (imageref, rect);

UIImage *imagerect = [[UIImage alloc] initwithcgimage:imagerefrect];

2, to determine whether the device is the iphone or iphone4 code:

#define Isretina ([UIScreen instancesrespondtoselector: @selector (currentmode)]? Cgsizeequaltosize (Cgsizemake (640, 960), [[UIScreen Mainscreen] currentmode].size): NO)

3, determine whether the mailbox input is correct:

-(BOOL) Validateemail: (NSString *) candidate {

NSString *emailregex = @ "[A-z0-9a-z._%+-][email protected][a-za-z0-9.-]+\. [A-za-z] {2,4} ";

Nspredicate *emailtest = [Nspredicate predicatewithformat:@ "Self MATCHES%@", Emailregex];

return [Emailtest evaluatewithobject:candidate];

}

4, how to save the current view as a photo to the album:

#import <QuartzCore/QuartzCore.h>

Uigraphicsbeginimagecontext (currentView.bounds.size); CurrentView the current View

[Currentview.layer Renderincontext:uigraphicsgetcurrentcontext ()];

UIImage *viewimage = Uigraphicsgetimagefromcurrentimagecontext ();

Uigraphicsendimagecontext ();

Uiimagewritetosavedphotosalbum (viewimage, nil, nil, nil);

5, local notification (similar to push notification) press the home key to the background 10 seconds after the trigger:

Uilocalnotification *notification=[[uilocalnotification alloc] init];

if (Notification!=nil) {

NSLog (@ ">> support local Notification");

NSDate *now=[nsdate New];

Notification.firedate=[now Addtimeinterval:10];

Notification.timezone=[nstimezone Defaulttimezone];

[Email protected] "It's time to have dinner!" ";

[[UIApplication sharedapplication].schedulelocalnotification:notification];

}

6. Capturing iphone Call events:

Ctcallcenter *center = [[Ctcallcenter alloc] init];

Center.calleventhandler = ^ (Ctcall *call)

{

NSLog (@ "call:%@", call.callstate);

}

7, IOS 4 introduced multi-tasking support, so the user press "Home" key after the program may not exit but into the background to run. If you want to get the app to exit directly, the simplest way is to find application does not run in background info-plist, and tick.

8. Rotate the image of the Uiimageview:

float rotateangle = M_PI;

Cgaffinetransform transform =cgaffinetransformmakerotation (rotateangle);

Imageview.transform = transform;

9. Set the origin of the rotation:

#import <QuartzCore/QuartzCore.h>

Uiimageview *imageview = [[Uiimageview alloc] initwithimage:[uiimage imagenamed:@ "Bg.png"];

ImageView.layer.anchorPoint = Cgpointmake (0.5, 1.0);

10. Implement a custom status bar (cloak status bar):

CGRect frame = {{0, 0}, {320, 20}};

uiwindow* WD = [[UIWindow alloc] initwithframe:frame];

[WD Setbackgroundcolor:[uicolor Clearcolor]];

[WD Setwindowlevel:uiwindowlevelstatusbar];

frame = CGRectMake (100, 0, 30, 20);

uiimageview* img = [[Uiimageview alloc] initwithframe:frame];

[img Setcontentmode:uiviewcontentmodecenter];

[img setimage:[uiimage imagenamed:@ "00_0103.png"];

[WD ADDSUBVIEW:IMG];

[WD Makekeyandvisible];

[UIView Beginanimations:nil Context:nil];

[UIView Setanimationduration:2];

Frame.origin.x + = 150;

[img Setframe:frame];

[UIView commitanimations];

11, in the program to achieve the call:

Add Phone icon button

UIButton *btnphone = [[UIButton Buttonwithtype:uibuttontypecustom] retain];

Btnphone.frame = CGRectMake (280,10,30,30);

UIImage *image = [UIImage imagenamed:@ "Phone.png"];

[Btnphone setbackgroundimage:image Forstate:uicontrolstatenormal];

Tap the Dial button to dial directly

[Btnphone addtarget:self Action: @selector (callaction:event:) forcontrolevents:uicontroleventtouchupinside];

[Cell.contentview Addsubview:btnphone]; Cell is a UITableViewCell

Define actions When you click the Dial button

-(void) Callaction: (ID) Sender event: (ID) event{

Nsset *touches = [Event Alltouches];

Uitouch *touch = [touches anyobject];

Cgpoint currenttouchposition = [Touch locationInView:self.listTable];

Nsindexpath *indexpath = [self.listtable indexpathforrowatpoint:currenttouchposition];

if (Indexpath = = nil) {

Return

}

Nsinteger section = [Indexpath section];

Nsuinteger row = [Indexpath row];

Nsdictionary *rowdata = [Datas Objectatindex:row];

NSString *num = [[NSString alloc] initwithformat:@ "tel://%@", number]; Number is a string of numbers

[[UIApplication sharedapplication] Openurl:[nsurl Urlwithstring:num]]; Dial

}

12. Change the keyboard color of iphone:

1. Only these 2 kinds of numeric keypad have effect. Uikeyboardtypenumberpad,uikeyboardtypephonepad

2. keyboardappearance = Uikeyboardappearancealert

-(void) textviewdidbeginediting: (Uitextview *) textview{

Nsarray *WS = [[UIApplication sharedapplication] windows];

For (UIView *w in ws) {

Nsarray *vs = [w subviews];

For (UIView *v in VS)

{

if ([[NSString Stringwithutf8string:object_getclassname (v)] isequaltostring:@ "Uikeyboard"])

{

V.backgroundcolor = [Uicolor Redcolor];

}

}

}

13. Set the time zone

Nstimezone *defaulttimezone = [Nstimezone defaulttimezone];

Nstimezone *tzgmt = [Nstimezone timezonewithname:@ "GMT"];

[Nstimezone SETDEFAULTTIMEZONE:TZGMT];

Use one of the above two time zones.

14. The ipad hides the keyboard while triggering the method.

[[Nsnotificationcenter Defaultcenter] Addobserver:self

Selector: @selector (keyboardwillhide:)

Name:uikeyboardwillhidenotification

Object:nil];

-(Ibaction) Keyboardwillhide: (nsnotification *) Note

14, the method of opening another program in one program.

Http://www.cocoachina.com/iphonedev/sdk/2010/0322/768.html

15. Calculate the number of words in a string

-(int) Calculatetextnumber: (NSString *) text

{

float number = 0.0;

int index = 0;

for (index; index < [text length]; index++)

{

NSString *prototext = [text substringtoindex:[text length]-index];

NSString *tochangetext = [text substringtoindex:[text length] -1-index];

NSString *charater;

if ([Tochangetext length]==0)

{

Charater = Prototext;

}

Else

{

Nsrange range = [text Rangeofstring:tochangetext];

Charater = [Prototext stringbyreplacingcharactersinrange:range withstring:@ "];

}

NSLog (Charater);

if ([charater lengthofbytesusingencoding:nsutf8stringencoding] = = 3)

{

number++;

}

Else

{

Number = number+0.5;

}

}

return ceil (number);

}

Some little tricks.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.