如何突出显示UITextView';在swift中逐行显示文本?
我正试图逐行突出显示如何突出显示UITextView';在swift中逐行显示文本?,swift,loops,uitextview,Swift,Loops,Uitextview,我正试图逐行突出显示UITextView中的文本。我想迭代每一行并突出显示该行,以便用户看到,然后我想删除突出显示效果,为下一行做准备。我尝试过,但没有找到解决方案,这是我现在最好的机会 这是我迄今为止一直在做的一些工作,出于某种原因,它目前在UITextView中填充了“NSBackgroundColor 1101”,我不知道这是为什么 func highlight() { let str = "This is\n some placeholder\n text\nwith n
UITextView
中的文本。我想迭代每一行并突出显示该行,以便用户看到,然后我想删除突出显示效果,为下一行做准备。我尝试过,但没有找到解决方案,这是我现在最好的机会
这是我迄今为止一直在做的一些工作,出于某种原因,它目前在UITextView
中填充了“NSBackgroundColor 1101
”,我不知道这是为什么
func highlight() {
let str = "This is\n some placeholder\n text\nwith newlines."
var newStr = NSMutableAttributedString(string: "")
var arr:[String] = str.components(separatedBy: "\n")
var attArr:[NSMutableAttributedString] = []
for i in 0..<arr.count {
attArr.append(NSMutableAttributedString(string: arr[i]))
}
for j in 0..<attArr.count {
let range = NSMakeRange(0, attArr[j].length)
attArr[j].addAttribute(.backgroundColor, value: UIColor.yellow, range: range)
DispatchQueue.main.asyncAfter(deadline: .now() + 0.5){
for m in 0..<attArr.count {
newStr = NSMutableAttributedString(string: "\(attArr[m])\n")
self.textView.attributedText = newStr
}
}
attArr[j].removeAttribute(.backgroundColor, range: range)
//remove from texview here
}
}
func highlight(){
let str=“这是一个\n带有换行符的占位符\n文本。”
var newStr=nsmutableAttributeString(字符串:“”)
var arr:[String]=str.components(以“\n”分隔)
var attArr:[nsmutableAttributeString]=[]
对于0..中的i,您希望这样:
您需要文本视图的内容始终为完整字符串,并高亮显示一行,但您的代码将其设置为仅高亮显示一行。您的代码还计划所有高亮显示同时发生(.now()+0.5
),而不是在不同的时间发生
以下是我的建议:
创建一个范围数组,每行一个范围
使用该数组修改文本视图的textStorage
,方法是根据需要删除和添加.backgroundColor
属性以高亮显示和取消高亮显示行
高亮显示行n
时,安排行n+1
的高亮显示。这有两个优点:如果需要,可以更容易、更有效地提前取消动画;如果需要,可以更容易地使动画无休止地重复
我使用这个游乐场创建了上面的演示:
import UIKit
import PlaygroundSupport
let text = "This is\n some placeholder\n text\nwith newlines."
let textView = UITextView(frame: CGRect(x: 0, y:0, width: 200, height: 100))
textView.backgroundColor = .white
textView.text = text
let textStorage = textView.textStorage
// Use NSString here because textStorage expects the kind of ranges returned by NSString,
// not the kind of ranges returned by String.
let storageString = textStorage.string as NSString
var lineRanges = [NSRange]()
storageString.enumerateSubstrings(in: NSMakeRange(0, storageString.length), options: .byLines, using: { (_, lineRange, _, _) in
lineRanges.append(lineRange)
})
func setBackgroundColor(_ color: UIColor?, forLine line: Int) {
if let color = color {
textStorage.addAttribute(.backgroundColor, value: color, range: lineRanges[line])
} else {
textStorage.removeAttribute(.backgroundColor, range: lineRanges[line])
}
}
func scheduleHighlighting(ofLine line: Int) {
DispatchQueue.main.asyncAfter(deadline: .now() + .seconds(1)) {
if line > 0 { setBackgroundColor(nil, forLine: line - 1) }
guard line < lineRanges.count else { return }
setBackgroundColor(.yellow, forLine: line)
scheduleHighlighting(ofLine: line + 1)
}
}
scheduleHighlighting(ofLine: 0)
PlaygroundPage.current.liveView = textView
导入UIKit
导入PlaygroundSupport
let text=“这是\n某个占位符\n text\n和换行符。”
设textView=UITextView(帧:CGRect(x:0,y:0,宽度:200,高度:100))
textView.backgroundColor=.white
textView.text=文本
设textStorage=textView.textStorage
//在此处使用NSString,因为textStorage需要NSString返回的范围类型,
//不是字符串返回的范围类型。
将storageString=textStorage.string设为NSString
变量lineRanges=[NSRange]()
EnumerateSubstring(在:NSMakeRange(0,storageString.length)中),选项:。署名,在中使用:{({,lineRange,{,})
lineRanges.append(lineRange)
})
func setBackgroundColor(color:UIColor?,forLine:Int){
如果让颜色=颜色{
textStorage.addAttribute(.backgroundColor,值:颜色,范围:lineRanges[line])
}否则{
textStorage.removeAttribute(.backgroundColor,范围:lineRanges[line])
}
}
func scheduleHighlighting(行数:Int){
DispatchQueue.main.asyncAfter(截止日期:.now()+秒(1)){
如果行>0{setBackgroundColor(nil,forLine:line-1)}
警戒线
你可以参考哇,这真的很好,谢谢你帮我解决了一个巨大的头痛问题。我要试试这个。