Ios 在swift中为所有出现的字符串着色
此代码Ios 在swift中为所有出现的字符串着色,ios,swift,nsstring,nsmutableattributedstring,Ios,Swift,Nsstring,Nsmutableattributedstring,此代码 var textSearch="hi" var textToShow="hi hihi hi" var rangeToColor = (textToShow as NSString).rangeOfString(textSearch) var attributedString = NSMutableAttributedString(string:textToShow) attributedString.addAttribute(NSForegroundColorAttributeNam
var textSearch="hi"
var textToShow="hi hihi hi"
var rangeToColor = (textToShow as NSString).rangeOfString(textSearch)
var attributedString = NSMutableAttributedString(string:textToShow)
attributedString.addAttribute(NSForegroundColorAttributeName, value: UIColor.yellowColor() , range: rangeToColor)
TextView.attributedText=attributedString
给我NSRange在TextView中给字符串上色。
问题是,我只返回第一个事件。
如果单词包含“hi hi hi hi”,则只有第一个“hi”是彩色的。
如何获取所有出现的字符串?Swift 5
let attrStr = NSMutableAttributedString(string: "hi hihi hey")
let inputLength = attrStr.string.count
let searchString = "hi"
let searchLength = searchString.characters.count
var range = NSRange(location: 0, length: attrStr.length)
while (range.location != NSNotFound) {
range = (attrStr.string as NSString).range(of: searchString, options: [], range: range)
if (range.location != NSNotFound) {
attrStr.addAttribute(NSAttributedString.Key.foregroundColor, value: UIColor.yellow, range: NSRange(location: range.location, length: searchLength))
range = NSRange(location: range.location + range.length, length: inputLength - (range.location + range.length))
}
}
斯威夫特3
let attrStr = NSMutableAttributedString(string: "hi hihi hey")
let inputLength = attrStr.string.characters.count
let searchString = "hi"
let searchLength = searchString.characters.count
var range = NSRange(location: 0, length: attrStr.length)
while (range.location != NSNotFound) {
range = (attrStr.string as NSString).range(of: searchString, options: [], range: range)
if (range.location != NSNotFound) {
attrStr.addAttribute(NSForegroundColorAttributeName, value: UIColor.yellow(), range: NSRange(location: range.location, length: searchLength))
range = NSRange(location: range.location + range.length, length: inputLength - (range.location + range.length))
}
}
斯威夫特2
let attrStr = NSMutableAttributedString(string: "hi hihi hey")
let inputLength = attrStr.string.characters.count
let searchString = "hi"
let searchLength = searchString.characters.count
var range = NSRange(location: 0, length: attrStr.length)
while (range.location != NSNotFound) {
range = (attrStr.string as NSString).rangeOfString(searchString, options: [], range: range)
if (range.location != NSNotFound) {
attrStr.addAttribute(NSForegroundColorAttributeName, value: UIColor.yellowColor(), range: NSRange(location: range.location, length: searchLength))
range = NSRange(location: range.location + range.length, length: inputLength - (range.location + range.length))
}
}
以上是凯文的答案 被称为:
attrStr.attributeRangeFor(searchString, attributeValue: UIColor.yellowColor(), atributeSearchType: .All)
Swift 2.0:
import UIKit
extension NSMutableAttributedString {
enum AtributeSearchType {
case First, All, Last
}
func attributeRangeFor(searchString: String, attributeValue: AnyObject, atributeSearchType: AtributeSearchType) {
let inputLength = self.string.characters.count
let searchLength = searchString.characters.count
var range = NSRange(location: 0, length: self.length)
var rangeCollection = [NSRange]()
while (range.location != NSNotFound) {
range = (self.string as NSString).rangeOfString(searchString, options: [], range: range)
if (range.location != NSNotFound) {
switch atributeSearchType {
case .First:
self.addAttribute(NSForegroundColorAttributeName, value: attributeValue, range: NSRange(location: range.location, length: searchLength))
return
case .All:
self.addAttribute(NSForegroundColorAttributeName, value: attributeValue, range: NSRange(location: range.location, length: searchLength))
break
case .Last:
rangeCollection.append(range)
break
}
range = NSRange(location: range.location + range.length, length: inputLength - (range.location + range.length))
}
}
switch atributeSearchType {
case .Last:
let indexOfLast = rangeCollection.count - 1
self.addAttribute(NSForegroundColorAttributeName, value: attributeValue, range: rangeCollection[indexOfLast])
break
default:
break
}
}
}
使用
NSRegularExpression
可以避免您自己对其执行所有范围计算。此示例还将突出显示两个单词,而不是一个单词
let text = "If you don't have a plan, you become part of somebody else's plan."
let toHighlight = ["plan", "you"]
let range = text.nsRange(from: text.startIndex ..< text.endIndex) // full text
let rangesToHighlight: [[NSRange]] = toHighlight.map { search in
do {
let regex = try NSRegularExpression(pattern: search, options: [])
let matches: [NSTextCheckingResult] = regex.matches(in: text, options: [], range: range)
return matches.map { $0.range } // get range from NSTextCheckingResult
} catch {
return [NSRange]()
}
}
let yellow = UIColor.yellow
let attributedText = NSMutableAttributedString(string: text)
let flattenedRanges: [NSRange] = rangesToHighlight.joined()
flattenedRanges.forEach { // apply color to all ranges
attributedText.addAttribute(NSForegroundColorAttributeName,
value: yellow,
range: $0)
}
let text=“如果你没有计划,你就成为别人计划的一部分。”
让toHighlight=[“计划”,“你”]
让range=text.nsRange(from:text.startIndex..
Swift 4:
let string = "foo fbar foofoo foofo"
let mutableAttributedString = NSMutableAttributedString(string: string)
let searchString = "foo"
var rangeToSearch = string.startIndex..<string.endIndex
while let matchingRange = string.range(of: searchString, options: [], range: rangeToSearch) {
mutableAttributedString.addAttribute(.foregroundColor, value: UIColor.yellow, range: NSRange(matchingRange, in: string))
rangeToSearch = matchingRange.upperBound..<string.endIndex
}
let string=“foo fbar foo foo”
让mutableAttributedString=NSMutableAttributedString(string:string)
让searchString=“foo”
var rangeToSearch=string.startIndex..我在swift 4.2中为它创建了一个扩展
extension NSMutableAttributedString {
// Adds attributes EVERY TIME the text to change appears
func addAttributes(_ attributes: [NSAttributedString.Key: NSObject], forText text: String) {
var range = NSRange(location: 0, length: self.length)
while (range.location != NSNotFound) {
range = (self.string as NSString).range(of: text, options: [], range: range)
if (range.location != NSNotFound) {
self.addAttributes(attributes, range: NSRange(location: range.location, length: text.count))
range = NSRange(location: range.location + range.length, length: self.string.count - (range.location + range.length))
}
}
}
现在你可以这样称呼它:
let attributedString = NSMutableAttributedString(attributedString: textView.attributedText)
let myAttributes = [/* your attributes here */]
attributedString.addAttributes(myAttributes, forText: /* your text here */)
我将这两种方法设置为仅为该文本的一次出现添加颜色或为该文本的所有出现添加颜色:
extension NSMutableAttributedString{
func setColorForText(_ textToFind: String, with color: UIColor) {
let range = self.mutableString.range(of: textToFind, options: .caseInsensitive)
if range.location != NSNotFound {
addAttribute(NSForegroundColorAttributeName, value: color, range: range)
}
}
func setColorForAllOccuranceOfText(_ textToFind: String, with color: UIColor) {
let inputLength = self.string.count
let searchLength = textToFind.count
var range = NSRange(location: 0, length: self.length)
while (range.location != NSNotFound) {
range = (self.string as NSString).range(of: textToFind, options: [], range: range)
if (range.location != NSNotFound) {
self.addAttribute(NSForegroundColorAttributeName, value: color, range: NSRange(location: range.location, length: searchLength))
range = NSRange(location: range.location + range.length, length: inputLength - (range.location + range.length))
}
}
}
}
您的解决方案在objective-C中:/Swift中写道,目的是在将来改进我的答案。有人能解释为什么我的答案被否决吗?使用扩展方法对我来说似乎更干净。谢谢!发现这对于我的用例来说是一种非常有用和可扩展的方式。也许字典可以用于多个属性?