Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UILabel visible part of text

Tags:

uilabel

iphone

Is there a way to get the visible part of text in word wrapped UILabel? I mean exactly the last visible character?

I'd like to make two labels rounding the image and would like to continue the text which was out of rect for first label on the second one.

I know [NSString sizeWithFont...] but are there something reversing like [NSString stringVisibleInRect: withFont:...] ? :-)

Thank you in advance.

like image 528
Evgeny Avatar asked Nov 04 '10 19:11

Evgeny


People also ask

How do I know if my UILabel is truncated?

With a UILabel you can check for truncation by calculating the size of the text within the label and comparing that with the bounds of the label itself. If the text extends beyond the bounds then it's being displayed as truncated.

Is UILabel a view?

A view that displays one or more lines of informational text.

What is label in iOS?

Label in iOS is a basic UI control that is used to display plain static text or styled text. The content in iOS label control is a read-only text we cannot able change or edit the text but we can copy the content of label.

What is label in Swift?

Label is a user interface item in SwiftUI which enables you to display a combination of an image (icon, SF Symbol or other) and a text label in a single UI element.


1 Answers

You could use a category to extend NSString and create the method you mention

@interface NSString (visibleText)

- (NSString*)stringVisibleInRect:(CGRect)rect withFont:(UIFont*)font;

@end

@implementation NSString (visibleText)

- (NSString*)stringVisibleInRect:(CGRect)rect withFont:(UIFont*)font
{
    NSString *visibleString = @"";
    for (int i = 1; i <= self.length; i++)
    {
        NSString *testString = [self substringToIndex:i];
        CGSize stringSize = [testString sizeWithFont:font];
        if (stringSize.height > rect.size.height || stringSize.width > rect.size.width)
            break;

        visibleString = testString;
    }
    return visibleString;
}

@end
like image 189
Vertism Avatar answered Sep 29 '22 18:09

Vertism