使用如多看阅读等能即时打开文本文件,而我开发的惠文阅读打开速度就很慢,特别是对大文本文件如天龙八部,要两三分钟才能打开,这是为什么,多看阅读他们采用了什么技术,我应该如何改进,请各位指点,谢谢!读取文件代码如下:
var titleString:NSString="连城诀"
do{
//var titleString:NSString?=try NSString(contentsOfFile: location, encoding: String.Encoding.utf8.rawValue)
titleString=try NSString(contentsOfFile: DataViewController.location+DataViewController.shuming, encoding: String.Encoding.utf8.rawValue) }catch let error {
print(error.localizedDescription)
print("文件读取失败")
}
let layoutManager = NSLayoutManager()
let cunchu:NSTextStorage=NSTextStorage(string:titleString as String);
cunchu.addLayoutManager(layoutManager)
cunchu.addAttribute(NSAttributedString.Key.font, value: UIFont.systemFont(ofSize: DataViewController.zitidaxiao), range: NSMakeRange(0, titleString.length))
let style=NSMutableParagraphStyle()
style.lineSpacing=DataViewController.hangjudaxiao
cunchu.addAttribute(NSAttributedString.Key.paragraphStyle, value: style, range: NSMakeRange(0, titleString.length))
let pageSize = CGSize(width: 384, height: 785)
var ranges: [NSRange] = []
i=0
//var totalpages:Int=1000
while true {
let container=NSTextContainer(size: pageSize)
layoutManager.addTextContainer(container)
let rang=layoutManager.glyphRange(for: container)
if rang.length <= 0 {
DataViewController.totalpages=i
break
}
ranges.append(rang)
i += 1
}
var k:Int=0
while (k<DataViewController.totalpages){
let cunchufenduan=cunchu.attributedSubstring(from: ranges[k])
DataViewController.stringfenduan.append(cunchufenduan.string)
k += 1
}