Set Line Spacing

How to control the line spacing in UILabel

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

"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."

See: Set UILabel line spacing


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.

How to set the line spacing for Qml Text Item?

A good habit is to check the documentation. Browsing through it, you could see a property called lineHeight. I believe this is what you're looking for. From the documentation:

lineHeight : real

Sets the line height for the text. The value can be in pixels or a multiplier depending on lineHeightMode.

They also tell you how to use it

The default value is a multiplier of 1.0. The line height must be a positive value.

Using lineHeight as a multiplier allows you to mimic the following line-spacing enumerations in MSWord.

Single
1.5 lines
Double
Multiple

Here's an example:

import QtQuick 2.0
import QtQuick.Window 2.0

Window {
visible: true
width: 200
height: 300

Text {
id: text
width: 175
anchors.centerIn: parent

// text: "HELLO HELLO HELLO HELLO HELLO HELLO HELLO HELLO HELLO HELLO HELLO HELLO HELLO HELLO"
text: "Cat ipsum dolor sit amet, sleep nap. You call this cat food. Push your water glass on the floor."

font.family: "Monaco" // Monaco ❤️
wrapMode: Text.WordWrap // Make the text multi-line
horizontalAlignment: Text.AlignHCenter

// lineHeight: 1.0 // single-spacing (default)
lineHeight: 1.5 // 1.5 line-spacing
// lineHeight: 2.0 // double-spacing
// lineHeight: 3.0 // triple-spacing

}
}

Here are the results of using different values of lineHeight (on a typical MacOS)

Single-spacing

1x Line Spacing

1.5x, Double (2x), Triple (3x)

1.5x Line Spacing
2x Line Spacing
3x Line Spacing


However, if you want to mimic the other line-spacing enumerations:

At least
Exactly

you'll need to modify the pixel height. You can do this by setting lineHeightMode to Text.FixedHeight. Like so

Window {
visible: true
width: 200
height: 300

Text {
id: text
width: 175
anchors.centerIn: parent

text: "Cat ipsum dolor sit amet, sleep nap. You call this cat food. Push your water glass on the floor."

font.family: "Monaco" // Monaco ❤️
wrapMode: Text.WordWrap // Make the text multi-line


lineHeightMode: Text.FixedHeight
lineHeight: 6 // exaggerated, text will be scrunched up

}
}

Exactly 6

Exactly 6

Set line spacing

Try the line-height property.

For example, 12px font-size and 4px distant from the bottom and upper lines:

line-height: 20px; /* 4px +12px + 4px */

Or with em units

line-height: 1.7em; /* 1em = 12px in this case. 20/12 == 1.666666  */

How to Increase Line spacing in UILabel in Swift

Programatically add LineSpacing to your UILabel using following snippet.

Earlier Swift version

let attributedString = NSMutableAttributedString(string: "Your text")

// *** Create instance of `NSMutableParagraphStyle`
let paragraphStyle = NSMutableParagraphStyle()

// *** set LineSpacing property in points ***
paragraphStyle.lineSpacing = 2 // Whatever line spacing you want in points

// *** Apply attribute to string ***
attributedString.addAttribute(NSParagraphStyleAttributeName, value:paragraphStyle, range:NSMakeRange(0, attributedString.length))

// *** Set Attributed String to your label ***
label.attributedText = attributedString

Swift 4.0

let attributedString = NSMutableAttributedString(string: "Your text")

// *** Create instance of `NSMutableParagraphStyle`
let paragraphStyle = NSMutableParagraphStyle()

// *** set LineSpacing property in points ***
paragraphStyle.lineSpacing = 2 // Whatever line spacing you want in points

// *** Apply attribute to string ***
attributedString.addAttribute(NSAttributedStringKey.paragraphStyle, value:paragraphStyle, range:NSMakeRange(0, attributedString.length))

// *** Set Attributed String to your label ***
label.attributedText = attributedString

Swift 4.2

let attributedString = NSMutableAttributedString(string: "Your text")

// *** Create instance of `NSMutableParagraphStyle`
let paragraphStyle = NSMutableParagraphStyle()

// *** set LineSpacing property in points ***
paragraphStyle.lineSpacing = 2 // Whatever line spacing you want in points

// *** Apply attribute to string ***
attributedString.addAttribute(NSAttributedString.Key.paragraphStyle, value:paragraphStyle, range:NSMakeRange(0, attributedString.length))

// *** Set Attributed String to your label ***
label.attributedText = attributedString

How to set a line spacing in a text of a TextView

Take a look at android:lineSpacingExtra (https://developer.android.com/reference/android/widget/TextView.html#attr_android:lineSpacingExtra) and android:lineSpacingMultiplier (https://developer.android.com/reference/android/widget/TextView.html#attr_android:lineSpacingMultiplier).

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:lineSpacingExtra="5dp"
android:text="abc \n defgh"/>

How do I change the line spacing in Visual Studio 2012?

I've found a workaround which works pretty well, partly alread described in this comment.

You need to download and install TypeLight (free for personal use), and open the Consolas.ttf file inside. To adjust the line spacing, go to Font -> Metrics -> Advanced and change the "Line Gap" to your desire (I changed "Type Line Gap" as well, but I have no idea what this actually does). I experimented a bit with a values and found around 730 to be similar to the 12 pixels spacing in IDEA.

To distinguish the modified font from the regular Consolas, I suggest changing the name of the font under Font -> Names (to "Consolas Spaced" or something). Save the font, double-click the file and choose "Install" from the preview dialog. Then (re)start Visual Studio, choose your modified font and you're done.



Related Topics



Leave a reply



Submit