在倒计时iOS iOS UILabel中的小数点alignment方式?

对不起,我还没有任何代码,但会感谢一些build议!

我有一个倒数计时器显示UILabel(10.0,9.9,9.8等)中的一个小数点的秒。 它工作正常,但小数点左右移动,取决于大小十进制值。 有没有办法将UILabel中的文本与小数点alignment,还是应该创build两个标签(一个是右alignment的秒值,另一个是左alignment的十进制值)?

谢谢!

我认为你对多个标签的build议是完全有效的。 这是一个属性string解决scheme(对于m:ss,但它也应该与浮点数一起使用):

let string = "\t43:21" // Sample min:sec. Note the leading tab is required in this code. let countdownFont = UIFont.systemFontOfSize(13) let terminators = NSCharacterSet(charactersInString: "\t:.") // in some locales '.' is used instead of ':' let tabStop = NSTextTab(textAlignment: .Right, location: 40, options: [NSTabColumnTerminatorsAttributeName: terminators]) let paragraphStyle = NSMutableParagraphStyle() paragraphStyle.tabStops = [tabStop] let attributeDictionary: [String: AnyObject] = [NSParagraphStyleAttributeName: paragraphStyle, NSFontAttributeName: countdownFont] let attributedString = NSAttributedString(string: string, attributes: attributeDictionary) self.countdownLabel.attributedText = attributedString 

相关资源:

https://www.objc.io/issues/9-strings/string-rendering/#tables-with-numbers

https://library.oreilly.com/book/0636920034261/programming-ios-8-1st-edition/314.xhtml?ref=toc#_tab_stops

当然,一个固定宽度的字体,如Courier或Menlo也可以解决这个问题,但是它们相当鲜明地反差。