UIButton multiline tail truncated text

I found similar questions that ask how to have multi-line text on UIButton, and the solution is to install

[myUIButton.titleLabel setLineBreakMode:UILineBreakModeWordWrap];
[myUIButton setTitle:myTitle forState:UIControlStateNormal];

However, this causes the button title to take up many lines. I tried to limit the number of rows using

[myUIButton.titleLabel setNumberOfLines:2];

but this does not affect the resulting row count.

Is there a way to limit the line word enclosed in 2 lines in the UIButton header and then truncate the tail with "..."?

+5
source share
3 answers

lineBreakMode numberOfLines, ... , lineBreakMode, , numberOfLines, .

Objective-C:

[button.titleLabel setLineBreakMode: UILineBreakModeTailTruncation];
[button.titleLabel setNumberOfLines:2];
[button setTitle:myTitle forState:UIControlStateNormal];

Swift 3: Xcode 6 UILineBreakMode NSLineBreakMode

button.titleLabel?.lineBreakMode = NSLineBreakMode.byTruncatingTail
button.titleLabel?.numberOfLines = 2
button.setTitle(myTitle, for: UIControlState.normal)
+3

, , , , . .

, , :

// Set the line break mode to word wrap so it won't truncate automatically
[button.titleLabel setLineBreakMode: UILineBreakModeWordWrap];

// Call a method that truncates the string I want to use
[button setTitle:[self truncateString:myButtonText] forState:UIControlStateNormal];

truncateString:

- (NSString *)truncateString:(NSString *)stringToTruncate
{
    if ([stringToTruncate length] > 50)
        stringToTruncate = [[stringToTruncate substringToIndex:50] stringByAppendingString:@"..."];

    return  stringToTruncate;
}

, , , , , "..." . , , , , , .

+2
[button.titleLabel setNumberOfLines:2];
[button.titleLabel setLineBreakMode: UILineBreakModeTailTruncation];
[button setTitle:myTitle forState:UIControlStateNormal];

he does it for me !: D hooray !!

+1
source

All Articles