2016-06-21 59 views
0

我想從底部做一個UILabel「填滿」。我的意思是(如果有換行符),我想在標籤的第二行有最長的數量,而不是在第一行。這裏是我的故事板的默認行爲:如何使UILabel從底部「填滿」?

,這裏是我想發生什麼:

這可能嗎?如果是這樣,我將如何去做這件事。我使用的是swift並安裝了可可豆莢,所以如果你有一個允許我這樣做的框架建議,我會很樂意這樣做,儘管我沒有通過搜索找到任何東西。提前致謝!

回答

2

如果你還在努力完成任務,這裏是我的代碼。希望它有助於:)

只需將以下類添加到您的項目。

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 

,並使用它,你只需要添加以下行,和你做:)

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"; 

你獲得以下代碼的輸出:

enter image description here

+0

謝謝Pooja!你是最好的! CustomLabel.h的代碼是什麼? –

+0

沒有在CustomLabel.h中添加額外的東西 –

+0

好的,謝謝!我不是很瞭解Objective-C,所以我不確定在那裏做什麼。我知道如何使用橋接頭,謝天謝地! –

2

聽起來像你可能必須自己寫行分割功能。 向後翻遍字符串,直到找到空格,用現有單詞+這個新單詞創建一個臨時字符串,根據您的窗口寬度測量字符串,如果它太大,則在該處添加新行並再次開始新行。

+0

:-(猜我會試試看 –