Non-ellipsis multiline text

I am trying to make ellipse text on several lines inside a rectangle.

For NSLineBreakByTruncatingTail , the documentation states

The line is displayed so that the beginning is inserted into the container, and the missing text at the end of the line is indicated by an ellipse symbol. Although this mode works for multi-line text, it is more often used for single-line text.

but in this mode I get only one line:

With NSLineBreakByTruncatingTail

However, with NSLineBreakByWordWrapping I do not get ellipsis for overlapping text:

With NSLineBreakByWordWrapping

Both images use the same code below (red background is a rectangle for drawing text) and, of course, the same size of the rectangle, so 2 lines should definitely match.

 NSMutableParagraphStyle* paragraphStyle = [[NSParagraphStyle defaultParagraphStyle] mutableCopy]; paragraphStyle.lineBreakMode = <<see above>>; paragraphStyle.alignment = NSTextAlignmentNatural; NSDictionary* drawingAttributes = [NSDictionary dictionaryWithObjectsAndKeys: [UIFont fontWithName:@"HelveticaNeue" size:36], NSFontAttributeName, paragraphStyle, NSParagraphStyleAttributeName, nil]; const CGRect rect = CGRectMake(...); [@"I do not get ellipsized in any way" drawInRect:rect withAttributes:drawingAttributes]; 

Is there a way to combine ellipsing with multi-line rendering, as the documentation says? With UILabel, I would only need to set the number of lines to something other than 1, but what about text rendering through code?

+6
source share
1 answer

I don’t understand why you are using NSMutableParagraphStyle and drawInRect, maybe I need some additional information about the context you are trying to work with.

But using this simple technique:

 UILabel *label = [[UILabel alloc] initWithFrame:(CGRect){0,0,300,100}]; label.numberOfLines = 0; label.backgroundColor = [UIColor redColor]; label.center = self.view.center; label.font = [UIFont fontWithName:@"HelveticaNeue" size:36]; label.textAlignment = NSTextAlignmentNatural; label.lineBreakMode = NSLineBreakByTruncatingTail; label.text = @"I do not get ellipsized in any way"; [self.view addSubview:label]; 

You will see the following result: enter image description here

My suggestion is to get rid of drawInRect in this case.

+1
source

All Articles