简体   繁体   中英

How to make a UILabel “fill up” from the bottom?

I'm trying to make a UILabel "fill up" from the bottom. What I mean by this is (if there's a line break) I'd like to have the longest possible amount on the second line of the label rather than having it on the first line. Here is the default behaviour from my storyboard:

And here is what I'd like to happen:

Is this possible? If so, how would I go about doing this. I'm using swift and have cocoa pods installed, so if you have a suggestion for a framework that allows me to do this I'd be happy to do that, though I haven't been able to find any by searching. Thanks in advance!

Sounds like you might have to write the line splitting function yourself. Work backwards through the string, until you find a space, create a temporary string with your existing words + this new word, measure the string against your window width, if it's too large, put a new line there and start a new line again.

In case you are still struggling with the task, here is my code for the same. Hope it helps :)

Just add the following class to your project.

CustomLabel.h

#import <UIKit/UIKit.h>
@interface CustomLabel : UILabel
@end

CustomLabel.m

#import "CustomLabel.h"

@implementation CustomLabel


- (instancetype)initWithFrame:(CGRect)frame {

    self = [super initWithFrame:frame];

    if (self) {

        self.numberOfLines = 0;
    }

    return self;
}

-(NSString *) getManipulatedString: (NSString *)text {

    NSString *reverseString = [self getReverseString:text];

    NSArray *arrayOfReverseString = [reverseString componentsSeparatedByString:@" "];

    NSString *finalReverseString = @"";

    for (NSString *word in arrayOfReverseString) {

        NSString *tempString = finalReverseString;

        tempString = [tempString stringByAppendingString:[NSString stringWithFormat:@"%@ ", word]];

        CGRect screenRect = [[UIScreen mainScreen] bounds];
        CGFloat screenWidth = screenRect.size.width;

        CGRect labelRect = [tempString boundingRectWithSize:CGSizeMake(screenWidth, 10000)
                                          options:NSStringDrawingUsesLineFragmentOrigin
                                          attributes:@{
                                                       NSFontAttributeName : self.font
                                                       }
                                          context:nil];

        if (labelRect.size.width > self.frame.size.width) {

            finalReverseString = [finalReverseString stringByAppendingString:[NSString stringWithFormat:@"\n%@ ", word]];
        } else {

            finalReverseString = tempString;
        }

    }

    NSArray *arrayOflines = [self getReverseOfArray:[finalReverseString componentsSeparatedByString:@" \n"]];

    NSString *finalString = @"";

    for (NSString *lineString in arrayOflines) {

        NSString *reverse = [self getReverseString:lineString];

        finalString = [finalString stringByAppendingString:[NSString stringWithFormat:@"%@\n",reverse]];
    }

    NSLog(@"%@", finalString);

    return [[finalString stringByTrimmingCharactersInSet:[NSCharacterSet newlineCharacterSet]] stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];

}


- (NSString *)getReverseString:(NSString *)string {

    NSArray *arrayOfWords = [string componentsSeparatedByString:@" "];

    NSString *reverseString = [[self getReverseOfArray:arrayOfWords] componentsJoinedByString:@" "];

    return reverseString;
}

- (NSArray *)getReverseOfArray: (NSArray *)array {

    NSMutableArray *reversed = [NSMutableArray arrayWithCapacity:[array count]];
    NSEnumerator *enumerator = [array reverseObjectEnumerator];
    for (id element in enumerator) {
        [reversed addObject:element];
    }

    return reversed;
}

- (void)setText:(NSString *)text {

    NSString *string = [self getManipulatedString:text];

    [super setText:string];

    [self sizeToFit];
}

@end

And to use it, you just need to add following lines and you are done :)

CustomLabel *label = [[CustomLabel alloc] initWithFrame:CGRectMake(10, 100, 210, 100)];

label.text = @"This is a test for aa next aa aa alignment from bottom tadadada tatadad";

you get the following Output for above code :

在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM