UILabel中如何控制行距 [英] How to control the line spacing in UILabel

查看:20
本文介绍了UILabel中如何控制行距的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

UILabel 中放入多行时,是否可以减少文本之间的间隙?我们可以设置框架、字体大小和行数.我想减少该标签中两行之间的差距.

Is it possible to reduce the gap between text, when put in multiple lines in a UILabel? We can set the frame, font size and number of lines. I want to reduce the gap between the two lines in that label.

推荐答案

我想在这个答案中添加一些新的东西,所以我感觉没那么糟糕...这是一个 Swift 的答案:

I thought about adding something new to this answer, so I don't feel as bad... Here is a Swift answer:

import Cocoa

let paragraphStyle = NSMutableParagraphStyle()
paragraphStyle.lineSpacing = 40

let attrString = NSMutableAttributedString(string: "Swift Answer")
attrString.addAttribute(.paragraphStyle, value:paragraphStyle, range:NSMakeRange(0, attrString.length))

var tableViewCell = NSTableCellView()
tableViewCell.textField.attributedStringValue = attrString


简短的回答:你不能.要改变文本行之间的间距,你必须继承 UILabel 并滚动你自己的 drawTextInRect,或者创建多个标签."


"Short answer: you can't. To change the spacing between lines of text, you will have to subclass UILabel and roll your own drawTextInRect, or create multiple labels."

参见:设置 UILabel 行间距

这是一个非常古老的答案,其他人已经添加了新的更好的方法来处理这个问题.请参阅下面提供的最新答案.

This is a really old answer, and other have already addded the new and better way to handle this.. Please see the up to date answers provided below.

这篇关于UILabel中如何控制行距的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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