For output in a database program, I have certain text that I've inserted marks to indicate bold or italics, as well as some text that is substituted for images. For instance:
"%Important% ^All employees to the breakroom^" should have final output as:
Important All employees to the breakroom
I have code written to find the text with "%" signs around it and "^" signs, but the trouble I have now is the text outputs like:
%Important% ^All employees to the breakroom^
I'd like to remove these % and ^'s while retaining the string's formatting.
This is the code I'm using up until it breaks:
func processText(inString string: String) -> NSAttributedString {
let pattern = ["(?<=\\^).*?(?=\\^)","(?<=\\%).*?(?=\\%)","\\^", "\\%"]
let italicsRegex = NSRegularExpression(pattern: pattern[0], options: .allZeros, error: nil)
let range = NSMakeRange(0, count(string))
let italicsMatches = italicsRegex?.matchesInString(string, options: .allZeros, range: range) as? [NSTextCheckingResult]
var attributedText = NSMutableAttributedString(string: string)
for match in italicsMatches! {
attributedText.addAttribute(NSFontAttributeName, value: UIFont(name: "Helvetica-Oblique", size: 14.0)!, range: match.range)
}
let boldRegex = NSRegularExpression(pattern: pattern[1], options: .allZeros, error: nil)
let boldMatches = boldRegex?.matchesInString(string, options: .allZeros, range: range) as? [NSTextCheckingResult]
for match in boldMatches! {
attributedText.addAttribute(NSFontAttributeName, value: UIFont(name: "Helvetica-Bold", size: 14.0)!, range: match.range)
}
let removeItalicsMarksRegex = NSRegularExpression(pattern: pattern[2], options: .allZeros, error: nil)
let removeItalicsMarksMatches = removeItalicsMarksRegex?.matchesInString(string, options: .allZeros, range: range) as? [NSTextCheckingResult]
var numberOfLoops = 0
for match in removeItalicsMarksMatches! {
attributedText.replaceCharactersInRange(match.range, withString: "")
}
return attributedText.copy() as! NSAttributedString
}
This works for the % match (but only the first character) and causes a crash on the ^ character immediately.
Any help or advice with resolving this would be appreciated. Thanks.
Martin,
I ended up using something very similar, but I decided to change the regular expression to include the ^ marks. In doing so, I was able to then clip the first and last characters of the included attributed substring with the "replaceCharactersInRange" method. This works a little better for my purposes so far because it's working from the attributed string so it doesn't screw up or remove any of its attributes.
I've attached the regex and the portion of the code that deals with italics for anyone's future reference (and thanks, again!):
func processText(inString string: String) -> NSAttributedString {
let pattern = ["\\^.*?\\^"] //Presented as an array here because in the full code there are a lot of patterns that are run.
let italicsRegex = NSRegularExpression(pattern: pattern[0], options: .allZeros, error: nil)
//In addition to building the match for this first regular expression, I also gather build the regular expressions and gather matches for all other matching patterns on the initial string ("string") before I start doing any processing.
let range = NSMakeRange(0, count(string.utf16))
let italicsMatches = italicsRegex?.matchesInString(string, options: .allZeros, range: range) as? [NSTextCheckingResult]
var attributedText = NSMutableAttributedString(string: string)
var charactersRemovedFromString = 0
for match in italicsMatches! {
let newRange = NSMakeRange(match.range.location - charactersRemovedFromString, match.range.length) // Take the updated range for when this loop iterates, otherwise this crashes.
attributedText.addAttribute(NSFontAttributeName, value: UIFont(name: "Helvetica-Oblique", size: 12.0)!, range: newRange)
let rangeOfFirstCharacter = NSMakeRange(match.range.location - charactersRemovedFromString, 1)
attributedText.replaceCharactersInRange(rangeOfFirstCharacter, withString: "")
charactersRemovedFromString += 2
let rangeOfLastCharacter = NSMakeRange(match.range.location + match.range.length - charactersRemovedFromString, 1)
attributedText.replaceCharactersInRange(rangeOfLastCharacter, withString: "")
}
return attributedText
}
Here is a possible solution, essentially a translation of
how to catch multiple instances special indicated **characters** in an NSString and bold them in between?
from Objective-C to Swift.
The idea is to add the attributes and remove the delimiters in one loop. The shift
variable is needed to adjust the matching ranges after the first delimiters have been removed.
For the sake of simplicity, only the "^...^" processing is shown.
func processText(inString string: String) -> NSAttributedString {
let pattern = "(\\^)(.*?)(\\^)"
let regex = NSRegularExpression(pattern: pattern, options: nil, error: nil)!
var shift = 0 // number of characters removed so far
let attributedText = NSMutableAttributedString(string: string)
regex.enumerateMatchesInString(string, options: nil, range: NSMakeRange(0, count(string.utf16))) {
(result, _, _) -> Void in
var r1 = result.rangeAtIndex(1) // Location of the leading delimiter
var r2 = result.rangeAtIndex(2) // Location of the string between the delimiters
var r3 = result.rangeAtIndex(3) // Location of the trailing delimiter
// Adjust locations according to the string modifications:
r1.location -= shift
r2.location -= shift
r3.location -= shift
// Set attribute for string between delimiters:
attributedText.addAttribute(NSFontAttributeName, value: UIFont(name: "Helvetica-Oblique", size: 14.0)!, range: r2)
// Remove leading and trailing delimiters:
attributedText.mutableString.deleteCharactersInRange(r3)
attributedText.mutableString.deleteCharactersInRange(r1)
// Update offset:
shift += r1.length + r3.length
}
return attributedText.copy() as! NSAttributedString
}
Note that enumerateMatchesInString() takes an NSRange, therefore you have to compute
the number of UTF-16 characters and not the number of Swift characters.
Example:
let text = "🇩🇪😀aaa ^🇭🇰😁bbb^ 🇳🇱😆eee"
let attrText = processText(inString: text)
println(attrText)
Output:
🇩🇪😀aaa {
}🇭🇰😁bbb{
NSFont = " font-family: \"Helvetica-Oblique\"; font-weight: normal; font-style: italic; font-size: 14.00pt";
} 🇳🇱😆eee{
}
That worked for me!
extension UILabel {
func updateAttributedText(_ text: String) {
if let attributedText = attributedText {
let mutableAttributedText = NSMutableAttributedString(attributedString: attributedText)
mutableAttributedText.mutableString.setString(text)
self.attributedText = mutableAttributedText
}
}
}
Related
I am using the following code to get a String substring from an NSRange:
func substring(with nsrange: NSRange) -> String? {
guard let range = Range.init(nsrange)
else { return nil }
let start = UTF16Index(range.lowerBound)
let end = UTF16Index(range.upperBound)
return String(utf16[start..<end])
}
(via: https://mjtsai.com/blog/2016/12/19/nsregularexpression-and-swift/)
When I compile with Swift 4 (Xcode 9b4), I get the following errors for the two lines that declare start and end:
'init' is unavailable
'init' was obsoleted in Swift 4.0
I am confused, since I am not using an init.
How can I fix this?
Use Range(_, in:) to convert an NSRange to a Range in Swift 4.
extension String {
func substring(with nsrange: NSRange) -> Substring? {
guard let range = Range(nsrange, in: self) else { return nil }
return self[range]
}
}
With Swift 4 we can get substrings this way.
Substring from index
let originStr = "Test"
let offset = 1
let str = String(originStr.suffix(from: String.Index.init(encodedOffset: offset)))
Substring to index
let originStr = "Test"
let offset = 1
String(self.prefix(index))
many examples in SO are fixing both sides, the leading and trailing. My request is only about the trailing.
My input text is: " keep my left side "
Desired output: " keep my left side"
Of course this command will remove both ends:
let cleansed = messageText.trimmingCharacters(in: .whitespacesAndNewlines)
Which won't work for me.
How can I do it?
A quite simple solution is regular expression, the pattern is one or more(+) whitespace characters(\s) at the end of the string($)
let string = " keep my left side "
let cleansed = string.replacingOccurrences(of: "\\s+$",
with: "",
options: .regularExpression)
You can use the rangeOfCharacter function on string with a characterSet. This extension then uses recursion of there are multiple spaces to trim. This will be efficient if you only usually have a small number of spaces.
extension String {
func trailingTrim(_ characterSet : CharacterSet) -> String {
if let range = rangeOfCharacter(from: characterSet, options: [.anchored, .backwards]) {
return self.substring(to: range.lowerBound).trailingTrim(characterSet)
}
return self
}
}
"1234 ".trailingTrim(.whitespaces)
returns
"1234"
Building on vadian's answer I found for Swift 3 at the time of writing that I had to include a range parameter. So:
func trailingTrim(with string : String) -> String {
let start = string.startIndex
let end = string.endIndex
let range: Range<String.Index> = Range<String.Index>(start: start, end: end)
let cleansed:String = string.stringByReplacingOccurrencesOfString("\\s+$",
withString: "",
options: .RegularExpressionSearch,
range: range)
return cleansed
}
Simple. No regular expressions needed.
extension String {
func trimRight() -> String {
let c = reversed().drop(while: { $0.isWhitespace }).reversed()
return String(c)
}
}
This question already has an answer here:
how to find the index of a character in a string from specific position
(1 answer)
Closed 6 years ago.
How can I find the first position of a character in a substring. Not in the string overall, but the first after a specified character position.
Example:
var str = "This is a test string"
//find the position of first "i" after "is"
let position = str.firstPositionOfIAfterPosition(5) // returns 18
I know I can find the overall first position with code below. How can I extend this to start looking only after a specified character position?
let position = str.rangeOfString("i").startIndex
var s = "This is a test string"
var targetRange = s.characters.indices
targetRange.startIndex = targetRange.startIndex.advancedBy(6) // skip past
let r = s.rangeOfString("i", options: [], range: targetRange, locale: nil)
// 18..<19
var str = "This is a test string"
func getIndexAfterString(string: String) -> Int {
let firstIndex = str.rangeOfString(string)?.startIndex.advancedBy(string.characters.count)
let i: Int = str.startIndex.distanceTo(firstIndex!)
let secondIndex = str.substringFromIndex(firstIndex!).rangeOfString("i")?.startIndex
let j: Int = str.startIndex.distanceTo(secondIndex!)
return i + j
}
let index: Int = getIndexAfterString(" is ") //18
Similar to matt's answer, but as String extension and with error handling
extension String {
func firstPositionOf(string: String, afterPosition index: Int) -> String.Index?
{
if self.isEmpty || self.characters.count - 1 < index { return nil }
let subRange = Range<String.Index>(self.startIndex.advancedBy(index + 1)..<self.endIndex)
guard let foundRange = self.rangeOfString(string, options: [], range: subRange) else { return nil }
return foundRange.startIndex
}
}
let str = "This is a test string"
let position = str.firstPositionOf("i", afterPosition:5) // -> 18
I am trying to add attributes to some ranges in Swift String.
I found ranges of first and last symbol in substring and color the text between them (including) in red.
let mutableString = NSMutableAttributedString(string: text)
let str = mutableString.string
//Red symbols
var t = 0
let symbols = mutableString.string.characters.count
while t < symbols {
if str[t] == "[" {
let startIndex = t
while str[t] != "]" {
t += 1
}
t += 1
let endIndex = t
mutableString.addAttribute(
NSForegroundColorAttributeName,
value: UIColor.redColor(),
range: NSMakeRange(startIndex, endIndex - startIndex))
}
t += 1
}
But I found that ranges in String and in NSMutableAttributedString are not equal. Range in String is shorter (this text is not in Unicode encoding).
Is there a some way to find ranges not in underlying String but in NSAttributedString to find it correctly?
Example:
print(mutableString.length) //550
print(mutableString.string.characters.count) //548
Why is this difference?
Yes it is possible to find ranges in NSMutableAttributedString.
Example :
let text = "[I love Ukraine!]"
var start = text.rangeOfString("[")
var finish = text.rangeOfString("]")
let mutableString = NSMutableAttributedString(string: text)
let startIndex = mutableString.string.rangeOfString("[")
let finishIndex = mutableString.string.rangeOfString("]")
Example output from playground:
Distinguish between String and NSString, even though they are bridged to one another. String is native Swift, and you define a range in terms of String character index. NSString is Cocoa (Foundation), and you define a range in terms of NSRange.
Yes, I found it.
Windows end-of-line "\r\n" is two symbols in NSAttributedString but only one character in Swift String.
I use checking in my code:
let symbols = mutableString.string.characters.count
var extendedSymbols = 0
while t < symbols {
if str[t] == "\r\n" { extendedSymbols += 1 }
else if str[t] == "[" {
let startIndex = t + extendedSymbols
while str[t] != "]" {
t += 1
}
t += 1
let endIndex = t + extendedSymbols
mutableString.addAttribute(
NSForegroundColorAttributeName,
value: UIColor.redColor(),
range: NSMakeRange(startIndex, endIndex - startIndex))
}
t += 1
}
Thank you all for help!!!
I have been looking around for some time now and am wondering how to detect multiple locations of a substring within a larger string. For example:
let text = "Hello, playground. Hello, playground"
let range: Range<String.Index> = text.rangeOfString("playground")! //outputs 7...<17
let index: Int = text.startIndex.distanceTo(range.startIndex) //outputs 7, the location of the first "playground"
I use this code to detect the first location that the substring appears within a string, but its it possible to detect the location of the second occurrence of "playground" and then the third and so on and so forth?
This should get you an array of NSRanges with "playground"
let regex = try! NSRegularExpression(pattern: "playground", options: [.caseInsensitive])
let items = regex.matches(in: text, options: [], range: NSRange(location: 0, length: (text as NSString).length))
let ranges: [NSRange] = items.map{$0.range}
Then to get the strings:
let occurrences: [String] = ranges.map{String((text as NSString).substring(with:$0))}
Swift 4
let regex = try! NSRegularExpression(pattern: fullNameArr[1], options: [.caseInsensitive])
let items = regex.matches(in: str, options: [], range: NSRange(location: 0, length: str.count))
let ranges: [NSRange] = items.map{$0.range}
let occurrences: [String] = ranges.map{String((str as NSString).substring(with: $0))}