One-click modification of color in xib/Storyboard project

Keywords: Mobile github git xcode

git address: https://github.com/winterwd/OneKeyChangeXIBColor   


It can be downloaded directly (note that there is no Chinese in the path). If you are interested, read the article.

2018.9.2

This color-changing gadget was originally a problem that I met in my own work, so I made a gadget like this. At that time, it was limited to satisfying my own needs. But recently, a brief friend left a message with some bug s and put forward some suggestions for improvement (thank you for the fireworks on the lighthouse). It was originally intended to repair some of these problems as soon as possible, so as to delay the cancer patients. To this day, I am really ashamed!....

The optimization is as follows:

- > 1, the matching color value algorithm is optimized.
- > 2, optimize the color data model ColorValue;
-> 3. The optimization results indicate that red is used in case of failure and green is used in case of modification.
- > 4, after modification, restore the initial state to save resources and improve performance;

If this tool is helpful to you, give it a star t - >. star

2016.12.22

Recently, I am a little idle, so I updated the previous gadget. For such a long time, I also experienced an update and upgrade of Xcode 8. It is estimated that this gadget is useless. After opening the project cmd+R, I found that it is really useless. I simply upgraded the gadget once.

Some improvements in use:

1. I have made a very useful interface, you can operate on the interface color is worth operating;
2. You can click on the file path to select the path of the file you need to modify.

simple1.png

simple2.png

simple3.png

Code optimization:

1. First of all, I will briefly talk about the principle:

Record the color model before and after color, and file Path, respectively.
- > Convert the user input color value into the type needed, that is, the color value - > ColorValue model;
- > traverse the filePath path, so there are. xib and. storyboard files, and save the file path;
-> DOM parsing operations are performed on each. xib and. storyboard file to find all color element labels NSXMLElement;
- > Find the node.name, over, corresponding to NSXMLNode in the color element tag.

2. Some core codes:

  • Record color, this is a delegate method of textField, file path selection, using this NSOpenPanel;

    // NSTextDelegate Gets Input Content
    - (void)controlTextDidChange:(NSNotification *)notification;
    
    //  Get File Selection Path
    - (IBAction)choseFilePath:(NSButton *)sender {
        NSOpenPanel *openPanel = [NSOpenPanel openPanel];
        [openPanel setCanChooseFiles:YES];
        [openPanel setCanChooseDirectories:YES];
    
        NSWindow *window = [[NSApplication sharedApplication] keyWindow];
        [openPanel beginSheetModalForWindow:window completionHandler:^(NSModalResponse returnCode) {
            if (returnCode == 1) {
                NSURL *fileUrl = [[openPanel URLs] objectAtIndex:0];
                NSString *filePath = [[fileUrl.absoluteString componentsSeparatedByString:@"file://"] lastObject];
                NSLog(@"fileContext = %@",filePath);
                self.sourcePathTextField.stringValue = filePath;
                self.filePath = filePath;
            }
        }];
    }
    
    
  • Color Value, a color data model;

    // The transformed RGB color value is stored directly, and _redValue is CGFloat type, which is used as matching reference value, where the last 8 decimal bits are intercepted as the color value to be modified.
    - (void)setRedValue:(CGFloat)red {
        CGFloat tempValue = red / 255.0;
        _redValue = tempValue;
        self.red = [NSString stringWithFormat:@"%.8f",tempValue];
    }
    
  • Color matching algorithm;

    // Compared with the previous fault-tolerant algorithm, this method is more concise. Here, the similar precision value is set to 0.0001 temporarily.
    - (BOOL)isEqual:(WDColorModel *)object {
    
        BOOL redEqual = fabs(self.redValue - object.redValue) < 0.0001;
        BOOL blueEqual = fabs(self.blueValue - object.blueValue) < 0.0001;
        BOOL greenEqual = fabs(self.greenValue - object.greenValue) < 0.0001;
        
        return redEqual && blueEqual && greenEqual;
    }
    
  • Search for. xib and. storyboard files, which have nothing to say, is the operation of files, retrieving file suffixes;

  • Get the color element (NSXMLElement) and modify it.

  // Getting XMLDocument
  - (NSXMLDocument *)parsedDataFromData:(NSData *)data colorModel:(WDColorModel *)objColorModel {
      NSError *error = nil;
      NSXMLDocument *document = [[NSXMLDocument alloc] initWithData:data options:NSXMLNodePreserveWhitespace error:&error];
      NSXMLElement *rootElement = document.rootElement;
      [self parsedXMLElement:rootElement objColorModel:objColorModel];
  
      if (error) {
          NSLog(@"error = %@",error);
      }
      return document;
}

  // Modifying elements
  - (void)parsedXMLElement:(NSXMLElement *)element objColorModel:(WDColorModel *)objColorModel {
      for (NSXMLElement *subElement in element.children) {
          if ([subElement.name isEqualToString:@"color"]) {
            WDColorModel *obj = [WDColorModel colorModelWithArray:subElement.attributes];
              if ([obj isEqual:self.targetColorModel]) {
                  [self updateXMLNodelWithNode:subElement color:objColorModel];
              }
          }
          [self parsedXMLElement:subElement objColorModel:objColorModel];
      }
}

  // Update NSXMLElement
  - (void)updateXMLNodelWithNode:(NSXMLElement *)subElement color:(WDColorModel *)obj {
       NSArray *array = subElement.attributes;
       for (NSXMLNode *node in array) {   
          if ([node.name isEqualToString:@"red"]) {
              [node setStringValue:obj.red];
          }
          else if ([node.name isEqualToString:@"green"]) {
              [node setStringValue:obj.green];
          }
          else if ([node.name isEqualToString:@"blue"]) {
              [node setStringValue:obj.blue];
          }
       }
  }

See the details Code If you think it's good, start with it.

End

 

Posted by blinks on Fri, 25 Jan 2019 11:03:14 -0800