簡體   English   中英

如何在“故事板”文件中找到所有“自定義”按鈕

[英]How to find all the "custom" buttons in your "storyboard" files

我想找到自定義類型的 storyboard 文件中定義的所有按鈕。 storyboard 中的自定義按鈕在“<button”行中沒有屬性“buttonType”。

要查找所有按鈕,我使用正則表達式:'<button',它會產生所有類型的按鈕。

什么是正則表達式來僅查找那些自定義按鈕(沒有屬性'buttonType =“system')?

自定義和系統按鈕的 storyboard 代碼片段

                    <button opaque="NO" contentMode="scaleToFill" fixedFrame="YES" contentHorizontalAlignment="center" contentVerticalAlignment="center" lineBreakMode="middleTruncation" translatesAutoresizingMaskIntoConstraints="NO" id="aYC-hu-2aT">
                        <rect key="frame" x="170" y="116" width="75" height="35"/>
                        <autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMaxY="YES"/>
                        <inset key="imageEdgeInsets" minX="0.0" minY="0.0" maxX="2.2250738585072014e-308" maxY="0.0"/>
                        <state key="normal" title="Button"/>
                    </button>
                    <button opaque="NO" contentMode="scaleToFill" fixedFrame="YES" contentHorizontalAlignment="center" contentVerticalAlignment="center" buttonType="system" lineBreakMode="middleTruncation" translatesAutoresizingMaskIntoConstraints="NO" id="9L1-Cv-wFM">
                        <rect key="frame" x="146" y="278" width="75" height="35"/>
                        <autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMaxY="YES"/>
                        <inset key="imageEdgeInsets" minX="0.0" minY="0.0" maxX="2.2250738585072014e-308" maxY="0.0"/>
                        <state key="normal" title="Button"/>
                    </button>

正則表達式

<button(?![^>]+\s*buttonType\s*=\s*"system"[^>]*)[^>]*>

描述

# Options: Case insensitive; Exact spacing; Dot doesn’t match line breaks; ^$ match at line breaks; Greedy quantifiers; Regex syntax only
# 
# Match the character string “<button” literally (case insensitive) «<button»
# Assert that it is impossible to match the regex below starting at this position (negative lookahead) «(?![^>]+\s*buttonType\s*=\s*"system"[^>]*)»
#    Match any character that is NOT a “>” «[^>]+»
#       Between one and unlimited times, as many times as possible, giving back as needed (greedy) «+»
#    Match a single character that is a “whitespace character” (any Unicode separator, tab, line feed, carriage return, vertical tab, form feed, next line) «\s*»
#       Between zero and unlimited times, as many times as possible, giving back as needed (greedy) «*»
#    Match the character string “buttonType” literally (case insensitive) «buttonType»
#    Match a single character that is a “whitespace character” (any Unicode separator, tab, line feed, carriage return, vertical tab, form feed, next line) «\s*»
#       Between zero and unlimited times, as many times as possible, giving back as needed (greedy) «*»
#    Match the character “=” literally «=»
#    Match a single character that is a “whitespace character” (any Unicode separator, tab, line feed, carriage return, vertical tab, form feed, next line) «\s*»
#       Between zero and unlimited times, as many times as possible, giving back as needed (greedy) «*»
#    Match the character string “"system"” literally (case insensitive) «"system"»
#    Match any character that is NOT a “>” «[^>]*»
#       Between zero and unlimited times, as many times as possible, giving back as needed (greedy) «*»
# Match any character that is NOT a “>” «[^>]*»
#    Between zero and unlimited times, as many times as possible, giving back as needed (greedy) «*»
# Match the character “>” literally «>»

請注意,這假定按鈕的屬性字符串中沒有使用>字符。

注意buttonType=后面可以跟:

"system"
"detailDisclosure"
"infoLight"
"infoDark"
"contactAdd"
"close"

所以你會想要找到不包含"buttonType="的行

從命令行使用grep ,您可以執行以下操作:

grep -n '<button ' *.storyboard | grep -v 'buttonType='

-n表示顯示行號。

下一部分說 find <button (包括尾隨空格)。

然后我們將 pipe 傳遞給另一個 grep 命令,使用-v獲取不出現buttonType=的行。

例如,我在當前項目的 2 個故事板中都有一個自定義按鈕,以及其他buttonType按鈕。 我得到的結果是:

AnimCV.storyboard:454:                            <button opaque="NO" contentMode="scaleToFill" contentHorizontalAlignment="center" contentVerticalAlignment="center" lineBreakMode="middleTruncation" translatesAutoresizingMaskIntoConstraints="NO" id="Xfg-cw-DfY">
RegEx.storyboard:25:                            <button opaque="NO" contentMode="scaleToFill" fixedFrame="YES" contentHorizontalAlignment="center" contentVerticalAlignment="center" lineBreakMode="middleTruncation" translatesAutoresizingMaskIntoConstraints="NO" id="vPz-eC-017">

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM