我使用 MapKit 来记录运行距离。我有以下代码:
var runningDistance : Double = 0{
didSet{
guard let text = runningDistanceLabel.attributedText as? NSMutableAttributedString else{return}
let range = NSMakeRange(0, 4)
let attributeString = NSAttributedString(string: String(format:"%.2f",runningDistance/1000.0), attributes: [
NSFontAttributeName:UIFont(name: "DINCondensed-Bold", size: 17)!
])
text.replaceCharacters(in: range, with: attributeString)
runningDistanceLabel.attributedText = text
print(runningDistanceLabel.text)
print(runningDistanceLabel.attributedText)
}
}
当 userLocation 更新时,我计算距离并设置 runningDistance。
在控制台:
Optional("0.04公里")
Optional(0.04{
NSFontAttributeName = "Optional(<UICTFont: 0x7ff79b7b7250> font-family: \"DIN Condensed\"; font-weight: bold; font-style: normal; font-size: 17.00pt)";
}公里{
NSFont = "<UICTFont: 0x7ff79d800110> font-family: \".PingFangSC-Regular\"; font-weight: normal; font-style: normal; font-size: 14.00pt";
NSParagraphStyle = "Alignment 2, LineSpacing 0, ParagraphSpacing 0, ParagraphSpacingBefore 0, HeadIndent 0, TailIndent 0, FirstLineHeadIndent 0, LineHeight 0/0, LineHeightMultiple 0, LineBreakMode 4, Tabs (\n 28L,\n 56L,\n 84L,\n 112L,\n 140L,\n 168L,\n 196L,\n 224L,\n 252L,\n 280L,\n 308L,\n 336L\n), DefaultTabInterval 0, Blocks (\n), Lists (\n), BaseWritingDirection -1, HyphenationFactor 0, TighteningForTruncation NO, HeaderLevel 0";
})
这表明 runningDistanceLabel 的 text 和 attributedText 已设置和更改。 但在模拟器中,标签的文本保持不变。
如果我替换:
runningDistanceLabel.attributedText = text
与:
runningDistanceLabel.text = "\(runningDistance)"
然后在模拟器中,标签的文字就可以正常改变了。
关于设置 attributedText 的代码有什么问题?如何在模拟器中设置attributedText并正确更新?
非常感谢!
编辑:
我确定 guard 已经通过,因为它在控制台中打印了标签的文本和 attributedText 并且我添加了一个断点来测试它。
我也试试:
DispatchQueue.main.async {[unowned self] in
self.runningDistanceLabel.attributedText = text
}
但我仍然无法更新标签的文本。
解决方案
在 Rroobb 的帮助下,我在设置属性文本之前添加了一行代码来设置标签的文本:
runningDistanceLabel.text = String(format:"%.2f",runningDistance/1000.0) + "公里"
runningDistanceLabel.attributedText = text
并且在模拟器中,标签的文本现在可以更新了。太棒了!
在关于 UILabel 的 attributedText 的 iOS 文档中:
Assigning a new value to this property also replaces the value of the text property with the same string data, albeit without any formatting information.
好像设置attributedText可以自动改变text属性。
但在这里我不知道为什么我应该在设置attributedText之前设置text以更新标签的文本。
最佳答案
这行得通,我添加了两行代码:
var runningDistance : Double = 0.0 {
didSet{
guard let text = runningDistanceLabel.attributedText as? NSMutableAttributedString else{
return
}
let range = NSMakeRange(0, 4)
runningDistanceLabel.text = String(format:"%.2f",runningDistance/1000.0) //here
runningDistanceLabel.font = UIFont(name: "DINCondensed-Bold", size: 17) // here
let attributeString = NSAttributedString(string: String(format:"%.2f",runningDistance/1000.0), attributes: [
"NSFontAttributeName":UIFont(name: "DINCondensed-Bold", size: 17)!
])
text.replaceCharacters(in: range, with: attributeString)
runningDistanceLabel.attributedText = text
print(runningDistanceLabel.text!)
print(runningDistanceLabel.attributedText!)
}
}
关于ios - 设置 attributedText 后 UILabel 不更新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39979981/
我有一个Ruby程序,它使用rubyzip压缩XML文件的目录树。gem。我的问题是文件开始变得很重,我想提高压缩级别,因为压缩时间不是问题。我在rubyzipdocumentation中找不到一种为创建的ZIP文件指定压缩级别的方法。有人知道如何更改此设置吗?是否有另一个允许指定压缩级别的Ruby库? 最佳答案 这是我通过查看rubyzip内部创建的代码。level=Zlib::BEST_COMPRESSIONZip::ZipOutputStream.open(zip_file)do|zip|Dir.glob("**/*")d
给定这段代码defcreate@upgrades=User.update_all(["role=?","upgraded"],:id=>params[:upgrade])redirect_toadmin_upgrades_path,:notice=>"Successfullyupgradeduser."end我如何在该操作中实际验证它们是否已保存或未重定向到适当的页面和消息? 最佳答案 在Rails3中,update_all不返回任何有意义的信息,除了已更新的记录数(这可能取决于您的DBMS是否返回该信息)。http://ar.ru
我在使用omniauth/openid时遇到了一些麻烦。在尝试进行身份验证时,我在日志中发现了这一点:OpenID::FetchingError:Errorfetchinghttps://www.google.com/accounts/o8/.well-known/host-meta?hd=profiles.google.com%2Fmy_username:undefinedmethod`io'fornil:NilClass重要的是undefinedmethodio'fornil:NilClass来自openid/fetchers.rb,在下面的代码片段中:moduleNetclass
我正在查看instance_variable_set的文档并看到给出的示例代码是这样做的:obj.instance_variable_set(:@instnc_var,"valuefortheinstancevariable")然后允许您在类的任何实例方法中以@instnc_var的形式访问该变量。我想知道为什么在@instnc_var之前需要一个冒号:。冒号有什么作用? 最佳答案 我的第一直觉是告诉你不要使用instance_variable_set除非你真的知道你用它做什么。它本质上是一种元编程工具或绕过实例变量可见性的黑客攻击
我将应用程序升级到Rails4,一切正常。我可以登录并转到我的编辑页面。也更新了观点。使用标准View时,用户会更新。但是当我添加例如字段:name时,它不会在表单中更新。使用devise3.1.1和gem'protected_attributes'我需要在设备或数据库上运行某种更新命令吗?我也搜索过这个地方,找到了许多不同的解决方案,但没有一个会更新我的用户字段。我没有添加任何自定义字段。 最佳答案 如果您想允许额外的参数,您可以在ApplicationController中使用beforefilter,因为Rails4将参数
我想设置一个默认日期,例如实际日期,我该如何设置?还有如何在组合框中设置默认值顺便问一下,date_field_tag和date_field之间有什么区别? 最佳答案 试试这个:将默认日期作为第二个参数传递。youcorrectlysetthedefaultvalueofcomboboxasshowninyourquestion. 关于ruby-on-rails-date_field_tag,如何设置默认日期?[rails上的ruby],我们在StackOverflow上找到一个类似的问
这里有一个很好的答案解释了如何在Ruby中下载文件而不将其加载到内存中:https://stackoverflow.com/a/29743394/4852737require'open-uri'download=open('http://example.com/image.png')IO.copy_stream(download,'~/image.png')我如何验证下载文件的IO.copy_stream调用是否真的成功——这意味着下载的文件与我打算下载的文件完全相同,而不是下载一半的损坏文件?documentation说IO.copy_stream返回它复制的字节数,但是当我还没有下
我正在尝试解析一个文本文件,该文件每行包含可变数量的单词和数字,如下所示:foo4.500bar3.001.33foobar如何读取由空格而不是换行符分隔的文件?有什么方法可以设置File("file.txt").foreach方法以使用空格而不是换行符作为分隔符? 最佳答案 接受的答案将slurp文件,这可能是大文本文件的问题。更好的解决方案是IO.foreach.它是惯用的,将按字符流式传输文件:File.foreach(filename,""){|string|putsstring}包含“thisisanexample”结果的
我正在玩HTML5视频并且在ERB中有以下片段:mp4视频从在我的开发环境中运行的服务器很好地流式传输到chrome。然而firefox显示带有海报图像的视频播放器,但带有一个大X。问题似乎是mongrel不确定ogv扩展的mime类型,并且只返回text/plain,如curl所示:$curl-Ihttp://0.0.0.0:3000/pr6.ogvHTTP/1.1200OKConnection:closeDate:Mon,19Apr201012:33:50GMTLast-Modified:Sun,18Apr201012:46:07GMTContent-Type:text/plain
我在Rails应用程序中使用CarrierWave/Fog将视频上传到AmazonS3。有没有办法判断上传的进度,让我可以显示上传进度如何? 最佳答案 CarrierWave和Fog本身没有这种功能;你需要一个前端uploader来显示进度。当我不得不解决这个问题时,我使用了jQueryfileupload因为我的堆栈中已经有jQuery。甚至还有apostonCarrierWaveintegration因此您只需按照那里的说明操作即可获得适用于您的应用的进度条。 关于ruby-on-r