我在这里遇到了一个问题。我想addAttributes
到我从数组中收集的字符串。我该如何从不完整的功能中解决呢?
已编辑:代码可以正常工作,但是就像@Larme所说的那样,通过使用range
,属性文本将仅追加到找到的 first 范围内,而不是其余部分。
例如,如果文本为Desmond is careless, I am <b>desmond<b> with a <b>car<b>
,则最终结果将为 Desmond 为 car ,否则,我不知道如何找到范围索引的汽车重复添加?
func find(inText text: String, pattern: String) -> [String]?
{
let regex = try! NSRegularExpression(pattern:pattern+"(.*?)"+pattern, options: [])
var results = [String]()
regex.enumerateMatches(in: text, options: [], range: NSMakeRange(0, text.utf16.count))
{ result, flags, stop in
if let r = result?.range(at: 1),
let range = Range(r, in: text)
{
results.append(String(text[range]))
}
}
return results
}
let colorAttribute =
[NSAttributedStringKey.font: Fonts.OpenSansSemibold(isPhoneSE ? 13 :isPhone3 ? 12 : 16),
NSAttributedStringKey.foregroundColor: Colors.slate] as [NSAttributedStringKey : Any]
***EDITED***
let boldStrArray = find(inText: item.inbox_body, pattern: "<b>") // find the bold text
let replaceStr = item.inbox_body!.replacingOccurrences(of: "<b>", with: "")
print(boldStrArray) //["desmond", "car"]
print(replaceStr)
let attrStr = NSMutableAttributedString(string: replaceStr, attributes: normalAttribute)
for b in boldStrArray!
{
let range = (replaceStr as! NSMutableString).range(of: b)
print(range)
attrStr.setAttributes(colorAttribute, range: range)
//attrStr.addAttributes(colorAttribute, range: range)
}
任何评论都将不胜感激:)
答案 0 :(得分:1)
如果find(inText:pattern:)
返回(NS)Range
(或者更好的NSTextCheckingResult
s)会更容易。
因此,请进行一些修改:
func find(inText text: String, pattern: String) -> [NSTextCheckingResult] {
let regex = try! NSRegularExpression(pattern:pattern+"(.*?)"+pattern, options: []) //There should be a try/catch at least
return regex.matches(in: text, options: [], range: NSRange.init(location: 0, length: text.utf16.count))
}
我使用了“自己的字体和颜色”,因为我没有您的设置。
因为根据使用的模式不容易猜出可以使用range(at: 1)
,所以我不会使用方法,而是将其写入其中。
let initialString = "I am <b>desmond<b> with a <b>car<b>"
let colorAttribute: [NSAttributedStringKey : Any] = [.font: UIFont.boldSystemFont(ofSize: 12),
.foregroundColor: UIColor.red]
var attrStr = NSMutableAttributedString(string: initialString)
print("BEFORE attrStr: \(attrStr)")
let boldRanges = find(inText: initialString, pattern: "<b>")
boldRanges.reversed().forEach { (aResult) in
let subTextNSRange = aResult.range(at: 1)
guard let subTextRange = Range(subTextNSRange, in: attrStr.string) else { return } //This is to "switch" between NSRange & Range
let replacementString = String(attrStr.string[subTextRange])
let replacementAttributedString = NSAttributedString(string: replacementString, attributes: colorAttribute)
attrStr.replaceCharacters(in: aResult.range, with: replacementAttributedString)
}
print("AFTER attrStr: \(attrStr)")
输出:
$> BEFORE attrStr: I am <b>desmond<b> with a <b>car<b>{
}
$> AFTER attrStr: I am {
}desmond{
NSColor = "sRGB IEC61966-2.1 colorspace 1 0 0 1";
NSFont = "\".AppleSystemUIFontBold 12.00 pt. P [] (0x60c000051e50) fobj=0x101a36cf0, spc=3.09\"";
} with a {
}car{
NSColor = "sRGB IEC61966-2.1 colorspace 1 0 0 1";
NSFont = "\".AppleSystemUIFontBold 12.00 pt. P [] (0x60c000051e50) fobj=0x101a36cf0, spc=3.09\"";
}
答案 1 :(得分:0)
for b in boldStrArray!{
replaceStr = (replaceStr.replacingOccurrences(of: "<b>"+b+"<b>", with: b))
let str = NSMutableAttributedString(string: b, attributes: colorAttribute)
attrStr.append(str) }
答案 2 :(得分:0)
尝试一下:
let attrString = NSMutableAttributedString(string: "This is an attributed string")
let range = attrString.mutableString.range(of: "attributed")
attrString.setAttributes([:], range: range)