![](/img/trans.png)
[英]How to access character typed by user before showing it in UITextField
[英]How to detect if a user typed an ellipsis in a UITextField?
如何檢查字符串在 swift 中是否有省略號? 在使用 swift 字符串函數時,我注意到一些奇怪的行為,並了解到省略號是罪魁禍首。 當用戶在 UITextField 中輸入...
作為字符串的一部分時,然后我嘗試定位一個字符串,之后字符數總是關閉 2。這是因為字符串函數在定位時將...
視為 3 個字符我正在搜索的字符串的索引,但字符計數函數將其視為 1 個字符。 解決方案非常簡單......當我在字符串中有一個橢圓時,然后將字符串的“找到”索引調整 2。問題是我不知道如何搜索“這個字符串是否有省略號”,因為這個沒找到:
if heading.contains("...") {
print ("found the ...!")
}
我懷疑有一種特殊的方法可以搜索省略號,但無法找出它是什么。 這是我的“找到前 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)
}
省略號是 1 unicode 字符,而不是 3,所以它被算作 1 個字符,以下是我認為在您的情況下發生的情況。
這個沒找到
if heading.contains("...") {
print ("found the ...!")
}
因為這些是 3 個句點(3 個字符)並且與省略號字符(1 個字符)不同
嘗試用鼠標突出顯示您正在比較的內容 (...) 與實際的省略號字符 (...)
在第一種情況下,您可以使用鼠標單獨突出顯示每個點,在第二種情況下,您將無法 select 每個單獨的點。
這是一些測試:
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
如您所見,使用正確的省略號字符,它僅計為 1 個字符
現在使用您的contains
function 和省略號字符串:
var ellipsisString = "This is … a"
print(ellipsisString.contains("…"))
print(ellipsisString.contains("..."))
// The output
true
false
您可以看到 contains("...") 使用真正的省略號字符成功,但 contains("...") 使用您使用的三個點失敗。
最后,假設我想在省略號字符串中的省略號字符之后添加字符串nice
This is … a
- 如果使用了正確的省略號字符,您的策略將無法在索引中添加 2
這是我為實現這一目標所做的工作:
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
這給出了您希望的所需 output,即准確地找到省略號字符的 position,然后使用該 position 來執行您想要的操作,在我的情況下,添加文本
希望這可以為您解決一些問題
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.