iOS Code Tips

Source: Internet
Author: User

1. Get the screen image
-(UIImage *) Imagefromview: (UIView *) TheView
{

Uigraphicsbeginimagecontext (theView.frame.size);
Cgcontextref context = Uigraphicsgetcurrentcontext ();
[Theview.layer Renderincontext:context];
UIImage *theimage = Uigraphicsgetimagefromcurrentimagecontext ();
Uigraphicsendimagecontext ();

return theimage;
}

Dynamic size of 2.label

-(Cgsize) Labelautocalculaterectwith: (nsstring*) Text FontSize: (cgfloat) FontSize MaxSize: (cgsize) MaxSize

{
    
    nsmutableparagraphstyle* paragraphstyle = [[ Nsmutableparagraphstyle Alloc]init];
    
    paragraphstyle.linebreakmode=nslinebreakbywordwrapping;
    
    nsdictionary* attributes [email protected]{nsfontattributename:[ Uifont fontwithname:@ "Microsoftyahei" size:fontsize],nsparagraphstyleattributename:paragraphstyle.copy};
    
    cgsize labelsize = [Text boundingrectwithsize:maxsize options: nsstringdrawinguseslinefragmentorigin| Nsstringdrawingusesfontleading| Nsstringdrawingtruncateslastvisibleline attributes:attributes context:nil].size;
    
    Labelsize.height=ceil (labelsize.height);
    return labelsize;
    
}

3. Time stamp conversion to time

-(nsstring*) Timetrasformwithdate: (NSString *) datestring
{
NSDateFormatter *formatter = [[NSDateFormatter alloc]init];
[Formatter setdateformat:@ "Yy-mm-dd hh:mm"];
[Formatter Settimezone:[nstimezone timezonewithname:@ "asia/beijing"];

NSString *date = [formatter stringfromdate:[nsdate dateWithTimeIntervalSince1970:dateString.integerValue];
NSLog (@ "date1:%@", date);
return date;

}

4.RGB into color

+ (Uicolor *) Colorfromhexrgb: (NSString *) incolorstring
{
Uicolor *result = nil;
unsigned int colorcode = 0;
unsigned char redbyte, greenbyte, Bluebyte;

if (nil! = incolorstring)
{
Nsscanner *scanner = [Nsscanner scannerwithstring:incolorstring];
(void) [Scanner scanhexint:&colorcode]; Ignore error
}
Redbyte = (unsigned char) (ColorCode >> 16);
Greenbyte = (unsigned char) (ColorCode >> 8);
Bluebyte = (unsigned char) (ColorCode); Masks off High bits
result = [Uicolor
Colorwithred: (float) redbyte/0xff
Green: (float) greenbyte/0xff
Blue: (float) bluebyte/0xff
ALPHA:1.0];
return result;
}

5. Add border

Uirectcorner Corners=uirectcornertopleft | Uirectcornertopright;
Uibezierpath *maskpath = [Uibezierpath bezierPathWithRoundedRect:view.bounds
Byroundingcorners:corners
Cornerradii:cgsizemake (4, 0)];
Cashapelayer *masklayer = [Cashapelayer layer];
Masklayer.frame = View.bounds;
Masklayer.path = Maskpath.cgpath;
View.layer.mask = Masklayer;
6.//compressing pictures
+ (uiimage*) Imagewithimagesimple: (uiimage*) Image scaledtosize: (cgsize) newSize
{
Create a graphical context image
Uigraphicsbeginimagecontext (newSize);
Tell the old picture to draw in this new environment, the required
New size
[Image Drawinrect:cgrectmake (0,0,newsize.width,newsize.height)];
Get a new image of the context
uiimage* newimage = Uigraphicsgetimagefromcurrentimagecontext ();
End context
Uigraphicsendimagecontext ();
return newimage;
}

7.textfield of placeholder

[TEXTF setvalue:[uicolor Whitecolor] forkeypath:@ "_placeholderlabel.textcolor"];
[TEXTF Setvalue:[uifont boldsystemfontofsize:15] forkeypath:@ "_placeholderlabel.font"];
8.

Butleft. Imageedgeinsets = Uiedgeinsetsmake (7, 5, 7, 25);
Butleft.contenthorizontalalignment = Uicontrolcontenthorizontalalignmentcenter;
9.//call this method to change the size of the last 2 characters of a label
-(void) Label: (UILabel *) label Behindtextsize: (Nsinteger) integer
{
nsmutableattributedstring *mutastring = [[Nsmutableattributedstring alloc] initWithString:label.text];

[Mutastring addattribute:nsfontattributename Value:[uifont boldsystemfontofsize:16] Range:NSMakeRange ( Label.text.length-2, 2)];
Label.attributedtext = mutastring;
}

10.-(void) Changelabeltextcolor: (UILabel *) label
{
nsmutableattributedstring *mutastring = [[Nsmutableattributedstring alloc] initWithString:label.text];

[mutastring addattribute:nsforegroundcolorattributename value:[uicolor colorwithred:207/255.0 green:34/255.0 Blue : 42/255.0 alpha:1] Range:nsmakerange (0, 5)];
Label.attributedtext = mutastring;
}

11.

if ([TableView respondstoselector: @selector (setseparatorinset:)]) {

[TableView Setseparatorinset:uiedgeinsetszero];

}
if ([[Uidevice currentdevice].systemversion Floatvalue] >= 8.0) {
if ([TableView respondstoselector: @selector (setlayoutmargins:)]) {

[TableView Setlayoutmargins:uiedgeinsetszero];

}
}

Do any additional setup after loading the view.
}
-(void) TableView: (UITableView *) TableView Willdisplaycell: (UITableViewCell *) cell Forrowatindexpath: (Nsindexpath *) Indexpath

{

if ([Cell respondstoselector: @selector (setseparatorinset:)]) {

[Cell Setseparatorinset:uiedgeinsetszero];

}
if ([[Uidevice currentdevice].systemversion Floatvalue] >= 8.0) {
if ([Cell respondstoselector: @selector (setlayoutmargins:)]) {

[Cell Setlayoutmargins:uiedgeinsetszero];

}
}


}

iOS Code Tips

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.