gpt4 book ai didi

ios - 如何从底部制作一个 UILabel "fill up"?

转载 作者:行者123 更新时间:2023-11-28 10:18:37 25 4
gpt4 key购买 nike

我试图让 UILabel 从底部“填满”。我的意思是(如果有换行符)我想在标签的第二行有尽可能长的数量,而不是在第一行。这是我的 Storyboard 中的默认行为:

这是我希望发生的事情:

这可能吗?如果是这样,我将如何去做这件事。我正在使用 swift 并安装了 cocoa pods,所以如果您对允许我执行此操作的框架有建议,我很乐意这样做,尽管我无法通过搜索找到任何建议。提前致谢!

最佳答案

如果您仍在为这项任务而苦苦挣扎,这里是我的代码。希望对您有所帮助:)

只需将以下类添加到您的项目中即可。

自定义标签.h

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

自定义标签.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

关于ios - 如何从底部制作一个 UILabel "fill up"?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37939908/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com