简体   繁体   English

如何检测用户是否在 UITextField 中输入了省略号?

[英]How to detect if a user typed an ellipsis in a UITextField?

How can you check if you string has an ellipsis in swift?如何检查字符串在 swift 中是否有省略号? I have noticed some odd behavior when using the swift string functions and learned that ellipsis are to blame.在使用 swift 字符串函数时,我注意到一些奇怪的行为,并了解到省略号是罪魁祸首。 When a user enters ... as part of the string in a UITextField and then I try to locate a string after that the character count is always off by 2. This is because the string functions are treating ... as 3 characters when locating the index of the string I am searching for, but the character count functions are treating it as 1 character.当用户在 UITextField 中输入...作为字符串的一部分时,然后我尝试定位一个字符串,之后字符数总是关闭 2。这是因为字符串函数在定位时将...视为 3 个字符我正在搜索的字符串的索引,但字符计数函数将其视为 1 个字符。 The solution is pretty simple... when I have an elliptical in the string then adjust the "found" index of the string by 2. The issue is I don't know how to search "does this string have an ellipsis" because this didn't find it:解决方案非常简单......当我在字符串中有一个椭圆时,然后将字符串的“找到”索引调整 2。问题是我不知道如何搜索“这个字符串是否有省略号”,因为这个没找到:

if heading.contains("...") {
            print ("found the ...!")
        }

I suspect there is a special way to search for an ellipsis but haven't been able to find out what it is.我怀疑有一种特殊的方法可以搜索省略号,但无法找出它是什么。 This is my "find the last space after substringing out the first 30 characters" function that works for strings that don't have an ellipsis:这是我的“找到前 30 个字符后的最后一个空格” function 适用于没有省略号的字符串:

func breakOutHeadingFromString(fullString: String, charBreakPoint: Int) -> (heading: String, remainingText: String, breakChar: String) {
var heading = fullString
var remainingText = ""
var breakChar = ""

// If the Full string has less characters than break point then just return full blurb as heading and blank out 2
if fullString.count > charBreakPoint {
    // Get N characters out of total char count (hardcoded to 30 can this be dynamic?)
    var prefix = String(fullString.prefix(charBreakPoint))

    // Find the last space in heading so you can continue message there
    let lastSpace = prefix.lastIndex(of: " ") ?? prefix.endIndex
    var breakPoint = lastSpace
    breakChar = "|"

    // If there is a /n clip there
    if let newLine = prefix.firstIndex(of: "\n") {
        prefix = String(prefix[..<newLine])
        breakPoint = newLine
        breakChar = "\n"
    }
    // Use the Break Point to split the message in 2
    let breakPointInt: Int = fullString.distance(from: fullString.startIndex, to: breakPoint)
    // if the string has a eliptical ... then the break point is off by 2 because it 1 char in but 3 in
    heading = String(fullString.prefix(breakPointInt))
    remainingText = String(fullString.suffix(fullString.count - breakPointInt - 1))
}
return (heading,remainingText,breakChar)

} }

The ellipsis is 1 unicode character, not 3 so it is counted as 1 character and below is what I think is happening in your situation.省略号是 1 unicode 字符,而不是 3,所以它被算作 1 个字符,以下是我认为在您的情况下发生的情况。

This did not find it这个没找到

if heading.contains("...") {
            print ("found the ...!")
        }

Because these are 3 periods (3 characters) and different from the ellipsis character (1 character)因为这些是 3 个句点(3 个字符)并且与省略号字符(1 个字符)不同

Try highlighting with a mouse what you are comparing (...) with the actual ellipsis character (…)尝试用鼠标突出显示您正在比较的内容 (...) 与实际的省略号字符 (...)

In the first instance, you can highlight each of the dots individually using your mouse and in the second scenario you will not be able to select each individual dot.在第一种情况下,您可以使用鼠标单独突出显示每个点,在第二种情况下,您将无法 select 每个单独的点。

Here is some test:这是一些测试:

var ellipsisString = "This is … a"
var threeDotString = "This is ... a"

print("Ellipsis character count: \(ellipsisString.count)")
print("Three dot character count: \(threeDotString.count)")

// The output:
Ellipsis character count: 11
Three dot character count: 13

As you can see, with the proper ellipsis character, it counts it as only 1 character如您所见,使用正确的省略号字符,它仅计为 1 个字符

Now using your contains function with the ellipsis string:现在使用您的contains function 和省略号字符串:

var ellipsisString = "This is … a"
print(ellipsisString.contains("…"))
print(ellipsisString.contains("..."))

// The output
true
false

You can see contains("…") succeeds with the real ellipsis character but contains("...") fails with the three dots you used.您可以看到 contains("...") 使用真正的省略号字符成功,但 contains("...") 使用您使用的三个点失败。

Finally, let's say I wanted to add the string nice after the ellipsis character in the ellipsis string This is … a - your strategy will not work of adding 2 to the index if a proper ellipsis character was used最后,假设我想在省略号字符串中的省略号字符之后添加字符串nice This is … a - 如果使用了正确的省略号字符,您的策略将无法在索引中添加 2

Here is what I do to achieve this:这是我为实现这一目标所做的工作:

var ellipsisString = "This is … a"

// Find the index of the ellipsis character
if let ellipsisIndex = ellipsisString.firstIndex(of: "…")
{
// Convert the String index to a numeric value
let numericIndex: Int
= ellipsisString.distance(from: ellipsisString.startIndex,
                          to: ellipsisIndex)

// Ellipsis located at the 8th index which is right
print(numericIndex)

// Set the index where we want to add the new text
// The index after the ellipsis character so offset by 1
let insertIndex = ellipsisString.index(ellipsisIndex,
                           offsetBy: 1)

// Insert the text " nice" into the string after the
// ellipsis character
ellipsisString.insert(contentsOf: " nice",
          at: insertIndex)
}

// Print the output after the addition
print(ellipsisString)

// Output
This is … nice a

This gives the desired output you wish which is finding the position of the ellipsis character accurately and then using that position to do what you want, in my case, adding a text after the ellipsis character.这给出了您希望的所需 output,即准确地找到省略号字符的 position,然后使用该 position 来执行您想要的操作,在我的情况下,添加文本

Hope this clears some things up for you希望这可以为您解决一些问题

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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