ios - UILabel - 如何在 Swift 3 中的行之间添加空格

标签 ios swift uilabel nsparagraphstyle line-spacing

我有带有段落信息(多行文本)的 UILabel,我想在行与行之间添加一些空格,类似于这张图片。

this image

请帮我做一下。我试图查看苹果开发人员关于标签和行间距的所有文档,但可以找到。

最佳答案

来自 Interface Builder( Storyboard/XIB):

enter image description here

以编程方式:

swift 4

使用标签扩展

extension UILabel {

    // Pass value for any one of both parameters and see result
    func setLineSpacing(lineSpacing: CGFloat = 0.0, lineHeightMultiple: CGFloat = 0.0) {

        guard let labelText = self.text else { return }

        let paragraphStyle = NSMutableParagraphStyle()
        paragraphStyle.lineSpacing = lineSpacing
        paragraphStyle.lineHeightMultiple = lineHeightMultiple

        let attributedString:NSMutableAttributedString
        if let labelattributedText = self.attributedText {
            attributedString = NSMutableAttributedString(attributedString: labelattributedText)
        } else {
            attributedString = NSMutableAttributedString(string: labelText)
        }

        // Line spacing attribute
        attributedString.addAttribute(NSAttributedStringKey.paragraphStyle, value:paragraphStyle, range:NSMakeRange(0, attributedString.length))

        self.attributedText = attributedString
    }
}

新增来电扩展功能

let label = UILabel()
let stringValue = "How to\ncontrol\nthe\nline spacing\nin UILabel"

// Pass value for any one argument - lineSpacing or lineHeightMultiple
label.setLineSpacing(lineSpacing: 2.0) .  // try values 1.0 to 5.0

// or try lineHeightMultiple
//label.setLineSpacing(lineHeightMultiple = 2.0) // try values 0.5 to 2.0

或使用标签实例(只需复制并执行此代码即可查看结果)

let label = UILabel()
let stringValue = "How to\ncontrol\nthe\nline spacing\nin UILabel"
let attrString = NSMutableAttributedString(string: stringValue)
var style = NSMutableParagraphStyle()
style.lineSpacing = 24 // change line spacing between paragraph like 36 or 48
style.minimumLineHeight = 20 // change line spacing between each line like 30 or 40

// Line spacing attribute
attrString.addAttribute(NSAttributedStringKey.paragraphStyle, value: style, range: NSRange(location: 0, length: stringValue.characters.count))

// Character spacing attribute
attrString.addAttribute(NSAttributedStringKey.kern, value: 2, range: NSMakeRange(0, attrString.length))

label.attributedText = attrString

swift 3

let label = UILabel()
let stringValue = "Sample text"
let attrString = NSMutableAttributedString(string: stringValue)
var style = NSMutableParagraphStyle()
style.lineSpacing = 24 // change line spacing between paragraph like 36 or 48
style.minimumLineHeight = 20 // change line spacing between each line like 30 or 40
attrString.addAttribute(NSParagraphStyleAttributeName, value: style, range: NSRange(location: 0, length: stringValue.characters.count))
label.attributedText = attrString

关于ios - UILabel - 如何在 Swift 3 中的行之间添加空格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42464091/

相关文章:

iphone - 最小字体大小问题

iPhone:在 UIScrollview 中处理 400 个 UILabels 的最佳方法

ios - 在 Objective C 中禁用键值观察

ios - 收到 ARC 语义问题如何解决?

ios - 自定义导航 Controller 和导航栏?

swift - 如何让一个 SKSpriteNode 跟随另一个 SKSpriteNode

iphone - 将数据插入 SQLite db iPhone 的正确方法

ios - Swift - 如何发送内容类型为 "x-www-form-urlencoded"的 POST 请求

swift - 为什么将 UIView 重用为两个 UITextField 的 .leftView 会导致大量内存使用

ios - 从不同的 View 更改标签