Remove UILabel padding
Asked Answered
C

5

10

If it were a TextView I could do

self.textView.textContainer.lineFragmentPadding = 0;

But what I have is a multiline label. How do I remove the padding?

Conchoidal answered 19/2, 2015 at 21:39 Comment(0)
U
2

If you're using iOS 8, try changing the insets value of layoutMargins property. If it doesn't work or you're targeting earlier versions, look at this answer.

Unifoliolate answered 19/2, 2015 at 22:11 Comment(0)
B
1

NSMutableParagraphStyle has an attribute called lineSpacing. Try adding this attribute to an attributed string and setting the label's attributedText

Badminton answered 19/2, 2015 at 21:57 Comment(0)
C
1

you can try this:

let attributedString = NSMutableAttributedString(string: str)
let paragraphStyle = NSMutableParagraphStyle()
paragraphStyle.lineHeightMultiple = 0.75 //or other values you like
attributedString.addAttribute(
    NSAttributedString.Key.paragraphStyle,
    value: paragraphStyle,
    range: NSMakeRange(0, str.count)
)
typeLabel.attributedText = attributedString
Castile answered 16/9, 2019 at 11:53 Comment(0)
M
-1

Try this

label.padding = UIEdgeInsets(top: 0, left: 0, bottom: 0, right: 0)
Molal answered 13/7, 2022 at 5:40 Comment(0)
U
-3

I recommend to change your UILabel into UITextView and put this code.

yourTextView.textContainerInset = UIEdgeInsetsMake(0,0,0,0);

don't forget to set yourTextView

yourTextView.editable = NO
yourTextView.scrollEnabled = NO
Ulmer answered 18/3, 2017 at 16:58 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.