iOS development-Simple skin-changing implementation and tool-type extraction

Source: Internet
Author: User

According to the pictures provided by the artist, there are two kinds of skin-changing schemes.

<1> artwork provides a picture of type one:

<2> Art provides the type of picture two: This kind of folder file name is the same situation, dragged into the project after the last use of the Real folder (blue folder). Because the yellow folder in the project does not exist, files with the same name will only retain one

Tick the second one:

Two. Extraction of tools--based on the scheme of the second picture, the extracted tool class

  • Implementation of the header file
  • Implementation of the. m file
    1 #import "SkinTool.h"2 3 /** Current skin color*/4 StaticNSString *_currentskin;5 6 @implementationSkintool7 //when initializing a tool class, give the _currentskin an initial value of8+ (void) Initialize9 {Ten [Super Initialize]; One     //no value in preferences, skin default to a color A_currentskin = [[Nsuserdefaults standarduserdefaults] Objectforkey:@"Currentskin"]; -     if(_currentskin = =Nil) { -_currentskin =@"Blue"; the     } - } -  -+ (void) Setcurrentskincolor: (NSString *) Skin + { -_currentskin =Skin; +     //Store your current skin color in your preferences A[[Nsuserdefaults Standarduserdefaults] Setobject:skin forkey:@"Currentskin"]; at } -  -+ (UIImage *) Skintoolwithimagename: (NSString *) ImageName - { -     //stitching the current skin color picture in the Mainbundle position -NSString *imagepath = [NSString stringWithFormat:@"skin/%@/%@", _currentskin,imagename]; inUIImage *image =[UIImage Imagenamed:imagepath]; -     returnimage; to } +  -+ (Uicolor *) Skintoolwithlabelcolor the { *     //The relative position of stitching plist files in Mainbundle $NSString *plistpath = [NSString stringWithFormat:@"skin/%@/bgcolor.plist", _currentskin];Panax NotoginsengNSString *path =[[NSBundle Mainbundle] Pathforresource:plistpath Oftype:nil]; -     //load the plist, get the dictionary that holds the RGB theNsdictionary *dict =[Nsdictionary Dictionarywithcontentsoffile:path]; +NSString *rgbstring = dict[@"Labelbgcolor"]; A     //split RGB String theNsarray *rgbarr = [rgbstring componentsseparatedbystring:@","]; +Nsinteger red = [rgbarr[0] IntegerValue]; -Nsinteger green = [rgbarr[1] IntegerValue]; $Nsinteger blue = [rgbarr[2] IntegerValue]; $     //return Color -     return[Uicolor colorwithred:red/255.0Green:green/255.0Blue:blue/255.0Alpha1.0]; - } the @end

  • Note: The control text color or label background color, is defined in each color folder plist file, the file specifies the RGB
  • When the outside calls the tool class, the error is prone (the project appears in the Tabbar controller case)-because the Tabbar controller's view is lazy loading, if the set skin class code is written in Viewdidload, the default will only be executed once;!!!!!! Solution: Write code in Viewwillappear

The plot of the error appears:

iOS development-Simple skin-changing implementation and tool-type extraction

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.