Questions tagged [uilabel]

The UILabel class implements a read-only text view in iOS. You can use this class to draw one or multiple lines of static text, such as those you might use to identify other parts of your user interface. The base UILabel class provides support for both simple and complex styling of the label text. You can also control over aspects of appearance, such as whether the label uses a shadow or draws with a highlight.

The UILabel class implements a read-only text view. You can use this class to draw one or multiple lines of static text, such as those you might use to identify other parts of your user interface. The base UILabel class provides support for both simple and complex styling of the label text. You can also control over aspects of appearance, such as whether the label uses a shadow or draws with a highlight. If needed, you can customize the appearance of your text further by subclassing.

The default content mode of the UILabel class is UIViewContentModeRedraw. This mode causes the view to redraw its contents every time its bounding rectangle changes. You can change this mode by modifying the inherited contentMode property of the class.

New label objects are configured to disregard user events and clip subviews by default. If you want to handle events in a custom subclass of UILabel, you must explicitly change the value of the userInteractionEnabled property to YES after initializing the object. If you want to allow subviews to extend beyond the bounds of a label, you must explicitly change the value of the label’s clipsToBounds property to NO.

For information about basic view behaviors, see View Programming Guide for iOS.

For more information about appearance and behavior configuration, see Labels in UIKit User Interface Catalog.

6945 questions
2255
votes
49 answers

Vertically align text to top within a UILabel

I have a UILabel with space for two lines of text. Sometimes, when the text is too short, this text is displayed in the vertical center of the label. How do I vertically align the text to always be at the top of the UILabel?
Stefan
  • 28,004
  • 14
  • 62
  • 76
454
votes
25 answers

Multiple lines of text in UILabel

Is there a way to have multiple lines of text in UILabel like in the UITextView or should I use the second one instead?
Ilya Suzdalnitski
  • 49,863
  • 47
  • 128
  • 166
337
votes
30 answers

How do I make an attributed string using Swift?

I am trying to make a simple Coffee Calculator. I need to display the amount of coffee in grams. The "g" symbol for grams needs to be attached to my UILabel that I am using to display the amount. The numbers in the UILabel are changing dynamically…
dcbenji
  • 4,208
  • 5
  • 18
  • 23
322
votes
38 answers

UILabel text margin

I'm looking to set the left inset/margin of a UILabel and can't find a method to do so. The label has a background set so just changing its origin won't do the trick. It would be ideal to inset the text by 10px or so on the left hand side.
Ljdawson
  • 11,489
  • 11
  • 41
  • 58
321
votes
34 answers

Adjust UILabel height depending on the text

Consider I have the following text in a UILabel (a long line of dynamic text): Since the alien army vastly outnumbers the team, players must use the post-apocalyptic world to their advantage, such as seeking cover behind dumpsters, pillars, cars,…
Mustafa
  • 20,108
  • 39
  • 139
  • 208
295
votes
22 answers

How to control the line spacing in 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.
Abhinav
  • 36,284
  • 39
  • 178
  • 301
269
votes
21 answers

How to add line break for UILabel?

Let see that I have a string look like this: NSString *longStr = @"AAAAA\nBBBBB\nCCCCC"; How do I make it so that the UILabel display the message like this AAAAA BBBBB CCCCC I don't think \n is recognized by UILabel, so is there…
Thang Pham
  • 35,825
  • 73
  • 192
  • 279
264
votes
21 answers

How do I change the font size of a UILabel in Swift?

label.font.pointSize is read-only, so I'm not sure how to change it.
Jay
  • 8,544
  • 6
  • 31
  • 38
259
votes
14 answers

Bold & Non-Bold Text In A Single UILabel?

How would it be possible to include both bold and non-bold text in a uiLabel? I'd rather not use a UIWebView.. I've also read this may be possible using NSAttributedString but I have no idea how to use that. Any ideas? Apple achieves this in several…
DomMaiocchi
  • 2,717
  • 4
  • 14
  • 5
255
votes
35 answers

Create tap-able "links" in the NSAttributedString of a UILabel?

I have been searching this for hours but I've failed. I probably don't even know what I should be looking for. Many applications have text and in this text are web hyperlinks in rounded rect. When I click them UIWebView opens. What puzzles me is…
Lope
  • 4,836
  • 4
  • 27
  • 40
238
votes
10 answers

UILabel Align Text to center

How do I align text in UILabel?
vivianaranha
  • 2,659
  • 6
  • 31
  • 45
208
votes
37 answers

Adding space/padding to a UILabel

I have a UILabel where I want to add space in the top and in the bottom. With minimun height in constrainst I've modified it to: EDIT: To do this I've used: override func drawTextInRect(rect: CGRect) { var insets: UIEdgeInsets =…
Annachiara
  • 2,107
  • 2
  • 11
  • 10
198
votes
11 answers

Figure out size of UILabel based on String in Swift

I am trying to calculate the height of a UILabel based on different String lengths. func calculateContentHeight() -> CGFloat{ var maxLabelSize: CGSize = CGSizeMake(frame.size.width - 48, CGFloat(9999)) var contentNSString = contentText as…
Cody Weaver
  • 4,356
  • 8
  • 29
  • 50
195
votes
12 answers

Dynamically changing font size of UILabel

I currently have a UILabel: factLabel = [[UILabel alloc] initWithFrame:CGRectMake(20, 100, 280, 100)]; factLabel.text = @"some text some text some text some text"; factLabel.backgroundColor = [UIColor clearColor]; factLabel.lineBreakMode =…
CodeGuy
  • 26,751
  • 71
  • 191
  • 310
190
votes
8 answers

UIlabel layer.cornerRadius not working in iOS 7.1

I'm currently looking at a UILabel with the property addMessageLabel.layer.cornerRadius = 5.0f; On a device with iOS 7.0 installed, it has rounded corners. On a device with iOS 7.1 installed, it does not have rounded corners. Is this just a bug with…
Mike V
  • 5,136
  • 6
  • 23
  • 34
1
2 3
99 100