在我的应用程序中,我需要在 UILabel 中重复显示“正在加载”文本,如下所示:
加载中。加载中.. 加载中... 加载中 加载中。加载中.. 加载中...
我该怎么做?请问有什么建议吗?
我有一个带有动态文本的 uiTextField。
如何通过编辑器将其设置为将文本分成 2 行(或更多行)
如果文本不适合 uiTextField 宽度?
有没有办法不以编程方式做到这一点?
现在文本被视为
"This is a long line" ===>
"This is...."
Run Code Online (Sandbox Code Playgroud)
我希望它是
"This is a
long line"
Run Code Online (Sandbox Code Playgroud) 我需要在文本的某些部分添加一些tapGestures,即UILabel中的那些部分.它似乎能够创建一个超链接 - 所以我想它也可以做一个函数调用,但我只是不知道该怎么做.
继承我的代码:
let regularFont = UIFont(name: Constants.robotoRegular, size: 13)!
let att1 = [NSFontAttributeName: regularFont]
let turqoiseFont = UIFont(name: Constants.robotoBold, size: 13)!
let att2 = [NSFontAttributeName: turqoiseFont]
let attString1 = NSMutableAttributedString(string: "By creating a profile, I accept ", attributes: att1)
attString1.addAttribute(NSForegroundColorAttributeName, value: UIColor.darkGrayColor(), range: NSMakeRange(0, attString1.length))
let attString2 = NSMutableAttributedString(string: "Terms and Conditions ", attributes: att2)
attString2.addAttribute(NSForegroundColorAttributeName, value: Colors.loginButtonColor, range: NSMakeRange(0, attString2.length))
let attString3 = NSMutableAttributedString(string: "and ", attributes: att1)
attString3.addAttribute(NSForegroundColorAttributeName, value: UIColor.darkGrayColor(), range: NSMakeRange(0, attString3.length))
let attString4 = …Run Code Online (Sandbox Code Playgroud) 我们如何使用extension来设置默认值?例如,如果我UILabel在我storyboard的storyboard. 现在我想创建一个扩展,以便在应用程序中初始化标签时将我的文本值设置为“popo”。更清楚地说,我想这样做是将整个应用程序中的默认值设置为我的 UILabel。希望这很清楚。
extension UILabel {
open override func ??? {
self.text == "popo"
}
}
Run Code Online (Sandbox Code Playgroud)
请不要告诉我在故事板中设置 ma 值,因为这不是我需要的。
如何使用给定字体计算2 行 UILabel的高度?
我知道,一般多行标签有一种方法可以通过以下方式动态计算它们的大小
let label = UILabel()
label.numberOfLines = 0
label.text = "Some multiline [...] text"
let constraintSize = CGRect(width: aWidth, height: CGFloat.greatestFiniteMagnitude)
let size = aLabel.sizeThatFits(constraintSize)
Run Code Online (Sandbox Code Playgroud)
现在,我想知道的是:在不知道将在标签中写入的文本,而只知道字体的情况下,如何优雅地计算正好跨越2 行(或通常为x)行的标签高度?
优雅的意思:也许有人找到了比这更好的解决方案:
let label = UILabel()
label.numberOfLines = 2
label.text = "Some really, really, really, [...] long text to make sure it spans at least 2 lines even for the tiniest fonts."
let constraintSize = CGRect(width: aWidth, height: CGFloat.greatestFiniteMagnitude)
let size = …Run Code Online (Sandbox Code Playgroud) 我需要将 a 添加UITapGestureRecognizer到多个,UILabel并且它们都需要转到相同的函数进行处理。
我有的是这个:
@IBOutlet weak var label_something: UILabel!
let tap = UITapGestureRecognizer(target: self, action: #selector(self.myFunction))
label_something.addGestureRecognizer(tap)
Run Code Online (Sandbox Code Playgroud)
在这里收到:
@objc func myFunction(sender:UITapGestureRecognizer) { // Something... }
Run Code Online (Sandbox Code Playgroud)
像魅力一样工作。问题是它只适用于一个 UILabel(如果添加addGestureRecognizer(tap)到多个UIlabel它只适用于添加的最后一个)
所以我的问题是:
如何实现我想在这里做的事情?带有 tapRecognizer 的五个不同的 UILabels 具有相同的功能
我在堆栈视图中显示两个标签,如下图所示。我希望第一个标签显示其全部内容。为此,我尝试使其水平内容拥抱优先级以及其水平内容压缩阻力优先级高于其他标签。我仍然得到如图所示的裁剪标签。这不是应该工作吗?我如何得到想要的结果?堆栈视图的对齐设置为“填充”,其分布设置为“按比例填充”。
再会,
我正在开发一个工作正常的锻炼应用程序,除非将它移到后台。计时器挂起时暂停。我找到了一个我正在工作的后台计时器示例,但现在我无法让显示锻炼持续时间的 UILabel 工作。在控制台中,它指出我正在从我理解的主线程访问一个对象。我不知道该怎么做是让 UILabel 更新,因为计时器从后台线程中更新,更新标签在主线程中。
这是我所拥有的(打印语句帮助我遵循代码):
import UIKit
class ViewController: UIViewController {
var time = 0
var timer = Timer()
@IBOutlet weak var outputLabel: UILabel!
@IBOutlet weak var start: UIButton!
@IBOutlet weak var paused: UIButton!
@IBAction func startButton(_ sender: UIButton) {
startButtonPressed()
}
@IBAction func pausedButton(_ sender: UIButton) {
pausedButtonPressed()
}
@IBOutlet weak var timerLabel: UILabel!
func updateTimerLabel() {
let hours = Int(self.time) / 3600
let minutes = Int(self.time) / 60 % 60
let seconds = Int(self.time) % 60 …Run Code Online (Sandbox Code Playgroud) 我想添加文本,无论是 aUILabel还是CATextLayera CGPath。我意识到此功能背后的数学相当复杂,但想知道 Apple 是否提供了开箱即用的此功能,或者是否有开源 SDK 可以在 Swift 中实现这一点。谢谢!
我想在 UIStackview 中删除带有属性文本的 UILabel 的底部填充。
我找到了这个解决方案How to remove the extra padding under a one line UILabel。这适用于普通文本,但不适用于属性文本。
let textLabel = UILabel()
textLabel.translatesAutoresizingMaskIntoConstraints = false
textLabel.text = "What is a chemical property and how can you observe it?"
textLabel.numberOfLines = 0
textLabel.lineBreakMode = .byWordWrapping
textLabel.backgroundColor = .lightGray
mainStackView.addArrangedSubview(textLabel)
let textLabel2 = UILabel()
textLabel2.translatesAutoresizingMaskIntoConstraints = false
let html = "<html lang=\"en\"><head><meta charset=\"UTF-8\"></head><body><div style=\"font-size:36;\"><p>What is a <em>chemical property</em> and how can you observe it?</p></div></body></html>"
let data = Data(html.utf8)
if let …Run Code Online (Sandbox Code Playgroud) uilabel ×10
ios ×7
swift ×7
uistackview ×2
background ×1
calayer ×1
catextlayer ×1
cgpath ×1
iphone ×1
objective-c ×1
swift3 ×1
uikit ×1