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

标签 ios swift uitextview uiactivityindicatorview

我有一个 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/

相关文章:

iphone - 如何制作一个带有 UITextView 的 UITableViewCell,在 UITextView 的基础上动态调整其高度?

ios - 如何存储 Firebase 数据以便在 Swift 中轻松快速地访问

ios - UIAlertView 按钮的操作问题

ios - Swift:如何将短音播放得更长

swift - 在初始化时将信息传递给类

swift - 如何编写一个快速函数来计算没有条目的天数?

ios - 如何从 UITextView 中删除垂直线并在 DALinedTextView 中从左侧键入文本?

ios - SSL 操作失败并显示代码,同时向 iOS 设备发送推送通知

ios - 将 Bundle 中的文件显示到 UITableView - iOS

带有 UITextView 的 IOS 字体颜色