Convenient Development Methods: xcode debugging, uilabel, and View Controls

Source: Internet
Author: User
Tags border color

There are always some convenient development methods or general methods that you can't remember. You always go through the previous projects and make a backup. (Supplemented slowly)

1. xcode debugging skills-set a breakpoint when an exception is thrown by the System
Sometimes our program doesn't know where it is to run and crash is hard to reproduce.
A conservative approach is to set a breakpoint before the system throws an exception. Specifically, it is to set a breakpoint at the objc_exception_throw.
Step 1: Press cmd + 6 in xcode to enter the breakpoint Management window;
Click + in the lower-right corner to add a new symbolic breakpoint.
Enter objc_exception_throw in the symbol column and Click Done.
In this way, in debug mode, if the program is about to throw an exception, the exception will be interrupted.
For example, in the previous code, I asked [firstobjctcrashtest]; To throw an exception. After the breakpoint is set at objc_exception_throw, the program will be interrupted at this code, so that we can know where the code is going wrong.

2. Calculate the height of uilabel after multiple lines with the font
+ (Cgfloat) calctextheight :( INT) textwidth text :( nsstring *) text font :( INT) fontsize {
Cgrect bounds, result;
Bounds = cgrectmake (0, 0, textidth, 300 );

Uilabel * label = [[uilabel alloc] initwithframe: cgrectzero];
Label. font = [uifont systemfontofsize: fontsize];
Label. Text = text;
Result = [label textrectforbounds: bounds limitedtonumberoflines: 20];
Return result. Size. height;
}

Or cgsize requiredsize = [introducelabel. Text sizewithfont: [uifont systemfontofsize: 14] constrainedtosize: cgsizemake (296.0f, flt_max) linebreakmode: Empty];

3. Calculate the length of the current label with the font increase (single row)
Cgsize boundingsize = cgsizemake (320366f, cgfloat_max );
Cgsize requiredsize = [status. User. Username sizewithfont: [uifont boldsystemfontofsize: 13] constrainedtosize: boundingsize linebreakmode: uilinebreakmodewordwrap];
Cgfloat requiredwidth = requiredsize. width;

4. Add a border to the View Control
Profileimagebutton = [uibutton buttonwithtype: uibuttontypecustom];
[Profileimagebutton. layer setmaskstobounds: Yes];
[Profileimagebutton. layer setcornerradius: 4.0]; // you can specify the radius of the four rounded corners of a rectangle.
[Profileimagebutton. layer setborderwidth: 1.0]; // Border Width
Cgcolorspaceref colorspace = cgcolorspacecreatedevicergb ();
Cgcolorref colorref = cgcolorcreate (colorspace, (cgfloat []) {225.0/255.0, 225.0/255.0, 225.0/255.0, 1.0 });
[Profileimagebutton. layer setbordercolor: colorref]; // border color

Set rounded corners separately
[Iconimage. layer setcornerradius: 4.0];
[Iconimage setclipstobounds: Yes];

5. the time zone returns a number in the format of-12-+ 12)
-(Nsstring *) gettimezone {
Nsstring * zone = [[nstimezone systemtimezone] description]; // Europe/Berlin/America/new_york // Asia/Harbin
// These three can be used to test Exp: nsstring * zone = [[nstimezone timezonewithname: @ "America/new_york"] description];
Nsstring * time = [[zone componentsseparatedbystring: @ "offset"] objectatindex: 1];
Int inv = [time intvalue];
Int result = inv/(60*60 );
If (result> 0 ){
Return [nsstring stringwithformat: @ "+ % d", result];
}
Return [nsstring stringwithformat: @ "% d", result];
}

6. Check whether the input box is not blank or empty.
Nsstring * _ textfield = [textfield. Text stringbytrimmingcharactersinset: [nscharacterset whitespaceandnewlinecharacterset];
If ([_ textfield length] = 0 ){
Uialertview * alertview = [[uialertview alloc] initwithtitle: @ "" message: @ "comment content cannot be blank! "Delegate: Self cancelbuttontitle: @" OK "otherbuttontitles: Nil];
[Alertview show];
Return no;
}

7. Set the uitextfield location based on the current keyboard height.
-(Void) textfielddidbeginediting :( uitextfield *) textfield
{
[[Nsicationcenter center defacenter center] addobserver: Self selector: @ selector (keyboardwillshow :) name: uikeyboarddidshownotification object: Nil];
}

-(Void) keyboardwillshow :( ID) sender {
Cgrect keyboardframe;
[[(Nsnotification *) sender) userinfo] objectforkey: uikeyboardframeenduserinfokey] getvalue: & keyboardframe];
Cgfloat keyboardheight = cgrectgetheight (keyboardframe );
[Self. textimageview setframe: cgrectmake (0, 416-keyboardheight, 320, 45)];
}
8. Set label, imageview, and click on the current event.
Uitapgesturerecognizer * imagetap = [[uitapgesturerecognizer alloc] initwithtarget: Self action: @ selector (tapimage :)];
// [Imageview setuserinteractionenabled: Yes]; set to editable if you cannot click
[Imageview addgesturerecognizer: imagetap];

9. Determine the remaining words (two letters and numbers are counted as one Chinese character)
-(INT) charnumber :( nsstring *) strtemp
{
Int strlength = 1;
Char * P = (char *) [strtemp cstringusingencoding: nsunicodestringencoding];
For (INT I = 0; I <[strtemp lengthofbytesusingencoding: nsunicodestringencoding]; I ++)
{
If (* P ){
P ++;
Strlength ++;
}
Else {
P ++;
}
}
Return strlength/2;
}

10. Loading GIF images using uiwebview -- this can solve the problem that GIF images cannot be downloaded to a local file, and the interface is not smooth when sdwebimage is down.
Uiwebview * webview = [[uiwebview alloc] initwithframe: cgrectmake (0, 0, [hometablecell getimagewidth: funnypic], [hometablecell getimageheight: funnypic])];
Webview. backgroundcolor = [uicolor clearcolor];
// Webview. scalespagetofit = No
// Disable the slide of webview. This is mainly because the system earlier than 5.0 cannot directly obtain webview. scrollview.
[(Uiscrollview *) [[webview subviews] objectatindex: 0] setbounces: No];
// Do not allow white edges. This margin is required.
Nsstring * html = [nsstring stringwithformat: @ "<HTML> <body style = \" margin: 0px; \ "> </body> [Webview loadhtmlstring: HTML baseurl: Nil];
[Imageview addsubview: webview];

11. insert and load more tableview reloaddata flashes
If (requestarray & [requestarray count]> 0 ){
Int cellcount = [requestarray count]; // obtain a total of several rows
Nsmutablearray * indexarray = [[nsmutablearray alloc] initwithcapacity: 10];
Int numcountnow = [self. tableview numberofrowsinsection: 0];
For (; cellcount> 0; cellcount --){
Nsindexpath * Path = [nsindexpath indexpathforrow: numcountnow + cellcount-1 insection: 0];
[Indexarray addobject: path];
}
[Self. tableview insertrowsatindexpaths: indexarray withrowanimation: uitableviewrowanimationnone];
}

12. Upload a hex image (PNG images cannot be loaded directly when being compiled into freamwork, which must be converted)
First, use ultraedit to convert the image to hexadecimal
Static const char _ playerpause_icon [] = {
0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a, 0x00, 0x00, 0x00, 0x0d, 0x49, 0x48, 0x44, 0x52,
0x00, 0x00, 0x00, 0x11, 0x00, 0x00, 0x00, 0x1b, 0x08, 0x06, 0x00, 0x00, 0x00, 0x9a, 0xf6, 0x64,
0x9c, 0x00, 0x00, 0x00, 0x39, 0x49, 0x44, 0x41, 0x54, 0x38, 0x8d, 0x63, 0x64, 0x60, 0x60, 0x38,
0xc3, 0x80, 0x1d, 0x98, 0x60, 0x11, 0xc3, 0xaa, 0x96, 0xf1, 0xff, 0xff, 0xff, 0xff, 0xb1, 0x4a,
0x30, 0x32, 0x32, 0xa2, 0x8b, 0xe1, 0x52, 0xcb, 0x84, 0xc3, 0x15, 0x24, 0x81, 0x51, 0x43, 0x46,
0x0d, 0x19, 0x35, 0x64, 0xd4, 0x90, 0x51, 0x43, 0x46, 0x0d, 0xa1, 0xa7, 0x21, 0x00, 0xdd, 0x84,
0x09, 0xfd, 0x6b, 0x3c, 0x1f, 0xcb, 0x00, 0x00, 0x00, 0x00, 0x49, 0x45, 0x4e, 0x44, 0xae, 0x42,
0x60, 0x82
};
Then load
[Playbutton setimage: [uiimage imagewithdata: [nsdata datawithbytes: _ playerpause_icon length: sizeof (_ playerpause_icon)/sizeof (char)] forstate: uicontrolstatenormal];

13. Countdown (remaining time)
-(Void) timerfiremethod :( nstimer *) thetimer {

Id OBJ = [thetimer userinfo];
Nsdateformatter * F1 = [[nsdateformatter alloc] init];
[F1 setdateformat: @ "yyyy-mm-dd hh: mm: Ss. s"];

Nsdate * theday = [F1 datefromstring :( nsstring *) OBJ];
[F1 release];

Nscalendar * Cal = [nscalendar currentcalendar]; // defines a nscalendar object.

Nsdate * today = [nsdate date]; // obtain the current time

// Get the specific time difference
Unsigned int unitflags = nsyearcalendarunit | nsmonthcalendarunit | nsdaycalendarunit | nshourcalendarunit | nsminutecalendarunit | nssecondcalendarunit;
Nsdatecomponents * D = [Cal components: unitflags fromdate: Today todate: theday options: 0];

Nsstring * countdown = [nsstring stringwithformat: @ "% d: % d seconds", [d month], [D Day], [d hour], [d minute], [d Second];

Self. timelabel. Text = Countdown;
Return;

}

14. Nine cells or other types of coordinates
Frame. Size. width = 60; // you can specify the coordinates and size of a button.
Frame. Size. Height = 60;
Frame. Origin. x = (I % 3) * (60 + 32) + 40;
Frame. Origin. Y = floor (I/3) * (60 + 24) + 60;

 

Community post: http://www.cocoachina.com/bbs/read.php? Tid = 122380 & fpage = 19

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.