问题描述
这个问题甚至出现在故事板中.
The problem coming even in storyboard.
UILabel 具有以下属性:
- numberOfLines = 0
- lineBreakMode = .byWordWrapping
- 约束:领先 &落后26分到superview;垂直居中.
- 自定义字体:中等 17 磅.
如您所见,第四个单词无法放入第一行,因此会出现布局错误的问题.如果我删除最后一个单词,则句子完全适合一行或说出第四个单词.如果在它之后添加一个单词会将它们都移动到下一行,这会留下很多空间.它应该尝试在一行中尽可能不中断或连字符来适应单词.但是很明显,即使单词可以容纳,也会产生空白.
您可以在新项目中重新创建它并观察问题.
You can recreate this in a new project and observe the issue.
推荐答案
你可能想试试这个...
You may want to give this a try...
子类 UITextView,禁用滚动、编辑和选择...将 textContainerInset = UIEdgeInsets.zero 和 textContainer.lineFragmentPadding = 0 设置为零.
Subclass UITextView, disable scrolling, editing and selecting... set the textContainerInset = UIEdgeInsets.zero and textContainer.lineFragmentPadding = 0 to Zero.
结果:
代码(@IBDesignable 所以我们可以在 IB/Storyboard 中看到它):
Code (@IBDesignable so we can see it in IB / Storyboard):
@IBDesignable
class TextViewLabel: UITextView {
override init(frame: CGRect, textContainer: NSTextContainer?) {
super.init(frame: frame, textContainer: textContainer)
commonInit()
}
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
commonInit()
}
func commonInit() -> Void {
isScrollEnabled = false
isEditable = false
isSelectable = false
textContainerInset = UIEdgeInsets.zero
textContainer.lineFragmentPadding = 0
}
}
这篇关于即使有足够的空间可用于单词,UILabel 自动换行功能也会留下空间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!


大气响应式网络建站服务公司织梦模板
高端大气html5设计公司网站源码
织梦dede网页模板下载素材销售下载站平台(带会员中心带筛选)
财税代理公司注册代理记账网站织梦模板(带手机端)
成人高考自考在职研究生教育机构网站源码(带手机端)
高端HTML5响应式企业集团通用类网站织梦模板(自适应手机端)