我在对齐两个标签时遇到问题。两个例子来说明问题
示例 1(确定)
[leftLabel setText:@"03"];
[rightLabel setText:@"Description3"];
示例 2(挪威克朗)
[leftLabel setText:@"03"];
[rightLabel setAttributedText:[[NSAttributedString alloc] initWithString:@"Description3"]];
在这两个示例中,布局约束都是这样
[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-bigMargin-[leftLabel]-bigMargin-[rightLabel]-bigMargin-|"
options:NSLayoutFormatAlignAllBaseline
metrics:metrics
views:views];
问题是正确的标签,当文本是一个属性时,它被绘制在下面一点,如图所示,并且对齐结果错误。
为什么?我可以使用UIlabel
这两种方法来解决这个问题吗?
编辑:
我在 GitHub 中创建了一个项目,并对此进行了测试。这里的问题是,即使没有 NSAttributdString,我也遇到了问题!看标签上的数字,是不是与描述和金额正确对齐。
我在这里粘贴了单元格的代码,但必须在项目中看到整个场景。
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
UIView *contentView = [self contentView];
[contentView setBackgroundColor:[UIColor clearColor]];
dayLabel_ = [[UILabel alloc] initWithFrame:CGRectZero];
[dayLabel_ setTranslatesAutoresizingMaskIntoConstraints:NO];
[contentView addSubview:dayLabel_];
monthLabel_ = [[UILabel alloc] initWithFrame:CGRectZero];
[monthLabel_ setTranslatesAutoresizingMaskIntoConstraints:NO];
[monthLabel_ setFont:[UIFont boldSystemFontOfSize:13.0f]];
[contentView addSubview:monthLabel_];
descriptionLabel_ = [[UILabel alloc] initWithFrame:CGRectZero];
[descriptionLabel_ setTranslatesAutoresizingMaskIntoConstraints:NO];
[descriptionLabel_ setFont:[UIFont systemFontOfSize:20.0f]];
[contentView addSubview:descriptionLabel_];
conceptLabel_ = [[UILabel alloc] initWithFrame:CGRectZero];
[conceptLabel_ setTranslatesAutoresizingMaskIntoConstraints:NO];
[conceptLabel_ setLineBreakMode:NSLineBreakByTruncatingTail];
[conceptLabel_ setFont:[UIFont systemFontOfSize:12.0f]];
[contentView addSubview:conceptLabel_];
amountLabel_ = [[UILabel alloc] initWithFrame:CGRectZero];
[amountLabel_ setTranslatesAutoresizingMaskIntoConstraints:NO];
[contentView addSubview:amountLabel_];
// Constraints
NSDictionary *views = NSDictionaryOfVariableBindings(contentView, dayLabel_, monthLabel_, descriptionLabel_, conceptLabel_, amountLabel_);
NSDictionary *metrics = @{ @"bigMargin" : @12 };
[descriptionLabel_ setContentCompressionResistancePriority:UILayoutPriorityDefaultLow forAxis:UILayoutConstraintAxisHorizontal];
[conceptLabel_ setContentCompressionResistancePriority:UILayoutPriorityDefaultLow forAxis:UILayoutConstraintAxisHorizontal];
[contentView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|-bigMargin-[dayLabel_][monthLabel_]"
options:NSLayoutFormatAlignAllLeading
metrics:metrics
views:views]];
[contentView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|-bigMargin-[descriptionLabel_][conceptLabel_]"
options:NSLayoutFormatAlignAllLeading
metrics:metrics
views:views]];
[contentView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-bigMargin-[dayLabel_]-bigMargin-[descriptionLabel_]-(>=bigMargin)-[amountLabel_]-bigMargin-|"
options:NSLayoutFormatAlignAllBaseline
metrics:metrics
views:views]];
[contentView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-bigMargin-[monthLabel_]-bigMargin-[conceptLabel_]-bigMargin-|"
options:NSLayoutFormatAlignAllBaseline
metrics:metrics
views:views]];
}
return self;
}