简体   繁体   English

在Xcode Swift中使用不同字体大小的UILabel中心文本对齐问题

[英]Issue with UILabel center text alignment at different font sizes in Xcode Swift

This is an issue with UILabel in iOS7 and iOS8, it does not present in iOS9. 这是iOS7和iOS8中UILabel的问题,在iOS9中不存在。 This odd UILabel behaviour is experienced with different fonts, in Xcode versions 6 and 7 with Swift, using the simulator and device. 在带有Swift和模拟器的Xcode版本6和7中,使用不同的字体会遇到这种奇怪的UILabel行为。

Problem: 问题:

I have a UILabel that has a center text alignment. 我有一个具有中心文本对齐方式的UILabel。 At different font sizes, some lines of text stop being center aligned and snap to left alignment. 在不同的字体大小下,某些文本行将停止居中对齐并对齐到左对齐。 It is subtle but noticeable. 这是微妙的但值得注意的。

In the images below: 在以下图片中:

LEFT: all lines of text are centered neatly. 左:所有文本行均居中对齐。

MIDDLE: after increasing the font size, the third line from the top snaps more to the left instead of remaining centered. 中:增加字体大小后,从顶部开始的第三行向左对齐,而不是居中。 (Notice more blue space on right.) (请注意右侧有更多蓝色空间。)

RIGHT: after increasing the font size a little more, both the second and third line from the top snap to the left instead of remaining centered. 右:稍微增加字体大小后,从顶部开始的第二行和第三行都向左对齐,而不是居中。 (Notice more blue space on right.) (请注意右侧有更多蓝色空间。)

在此处输入图片说明

Questions: 问题:

  1. What explains what might be the cause of this unusual alignment behaviour in iOS7 and iOS8 but not iOS9? 是什么解释了导致iOS7和iOS8(而非iOS9)这种异常对齐行为的原因是什么?
  2. How can I ensure that all lines are forced to remain center aligned perfectly regardless of the font type or font size used? 如何确保无论所使用的字体类型或字体大小如何,所有行均被强制保持完全居中对齐?
  3. What is the bug fix or work around for this in iOS7 and iOS8 so that apps created using UILabel centre aligned don't feature the bug? 在iOS7和iOS8中,此问题的修补程序或解决方法是什么,以便使用对齐的UILabel中心创建的应用程序不包含该错误?

Use UITextView 使用UITextView

With all due respect, it is my experience that UILabel is plagued with bugs when it comes to multiple lines (Line count other than 1 ). 出于所有应有的尊重,根据我的经验,当涉及多行(行计数不是1 )时, UILabel会遇到很多bug。
Starting, but not limited to sizing cells containing such labels hardly able to predict its footprint 1 . 开始但不限于确定包含此类标签的细胞的大小,几乎无法预测其足迹1

Use UITextView , non editable , scrolling disabled (that is the key). 使用UITextView ,不可编辑禁用滚动(这是关键)。 You will achieve the same effects, with more control over the final appearance. 您将获得相同的效果,并且可以更好地控制最终外观。


1 See note 3 on this Stack Overflow UILabel answer . 1请参见有关此堆栈溢出UILabel答案的注释3。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM