简体   繁体   中英

How to properly rename a Color Set together with all its references in iOS?

Previously one of the Color Sets was named 'GreenTextColor', and now as part of the design change the text color is no longer green... so I wanted to name it to become something more generic 'PrimaryTextColor'. However I couldn't figure out a way to Refactor > Rename a Color Set so that all places that uses this color set gets renamed automatically...

I tried double clicking the Color Set and rename it that way, however upon checking all the xib files that uses that Color Set now became 'GreenTextColor (missing)' and doesn't automatically change to the new Color Set.

What I'm currently doing is to find and replace all text occurrences in *.xib files which is quite painful. It works for now but there must be a better way?

but there must be a better way

No, there isn't. Xcode has no automatic facility for this. You should be able to find all occurrences of "GreenTextColor" everywhere in your project, including xib files, using any decent text editor with global search facilities, such as BBEdit, but performing the find and replace as text, as you are already doing, is completely up to you.

It is egregious that Xcode's find/replace does not properly support Interface Builder files. Sometimes you can find but not replace (fonts). Other times you can't even find (color sets).

So far I've always had to solve this by using a combination of find/grep and sed, modifying the interface builder files directly. This can be risky. Make sure you've committed changes and generally trust yourself.

To start off only reviewing the changes to be made without any file modifications, I've used the following command in the root of my project directory:

find . -type f \( -name '*.xib' -or -name '*.storyboard' \) \
  -exec echo {} \; \
  -exec sed -E -n 's/([cC]olor.*)name="oldColorName"/\1name="newColorName"/p' {} \;

This prints each file found along with any changes that would be made to the file according to the sed pattern. The pattern is complex to guarantee matches related to color usage. If your color name is unique enough that you're not concerned with false positives, you can remove it and simply pattern match the name itself: 's/oldColorName/newColorName/p' .

When ready to apply the changes, execute the sed command in-place on the files with:

find . -type f \( -name '*.xib' -or -name '*.storyboard' \) \
  -exec sed -E -i '' 's/([cC]olor.*)name="oldColorName"/\1name="newColorName"/' {} \;

Note that printing was deliberately removed here. Don't attempt to keep -n and /p in your command. That's not how sed works . You can review the changes in your diff tool of choice at this point anyway. Discard them if it didn't do what you expected.

Also, you can perform this change even when the new color set has different color values , which is handy when updating a color in addition to renaming it. When you next open the Interface Builder file in Xcode, the color value will automatically update in the XML to match the value corresponding to the changed name.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM