Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
  • How to check if text is truncated in UILabel?

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 3.82k
    Comment on it

    Hello all ! Sometimes it happens that the content is more according to the size of UILabel then the three dots appears at the end of the UILabel. So we want to show the complete text of the UILabel. We can implement it by adding a UIButton, on clicking it the complete text will display.


    Here is an example which implements this :-


    In this example, if the text is equal to or less than 4 lines then the size of UILabel will remain same and there will be no showMore button present. But if the text is greater than 4 lines then three dots will appear with showMore button. When we click on the showMore button then the frame of the UILabel will increase to the size of the text in it and when we click on the button again the UILabel will come to its original frame.


    1. Drag a UILabel and a UIButton from the attributes inspector.
    2. Set very long text in the UILabel.
    3. Set number of lines to 4 of UILabel.
    4. Set the title of UIButton to Show in State coding ->Default and to Hide in State coding ->Selected.
    5. Create the outlet of UILabel and IBAction of the UIButton.

    In the viewDidLoad method of the controller class add the following lines of code :-


    CGSize size = [_lblToDisplayText.text sizeWithAttributes:@{NSFontAttributeName :_lblToDisplayText.font}];    // _lblToDisplayText outlet on UILabel
        if (size.width < _lblToDisplayText.bounds.size.width) {  // check if text is greater than UILabel 
        [self.btnShowMore setHidden:YES];


    Then in the action of the UIButton perform the following lines of code :-


    - (IBAction)btnShowMore:(UIButton *)sender {
       sender.selected = !sender.isSelected;
       if (sender.selected) {
            CGSize size = [_lblToDisplayText.text sizeWithAttributes:@{NSFontAttributeName :_lblToDisplayText.font}];
            [UIView transitionWithView:_lblToDisplayText duration:0.4 options:UIViewAnimationOptionTransitionCrossDissolve  
                                animations:^{_btnShowMore.hidden = NO;} completion:NULL];  // will show the change in the frame of UILabel with animation
                _lblToDisplayText.numberOfLines = 0;
                [_lblToDisplayText sizeToFit];
                _lblToDisplayText.lineBreakMode = NSLineBreakByTruncatingTail;
        else {
           [self.view layoutIfNeeded];
           _lblToDisplayText.numberOfLines = 4; 


    The output will be :


      Click on ShowMore Button   

 0 Comment(s)

Sign In

Sign up using

Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: