gpt4 book ai didi

ios - 如何向 UITextView 添加事件指示器?

转载 作者:行者123 更新时间:2023-11-28 11:11:14 25 4
gpt4 key购买 nike

我有一个 UITextView 可以从服务器获取和加载文件。如何添加事件指示器以显示它正在加载?托管它的服务器有点慢。

代码如下:

let url = NSURL(string: "http://www.aliectronics.com.au/thefournobletruths.rtf")
let data = NSData(contentsOfURL: url!)

do {
let options = [NSDocumentTypeDocumentAttribute : NSRTFTextDocumentType]
let attributedString = try NSAttributedString(data: data!, options: options, documentAttributes: nil)
print(attributedString)
textview2.attributedText = attributedString
textview2.editable = false
} catch {
NSLog("\(error)")

最佳答案

您不应该使用 URL 初始值设定项的数据内容来同步下载数据。不能保证它会成功。您需要使用 NSURLSession 方法 dataTaskWithURL 异步下载数据,正如我在上次 question 中向您建议的那样.关于你的新问题,已经有人回答了here .结合这两个答案,你应该能够完成你正在尝试的事情。更多信息在下面的代码中评论::

import UIKit
class ViewController: UIViewController {
@IBOutlet var textview2: UITextView!
// declare your activityIndicator
let activityIndicator = UIActivityIndicatorView(activityIndicatorStyle: .white)
// you will need a new view for your activity indicator and a label for your message
let messageFrame = UIView()
let strLabel = UILabel(frame: CGRect(x: 50, y: 0, width: 160, height: 50))
func displayActivityIndicator() {
strLabel.text = "Loading file"
strLabel.textColor = .white
messageFrame.layer.cornerRadius = 15
messageFrame.backgroundColor = UIColor(white: 0, alpha: 0.7)
activityIndicator.frame = CGRect(x: 0, y: 0, width: 50, height: 50)
activityIndicator.startAnimating()
messageFrame.addSubview(activityIndicator)
messageFrame.addSubview(strLabel)
view.addSubview(messageFrame)
}
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
// set your message frame (you can also position it using textview2.frame
messageFrame.frame = CGRect(x: view.frame.midX - 80, y: view.frame.midY - 25 , width: 160, height: 50)
// call your activity indicator method
displayActivityIndicator()
// call your method to download your data asynchronously
getRTFData(from: "http://thewalter.net/stef/software/rtfx/sample.rtf", textview2)
}
func getRTFData(from link: String,_ textView: UITextView) {
// make sure your link is valid NSURL using guard
guard let url = URL(string: link) else { return }
// creata a data task for your url
URLSession.shared.dataTask(with: url) {
(data, response, error) in
// use guard to make sure you get a valid response from the server and your data it is not nil and you got no errors otherwise return
guard
let httpURLResponse = response as? HTTPURLResponse, httpURLResponse.statusCode == 200,
let data = data, error == nil
else { return }
// you need to use dispatch async to update the UI
DispatchQueue.main.async {
// stop animating the activity indicator and remove the messageFrame from the view
self.activityIndicator.stopAnimating()
self.messageFrame.removeFromSuperview()

// NSAttributedString data initialiser throws an error so you need to implement Do Try Catch error handling
do {
textView.attributedText = try NSAttributedString(data: data, options: [.documentType: NSAttributedString.DocumentType.html, .characterEncoding: String.Encoding.utf8.rawValue], documentAttributes: nil)
textView.isEditable = false
} catch {
print(error)
}
}
}.resume()
}
override func viewDidLoad() {
super.viewDidLoad()
}
}

关于ios - 如何向 UITextView 添加事件指示器?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34938809/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com