OStack程序员社区-中国程序员成长平台

标题: ios - 如何使 TTTAttributedLabel 垂直对齐中心与截断的尾部 [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-13 10:05
标题: ios - 如何使 TTTAttributedLabel 垂直对齐中心与截断的尾部

我正在使用 TTTAttributedLabel显示一些垂直居中的文本(默认在 TTTAttributedLabelUILabel 中),单行(也是默认),并截断尾行中断。

TTTAttributedLabel *label1 = [[TTTAttributedLabel alloc] initWithFrame:CGRectMake(20.0, 40.0, 200.0, 60.0)];
label1.backgroundColor = [UIColor lightGrayColor];
label1.verticalAlignment = TTTAttributedLabelVerticalAlignmentCenter;
[self.view addSubview:label1];

TTTAttributedLabel *label2 = [[TTTAttributedLabel alloc] initWithFrame:CGRectMake(20.0, 120.0, 200.0, 60.0)];
label2.backgroundColor = [UIColor lightGrayColor];
label2.verticalAlignment = TTTAttributedLabelVerticalAlignmentCenter;
[self.view addSubview:label2];

NSString *shortString = @"This is a short string.";
NSString *longString = @"This is a somewhat longer string. In fact its really long. So long it takes up alot of space.";

NSDictionary *attributes = @{NSFontAttributeName: [UIFont systemFontOfSize:16.0]};

NSMutableAttributedString *shortAttributedString = [[NSMutableAttributedString alloc] initWithString:shortString attributes:attributes];
label1.attributedText = shortAttributedString;

NSMutableAttributedString *longAttributedString = [[NSMutableAttributedString alloc] initWithString:longString attributes:attributes];
label2.attributedText = longAttributedString;

以上代码呈现如下:

enter image description here

这两个标签的唯一区别是字符串长度。可以看到,第二个字符串没有垂直居中。



Best Answer-推荐答案


根据提出的类似问题 here ,需要将段落样式lineBreakMode设置为NSLineBreakByTruncatingTail:

TTTAttributedLabel *label1 = [[TTTAttributedLabel alloc] initWithFrame:CGRectMake(20.0, 40.0, 200.0, 60.0)];
label1.backgroundColor = [UIColor lightGrayColor];
label1.verticalAlignment = TTTAttributedLabelVerticalAlignmentCenter;
[self.view addSubview:label1];

TTTAttributedLabel *label2 = [[TTTAttributedLabel alloc] initWithFrame:CGRectMake(20.0, 120.0, 200.0, 60.0)];
label2.backgroundColor = [UIColor lightGrayColor];
label2.verticalAlignment = TTTAttributedLabelVerticalAlignmentCenter;
[self.view addSubview:label2];

NSString *shortString = @"This is a short string.";
NSString *longString = @"This is a somewhat longer string. In fact its really long. So long it takes up alot of space.";

NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
paragraphStyle.lineBreakMode = NSLineBreakByTruncatingTail;

NSDictionary *attributes = @{NSFontAttributeName: [UIFont systemFontOfSize:16.0],
                             NSParagraphStyleAttributeName: paragraphStyle};

NSMutableAttributedString *shortAttributedString = [[NSMutableAttributedString alloc] initWithString:shortString attributes:attributes];
label1.attributedText = shortAttributedString;

NSMutableAttributedString *longAttributedString = [[NSMutableAttributedString alloc] initWithString:longString attributes:attributes];
label2.attributedText = longAttributedString;

关于ios - 如何使 TTTAttributedLabel 垂直对齐中心与截断的尾部,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32175946/






欢迎光临 OStack程序员社区-中国程序员成长平台 (https://ostack.cn/) Powered by Discuz! X3.4