You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在Swift中自定义UIButton的文本段落行间距?

如何修改UIButton文本的行间距?

我已经用Swift 5写了一个显示五行文本的大UIButton,代码如下,但不知道怎么通过代码调整文本的行间距,比如设置双倍行间距或者半倍行间距?

import UIKit

class ViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        addMyButton()
    }
    
    func addMyButton() {
        let myButtonTitleLabelString = "First\nSecond\nThird\nFourth\nFifth"
        let myButtonTitleFontName = "HelveticaNeue"
        let myButtonTitleFontSize = CGFloat(20)
        let myButton = UIButton(type: .custom)
        
        myButton.setTitle(myButtonTitleLabelString, for: .normal)
        myButton.titleLabel!.textAlignment = .center
        myButton.titleLabel!.lineBreakMode = .byWordWrapping
        myButton.titleLabel!.font = UIFont(name: myButtonTitleFontName, size: myButtonTitleFontSize)
        myButton.titleEdgeInsets = UIEdgeInsets(top: 0, left: 0, bottom: 0, right: 0)
        myButton.setTitleColor(.white, for: .normal)
        myButton.backgroundColor = .orange
        myButton.frame = CGRect(x: 40, y: 100, width: 300, height: 300)
        
        view.addSubview(myButton)
    }
}

解决方案

UIButton的普通文本设置(setTitle)无法直接调整行间距,需要通过**富文本(NSAttributedString)**来实现。核心是创建NSMutableParagraphStyle并设置lineSpacing属性,再将其与文本、字体、颜色等属性组合后赋值给按钮。

具体步骤:

  • 创建段落样式对象,设置需要的行间距值
  • 定义富文本的属性字典,包含字体、文本颜色、段落样式
  • 用上述属性创建NSAttributedString,通过setAttributedTitle设置给按钮

修改后的完整代码

import UIKit

class ViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        addMyButton()
    }
    
    func addMyButton() {
        let myButtonTitleLabelString = "First\nSecond\nThird\nFourth\nFifth"
        let myButtonTitleFontName = "HelveticaNeue"
        let myButtonTitleFontSize = CGFloat(20)
        let myButton = UIButton(type: .custom)
        
        // 1. 创建段落样式,设置行间距
        let paragraphStyle = NSMutableParagraphStyle()
        // 示例:设置双倍行间距(取字体大小作为行间距值,可按需调整)
        paragraphStyle.lineSpacing = myButtonTitleFontSize
        // 半倍行间距可设置为 myButtonTitleFontSize * 0.5
        
        // 2. 定义富文本属性
        let attributes: [NSAttributedString.Key: Any] = [
            .font: UIFont(name: myButtonTitleFontName, size: myButtonTitleFontSize)!,
            .foregroundColor: UIColor.white,
            .paragraphStyle: paragraphStyle
        ]
        
        // 3. 创建富文本并设置给按钮
        let attributedTitle = NSAttributedString(string: myButtonTitleLabelString, attributes: attributes)
        myButton.setAttributedTitle(attributedTitle, for: .normal)
        
        // 保留原有基础设置
        myButton.titleLabel!.textAlignment = .center
        myButton.titleLabel!.lineBreakMode = .byWordWrapping
        myButton.titleEdgeInsets = UIEdgeInsets(top: 0, left: 0, bottom: 0, right: 0)
        myButton.backgroundColor = .orange
        myButton.frame = CGRect(x: 40, y: 100, width: 300, height: 300)
        
        view.addSubview(myButton)
    }
}

说明

  • lineSpacing的单位是点(pt),默认值为0。可根据需求自定义:
    • 双倍行间距:设置为字体大小(如示例中的20)
    • 半倍行间距:设置为字体大小的一半(如10)
    • 也可设置任意数值,比如5(紧凑行间距)或30(宽松行间距)
  • 使用自定义字体时,需确保字体名称正确,避免UIFont初始化返回nil的情况

内容的提问来源于stack exchange,提问作者user4806509

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.19 06:30:50