Dynamically adding multiline label in a view

s.d.

I am dynamically creating UILabel in my app to display a list of directions for a recipe. The labels populate correctly displaying all the items one after another.

The problem is when the text goes on next line of the label, it is overlapped with the next label.

I have set numberOfLines to 0 and also set lineBreakMode to NSLineBreakByWordWrapping. This helps the label to display text on multiple lines.

I have tried to adjust the height of the label, as you will see in the code, but it doesn't work.

How do I prevent the overlapping of labels due to multiline text in label?

Here is the code for populating the labels with multilines:

//add all the directions to the uiview
    for (int i = 0; i < self.recipe.directions.count; i++)
    {
        UILabel *label =  [[UILabel alloc] initWithFrame: CGRectMake(0,(i+1)*25,280,25)];
        label.lineBreakMode = NSLineBreakByWordWrapping; //multiple lines in a label
        label.numberOfLines = 0;
        label.text =[NSString stringWithFormat: @"%@", self.recipe.directions[i]];
        [label sizeToFit]; // resize the width and height to fit the text
        NSLog(@"Actual height is: %f", label.frame.size.height); // Use this for spacing any further elements


        CGSize expectedLabelSize = [label.text sizeWithFont:label.font
                                            constrainedToSize:label.frame.size
                                                lineBreakMode:NSLineBreakByWordWrapping];
        //adjust the label the the new height.
        CGRect newFrame = label.frame;
        newFrame.size.height = expectedLabelSize.height;
        label.frame = newFrame;
        [self.directionsView addSubview:label];

    }
lucaslt89

Instead of initialising your label with an y position of (i+1)*25, you should store your last label's bottom position

CGFloat lastLabelBottomCoordinate = 25;
CGFloat spaceBetweenLines = 10;
for (int i = 0; i < 10; i++)
{
    UILabel *label =  [[UILabel alloc] initWithFrame: CGRectMake(0, lastLabelBottomCoordinate + spaceBetweenLines,280,25)];
    label.lineBreakMode = NSLineBreakByWordWrapping; //multiple lines in a label
    label.numberOfLines = 0;
    label.text =[NSString stringWithFormat: @"This is a very long text to see if the text have more than 2 lines"];
    [label sizeToFit]; // resize the width and height to fit the text
    NSLog(@"Actual height is: %f", label.frame.size.height); // Use this for spacing any further elements


    CGSize expectedLabelSize = [label.text boundingRectWithSize:CGSizeMake(label.frame.size.width, MAXFLOAT)
                                                        options:NSStringDrawingUsesLineFragmentOrigin
                                                     attributes:@{NSFontAttributeName : label.font}
                                                        context:nil].size;
    //adjust the label the the new height.
    CGRect newFrame = label.frame;
    newFrame.size.height = expectedLabelSize.height;
    label.frame = newFrame;

    lastLabelBottomCoordinate = label.frame.origin.y + label.frame.size.height;
    [self.view addSubview:label];

}

This is how it looks:

enter image description here

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Dynamically adding multiline label in a view

From Dev

Adding a view dynamically to Layout

From Dev

Dynamically adding instances of a view to a TransparentContainer

From Dev

Adding Android buttons to view dynamically

From Dev

Dynamically adding buttons to scroll view

From Dev

Adding hyperlink to dynamically created label in VBA Excel

From Dev

Can't autoresize cell in table view with multiline label

From Dev

Adding caption label to UImageView in Collection View

From Dev

Alignment not right when adding view dynamically to GridLayout

From Dev

C# Adding Controls in a Grid view dynamically

From Dev

Adding scroll view to linear layout dynamically

From Dev

Adding scroll view to linear layout dynamically

From Dev

Adding Labels and images dynamically to a custom view in Android

From Dev

Dynamically adding Groups and Headers to an Expandable List View

From Dev

Adding view dynamically inside template Ember Js

From Java

Multiline label in UIStackView

From Dev

multiline TextField - floating label

From Dev

Multiline Label On UIButton Swift

From Dev

CCLabelTTF multiline label not working

From Dev

Adding a dynamically sized view (height) to a UIScrollView with Auto Layout

From Dev

Adding a gesture dynamically to an existing table view section header

From Dev

Adding DIVs dynamically to table and showing with jQuery and partial view

From Dev

Adding View dynamically and setting Parameters and rules is not working properly (android)

From Dev

Dynamically adding view or inflating layout to RecyclerView's item

From Dev

Dynamically adding data to page with aurelia after the view is rendered

From Dev

Cannot show paged table view to when adding text dynamically to a table

From Dev

How to make multiline label with Eureka

From Dev

QML Label max width & multiline

From Java

Multiline Text View in SwiftUI

Related Related

  1. 1

    Dynamically adding multiline label in a view

  2. 2

    Adding a view dynamically to Layout

  3. 3

    Dynamically adding instances of a view to a TransparentContainer

  4. 4

    Adding Android buttons to view dynamically

  5. 5

    Dynamically adding buttons to scroll view

  6. 6

    Adding hyperlink to dynamically created label in VBA Excel

  7. 7

    Can't autoresize cell in table view with multiline label

  8. 8

    Adding caption label to UImageView in Collection View

  9. 9

    Alignment not right when adding view dynamically to GridLayout

  10. 10

    C# Adding Controls in a Grid view dynamically

  11. 11

    Adding scroll view to linear layout dynamically

  12. 12

    Adding scroll view to linear layout dynamically

  13. 13

    Adding Labels and images dynamically to a custom view in Android

  14. 14

    Dynamically adding Groups and Headers to an Expandable List View

  15. 15

    Adding view dynamically inside template Ember Js

  16. 16

    Multiline label in UIStackView

  17. 17

    multiline TextField - floating label

  18. 18

    Multiline Label On UIButton Swift

  19. 19

    CCLabelTTF multiline label not working

  20. 20

    Adding a dynamically sized view (height) to a UIScrollView with Auto Layout

  21. 21

    Adding a gesture dynamically to an existing table view section header

  22. 22

    Adding DIVs dynamically to table and showing with jQuery and partial view

  23. 23

    Adding View dynamically and setting Parameters and rules is not working properly (android)

  24. 24

    Dynamically adding view or inflating layout to RecyclerView's item

  25. 25

    Dynamically adding data to page with aurelia after the view is rendered

  26. 26

    Cannot show paged table view to when adding text dynamically to a table

  27. 27

    How to make multiline label with Eureka

  28. 28

    QML Label max width & multiline

  29. 29

    Multiline Text View in SwiftUI

HotTag

Archive