更改属性字符串的文本并在Swift中保留属性 [英] Change text of an attributed string and retain attributes in Swift
问题描述
对于数据库程序中的输出,我有一些文本,我已经插入一些标记以指示粗体或斜体,以及一些替代图像的文本.例如:
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:
重要 所有进入休息室的员工
我编写了代码来查找带有%"符号和"^"符号的文本,但是现在遇到的麻烦是文本输出,如:
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:
%重要% ^ 所有进入休息室的员工 ^
我想在保留字符串格式的同时删除这些%和^.
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.
推荐答案
马丁,
我最终使用了非常相似的东西,但是我决定更改正则表达式以包含^标记.这样,我便可以使用"replaceCharactersInRange"方法剪辑所包含的属性子字符串的第一个和最后一个字符.到目前为止,这对于我的目的来说要好一些,因为它是在属性字符串中工作的,因此不会弄乱或删除其任何属性.
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
}
这篇关于更改属性字符串的文本并在Swift中保留属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!