UITextView在Swift中没有显示attributionString

时间:2017-07-18 00:31:52

标签: swift string swift3 uitextview nsmutableattributedstring

当我运行下面的代码时,在控制台中显示了attributionText,但是没有显示在UITextView中。我将attributionText保存在全局变量scrollViewText中。我已经尝试打印scrollViewText,但它仍然显示为控制台中的空白区域。

public static func getHonorsAdvisorsText(){

    Alamofire.request("https://honors.purdue.edu/json/honors-advisors.json").responseJSON { response in
        //check if result has value
        if let value = response.result.value {
            let json = JSON(value)
            let freshmenName = json["first_year_advisors"][0]["name"].string
            let freshmenAssignment = json["first_year_advisors"][0]["assignment"].string
            let freshmenEmail = json["first_year_advisors"][0]["email"].string
            let formattedString = NSMutableAttributedString()
            formattedString
                .bold("Freshmen Advisor", fontSize: 40)
                .bold("\n" + "Name: " , fontSize: 20)
                .normal(freshmenName!, fontSize: 20)
                .bold("\n" + "Assignment: " , fontSize: 20)
                .normal(freshmenAssignment!, fontSize: 20)
                .bold("\n" + "Email: " , fontSize: 20)
                .normal(freshmenEmail!, fontSize: 20)
            scrollViewText = formattedString
            print(formattedString)
        }
    }
}

我使用代码

调用ThirdViewController中的getHonorsAdvisorsText函数
case 1:
AcademicsPage.getHonorsAdvisorsText()
textViewer.attributedText = scrollViewText

1 个答案:

答案 0 :(得分:1)

当然,你永远不会得到回复。您正在运行这两个电话:

AcademicsPage.getHonorsAdvisorsText()
textViewer.attributedText = scrollViewText

然而,他们一个接一个地跑,但" getHonorsAdvisorsText"是异步的。 (意思是它没有立即完成)这意味着当设置belongsText时,值仍然是空的

解决此问题的最简单方法是删除该行,而是将getHonorsAdvisorsText函数更改为:

public static func getHonorsAdvisorsText(){

    Alamofire.request("https://honors.purdue.edu/json/honors-advisors.json").responseJSON { response in
        //check if result has value
        if let value = response.result.value {
            let json = JSON(value)
            let freshmenName = json["first_year_advisors"][0]["name"].string
            let freshmenAssignment = json["first_year_advisors"][0]["assignment"].string
            let freshmenEmail = json["first_year_advisors"][0]["email"].string
            let formattedString = NSMutableAttributedString()
            formattedString
                .bold("Freshmen Advisor", fontSize: 40)
                .bold("\n" + "Name: " , fontSize: 20)
                .normal(freshmenName!, fontSize: 20)
                .bold("\n" + "Assignment: " , fontSize: 20)
                .normal(freshmenAssignment!, fontSize: 20)
                .bold("\n" + "Email: " , fontSize: 20)
                .normal(freshmenEmail!, fontSize: 20)
            scrollViewText = formattedString

            // SET THE TEXT HERE

            DispatchQueue.main.async {
               textViewer.attributedText = scrollViewText
            }

            print(formattedString)
        }
    }
}

注意文本是如何在" main"中设置的。调度队列。这是因为必须在主队列上完成所有接口更改。

使用完成处理程序:

public static func getHonorsAdvisorsText(completionHandler : @escaping ((_ formattedString : NSMutableAttributedString) -> Void)){

    Alamofire.request("https://honors.purdue.edu/json/honors-advisors.json").responseJSON { response in
        //check if result has value
        if let value = response.result.value {
            let json = JSON(value)
            let freshmenName = json["first_year_advisors"][0]["name"].string
            let freshmenAssignment = json["first_year_advisors"][0]["assignment"].string
            let freshmenEmail = json["first_year_advisors"][0]["email"].string
            let formattedString = NSMutableAttributedString()
            formattedString
                .bold("Freshmen Advisor", fontSize: 40)
                .bold("\n" + "Name: " , fontSize: 20)
                .normal(freshmenName!, fontSize: 20)
                .bold("\n" + "Assignment: " , fontSize: 20)
                .normal(freshmenAssignment!, fontSize: 20)
                .bold("\n" + "Email: " , fontSize: 20)
                .normal(freshmenEmail!, fontSize: 20)
            scrollViewText = formattedString

            // SET THE TEXT HERE

            DispatchQueue.main.async {
               completionHandler(formattedString)
            }

            print(formattedString)
        }
    }
}

你这样称呼它:

AcademicsPage.getHonorsAdvisorsText { (formattedString) in
    textViewer.attributedText = formattedString
}