【问题标题】:Xcode: Is there a way to change line spacing (UI Label) in interface builder?Xcode:有没有办法在界面生成器中更改行距(UI 标签)?
【发布时间】:2012-08-20 05:02:39
【问题描述】:

我有几个带有多行文本的 UILabel,但行距比我希望的要大。有什么办法可以改变这个吗?

【问题讨论】:

    标签: xcode cocoa-touch interface-builder uilabel


    【解决方案1】:

    你好,这是一个迟到的回复,但它可能有助于一些行高可以改变,将文本从纯文本更改为属性

    【讨论】:

    • @souvickcse 这对自定义字体仍然不起作用。
    • 效果很好,只需要将标签高度数设置为0
    【解决方案2】:

    从 iOS 6 开始,Apple 将 NSAttributedString 添加到 UIKit,使得使用 NSParagraphStyle 更改行距成为可能。

    要真正从 NIB 更改它,please see souvickcse's answer.

    【讨论】:

    • 嗨...谢谢.. UILabel 的行间距的默认值是多少?提前谢谢..
    • 下面提供的答案是正确答案
    【解决方案3】:

    因为我讨厌在界面构建器中使用属性文本(我总是遇到 IB 错误),所以这里有一个扩展,允许您在界面构建器中直接将行高设置为 UILabel 的倍数

    extension UILabel {
    
        @IBInspectable
        var lineHeightMultiple: CGFloat {
            set{
    
                //get our existing style or make a new one
                let paragraphStyle: NSMutableParagraphStyle
                if let existingStyle = attributedText?.attribute(NSAttributedString.Key.paragraphStyle, at: 0, effectiveRange: .none) as? NSParagraphStyle, let mutableCopy = existingStyle.mutableCopy() as? NSMutableParagraphStyle  {
                    paragraphStyle = mutableCopy
                } else {
                    paragraphStyle = NSMutableParagraphStyle()
                    paragraphStyle.lineSpacing = 1.0
                    paragraphStyle.alignment = self.textAlignment
                }
                paragraphStyle.lineHeightMultiple = newValue
    
                //set our text from existing text
                let attrString = NSMutableAttributedString()
                if let text = self.text {
                    attrString.append( NSMutableAttributedString(string: text))
                    attrString.addAttribute(NSAttributedString.Key.font, value: self.font, range: NSMakeRange(0, attrString.length))
                }
                else if let attributedText = self.attributedText {
                    attrString.append( attributedText)
                }
    
                //add our attributes and set the new text
                attrString.addAttribute(NSAttributedString.Key.paragraphStyle, value:paragraphStyle, range:NSMakeRange(0, attrString.length))
                self.attributedText = attrString
            }
    
            get {
                if let paragraphStyle = attributedText?.attribute(NSAttributedString.Key.paragraphStyle, at: 0, effectiveRange: .none) as? NSParagraphStyle {
                    return paragraphStyle.lineHeightMultiple
                }
                return 0
            }
        }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-12-13
      • 1970-01-01
      • 2011-10-02
      • 1970-01-01
      • 1970-01-01
      • 2014-12-19
      • 2011-09-27
      相关资源
      最近更新 更多