即使有足够的空间可用于单词,UILabel 自动换行功能也会留下空间 [英] UILabel word wrap feature leaving space even when sufficient space available for the word

查看:25
本文介绍了即使有足够的空间可用于单词,UILabel 自动换行功能也会留下空间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这个问题甚至出现在故事​​板中.

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.zerotextContainer.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 自动换行功能也会留下空间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆