如何在iPhone开发中将度数符号添加为UILabel的一部分?

AltBrian

我已将度数符号添加到我的UILabel中,但是当应用程序运行时,它消失了。我正在创建一个天气应用程序,因此正确设置其格式非常重要。这是ViewController加载后的代码。

 override func viewDidLoad() {
    super.viewDidLoad()
    cityLabel.text = city
    userEnteredNewCityName(city: city)
    // Do any additional setup after loading the view.
}

这是更新ViewController的代码

func updateWeatherData(json: JSON) {
    if let tempResult = json["main"]["temp"].double {
        weatherDataModel.temperature = Int(tempResult - 273)
        weatherDataModel.city = json["name"].stringValue
        weatherDataModel.condition = json["weather"][0]["id"].intValue
        weatherDataModel.weatherIconName = weatherDataModel.updateWeathericon(condition: weatherDataModel.condition)

        updateUIWithWeatherData()
    } else {
        cityLabel.text = "No data available"
    }

}

func userEnteredNewCityName(city: String){
    let params : [String : String] = ["q" : city, "appid" : APP_ID]
    getWeatherData(url: WEATHER_URL, parameters: params)
}

 //Write the updateUIWithWeatherData method here:


func updateUIWithWeatherData() {
    cityLabel.text = weatherDataModel.city
    temperatureLabel.text = String(weatherDataModel.temperature)
    weatherIcon.image = UIImage(named: weatherDataModel.weatherIconName)
}

任何帮助,将不胜感激。在此处输入图片说明

苏珊

理想情况下,使用MeasurementFormatter

let measurement = Measurement(value: weatherDataModel.temperature, unit: UnitTemperature.celsius)

let measurementFormatter = MeasurementFormatter()
measurementFormatter.unitStyle = .short
measurementFormatter.numberFormatter.maximumFractionDigits = 0
measurementFormatter.unitOptions = .temperatureWithoutUnit

temperatureLabel.text = measurementFormatter.string(from: measurement)   

无需转换为Int格式化程序将处理十进制数字的显示。

作为奖励,您不必自己进行开尔文到摄氏度的转换:

let measurementInKelvin = Measurement(value: weatherDataModel.temperature, unit: UnitTemperature.kelvin)
let measurement = measurementInKelvin.converted(to: .celsius)

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

如何在SwiftUI中将.fontWeight添加为ViewModifer的一部分

如何在flutter中将徽章计数数字添加为BottomNavigationBarItem的一部分

如何将Shell脚本添加为Java Project的一部分

如何在UILabel中的一部分NSAttributedString中添加函数调用(而非超链接)?

如何在Swift 4中将GMSMapView添加到视图的一部分

如何在Paw中将文件作为多部分请求的一部分上载?

如何通过PHP在JSON添加@符号作为对象名的一部分

如何将文本文件的内容添加为ELF文件中的一部分?

如何在表格的一部分中添加百分比

如何在python中将数组的一部分存储在不同的数组中

如何在iOS中将字符串的一部分加粗?

如何在PowerShell中将参数作为-file的一部分传递

如何在Golang中将jsonb编码为http响应的一部分

如何在Jgit中将文件列表作为提交的一部分

如何在SwiftUI中将View()作为列表的一部分传递?

如何在PHP中将“&”作为字符串的一部分传递?

如何在MarkLogic中将子元素包含为单词查询的一部分

如何在SQL中将NULL连接为字符串的一部分

如何在python中将api响应的一部分转换为完整的json

如何在python中将字符串用作变量的一部分

如何在 Python Pandas 中将列的一部分转换为日期?

如何在React中为正文文本的一部分添加样式

将轻击手势添加到UILabel的一部分

iOS:将IBAction添加到UILabel的一部分

UiSegmentedControl作为UILabel子类的一部分

如何在Matlab中将数组的一部分存储到另一个数组?

如何在Logstash中将json拆分为多个事件,其中事件是哈希的一部分(唯一)

将鉴别符列添加为Entity Framework中唯一索引的一部分

如何在python-docx中以粗体显示列表项目符号中的单词的一部分