2014-08-27 78 views
5

我想通過搜索來突出顯示所有匹配的詞。我寫了代碼,但我不能使用循環。當我搜索一個單詞時,我的應用程序找到單詞並僅突出顯示第一個單詞。這裏是我的代碼UITextView使用swift突出顯示所有匹配

var count = 0 
let attributedText = NSMutableAttributedString(attributedString: txtMetin2.attributedText) 
let text2 = txtArama.text as NSString 
let text = txtMetin2.text as NSString 
var range:NSRange 
var checker:NSString = "" 

for(var i=0 ; i<text.length - text2.length-1 ; i++) 
{   
    range = NSMakeRange(i, text2.length) 
    checker = text.substringWithRange(range) 
    if(text2 == checker) 
    { 
     count++  
     let highlightedRange = text.rangeOfString("\(text2)") 
     attributedText.addAttribute(NSBackgroundColorAttributeName, value: UIColor.blueColor(), range: highlightedRange) 
     let textAttachment = NSTextAttachment() 
     let textAttachmentString = NSAttributedString(attachment: textAttachment) 
     attributedText.appendAttributedString(textAttachmentString) 
     txtMetin2.attributedText = attributedText        
    } 
} 
println("\(count)") 

我在swift很新。抱歉代碼不正確。我的代碼找到匹配數,但我如何突出顯示所有匹配謝謝

回答

15

強制性NSRegularExpression基於溶液。

let searchString = "this" 
let baseString = "This is some string that contains the word \"this\" more than once. This substring has multiple cases. ThisthisThIs." 

let attributed = NSMutableAttributedString(string: baseString) 

var error: NSError? 
let regex = NSRegularExpression(pattern: searchString, options: .CaseInsensitive, error: &error) 

if let regexError = error { 
    println("Oh no! \(regexError)") 
} else { 
    for match in regex?.matchesInString(baseString, options: NSMatchingOptions.allZeros, range: NSRange(location: 0, length: baseString.utf16Count)) as [NSTextCheckingResult] { 
     attributed.addAttribute(NSBackgroundColorAttributeName, value: UIColor.yellowColor(), range: match.range) 
    } 

    textView.attributedText = attributed 
} 
+1

非常感謝你:) – ardacankaya 2014-08-27 12:43:16

+0

當我搜索符號像點和問號時,它不起作用。「 '?' – 2016-04-19 14:36:52

+0

@ArunKumarP它是一個正則表達式,因此您需要在搜索字符串中將它們轉義。例如'stuff \\?'或'stuff \\。'。 – 2016-04-19 15:29:24

0

解決。

這是新的:

var count = 0 
let attributedText = NSMutableAttributedString(attributedString: txtMetin2.attributedText) 
let text2 = txtArama.text as NSString 
let text = txtMetin2.text as NSString 
println("\(text.length)") 
println("\(text2.length)") 
var range:NSRange 
var checker:NSString = "" 

for(var i=0 ; i <= text.length - text2.length ; i++) 
{ 
    range = NSMakeRange(i, text2.length) 
    checker = text.substringWithRange(range) 
    if(text2 == checker) 
    { 
     attributedText.addAttribute(NSBackgroundColorAttributeName, value: UIColor.yellowColor(), 
      range: range) 
     let textAttachment = NSTextAttachment() 
     let textAttachmentString = NSAttributedString(attachment: textAttachment) 
     attributedText.appendAttributedString(textAttachmentString) 
     txtMetin2.attributedText = attributedText 
     count++ 
    } 
} 
println("\(count)") 

我使用範圍變量,而不是突出顯示的範圍中attributedText.addAtrribute()

1

UITextView的高亮顯示所有匹配字SWIFT 3.0

let searchString = "Lorem Ipsum" 
let baseString = "Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard .Containing LOREM IPSUM passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem IPSUM" 



let attributed = NSMutableAttributedString(string: baseString) 
    do 
    { 
     let regex = try! NSRegularExpression(pattern: searchString,options: .caseInsensitive) 
     for match in regex.matches(in: baseString, options: NSRegularExpression.MatchingOptions(), range: NSRange(location: 0, length: baseString.characters.count)) as [NSTextCheckingResult] { 
      attributed.addAttribute(NSBackgroundColorAttributeName, value: UIColor.yellow, range: match.range) 
     } 
     self.txtView.attributedText = attributed 
    } 

click here to see image

+0

請添加描述您的代碼的意圖是什麼。它有助於更​​好地瞭解樣本。 – 2016-10-11 20:18:34

+0

如果我想突出顯示baseString中的更多單詞,該怎麼辦? – 2016-10-25 10:10:12

2

您可以使用下面的函數傳遞搜索輸入和當前內容。這將返回NSAttributedString?,你可以在你的TextView

斯威夫特3

func generateAttributedString(with searchTerm: String, targetString: String) -> NSAttributedString? { 
    let attributedString = NSMutableAttributedString(string: targetString) 
    do { 
     let regex = try NSRegularExpression(pattern: searchTerm, options: .caseInsensitive) 
     let range = NSRange(location: 0, length: targetString.utf16.count) 
     for match in regex.matches(in: targetString, options: .withTransparentBounds, range: range) { 
      attributedString.addAttribute(NSFontAttributeName, value: UIFont.systemFont(ofSize: 16, weight: UIFontWeightBold), range: match.range) 
     } 
     return attributedString 
    } catch _ { 
     NSLog("Error creating regular expresion") 
     return nil 
    } 
} 

編輯設置:

亮點具有變音符不敏感的選項:

func generateAttributedString(with searchTerm: String, targetString: String) -> NSAttributedString? { 

    let attributedString = NSMutableAttributedString(string: targetString) 
    do { 
     let regex = try NSRegularExpression(pattern: searchTerm.trimmingCharacters(in: .whitespacesAndNewlines).folding(options: .diacriticInsensitive, locale: .current), options: .caseInsensitive) 
     let range = NSRange(location: 0, length: targetString.utf16.count) 
     for match in regex.matches(in: targetString.folding(options: .diacriticInsensitive, locale: .current), options: .withTransparentBounds, range: range) { 
      attributedString.addAttribute(NSFontAttributeName, value: UIFont.systemFont(ofSize: 16, weight: UIFontWeightBold), range: match.range) 
     } 
     return attributedString 
    } catch { 
     NSLog("Error creating regular expresion: \(error)") 
     return nil 
    } 
} 
相關問題