我正在尝试使用自动布局制作一个带有两个标签、一个进度条和一个要取消的 X 的小通知框。我的标签设置与字体缩放相同,但看起来有些标签被切断了。我不确定这是我的自动布局代码还是我的 UILabel 代码的结果。
您可以看到文本根本没有对齐,并且 Playing in 标签被完全切断。
相关代码:
// Container
pttProgressContainer = [UIView new];
[pttProgressContainer setBackgroundColor:UIColorFromRGBWithAlpha(0x3a4362, 0.10)];
[pttProgressContainer setTranslatesAutoresizingMaskIntoConstraints:NO];
[pttContainer addSubview:pttProgressContainer];
// Separator
UIView *progressContainer1pxView = [UIView new];
[progressContainer1pxView setBackgroundColor:[UIColor whiteColor]];
[progressContainer1pxView setTranslatesAutoresizingMaskIntoConstraints:NO];
[pttProgressContainer addSubview:progressContainer1pxView];
// Progress viewzlers
UIView *progressViewLeft = [UIView new];
[progressViewLeft setTranslatesAutoresizingMaskIntoConstraints:NO];
[pttProgressContainer addSubview:progressViewLeft];
UIView *progressViewRight = [UIView new];
[progressViewRight setTranslatesAutoresizingMaskIntoConstraints:NO];
[pttProgressContainer addSubview:progressViewRight];
// Progress view
pttProgressView = [[UIProgressView alloc] initWithProgressViewStyle:UIProgressViewStyleDefault];
[pttProgressView setTranslatesAutoresizingMaskIntoConstraints:NO];
[pttProgressView setProgress:0.5 animated:NO];
[pttProgressView setTrackTintColor:[UIColor colorWithRed:1.0 green:1.0 blue:1.0 alpha:0.20]];
[pttProgressView setProgressTintColor:UIColorFromRGB(0xd7d7d7)];
[[pttProgressView layer] setCornerRadius:5];
[[pttProgressView layer] setMasksToBounds:YES];
[pttProgressView setClipsToBounds:YES];
[progressViewLeft addSubview:pttProgressView];
// Now the playing in label countdown
UILabel *pttPlayingInLabel = [UILabel new];
[pttPlayingInLabel setFont:[UIFont systemFontOfSize:20]];
[pttPlayingInLabel setAdjustsFontSizeToFitWidth:YES];
[pttPlayingInLabel setMinimumScaleFactor:8/20];
[pttPlayingInLabel setTextAlignment:NSTextAlignmentLeft];
[pttPlayingInLabel setTextColor:[UIColor whiteColor]];
[pttPlayingInLabel setContentMode:UIViewContentModeCenter];
[pttPlayingInLabel setTranslatesAutoresizingMaskIntoConstraints:NO];
[pttPlayingInLabel setText:@"Playing In..."];
[progressViewLeft addSubview:pttPlayingInLabel];
// Countdown numbers
pttPlayingCountdownLabel = [UILabel new];
[pttPlayingCountdownLabel setFont:[UIFont systemFontOfSize:20]];
[pttPlayingCountdownLabel setMinimumScaleFactor:8/20];
[pttPlayingCountdownLabel setAdjustsFontSizeToFitWidth:YES];
[pttPlayingCountdownLabel setTextAlignment:NSTextAlignmentRight];
[pttPlayingCountdownLabel setContentMode:UIViewContentModeCenter];
[pttPlayingCountdownLabel setTranslatesAutoresizingMaskIntoConstraints:NO];
[pttPlayingCountdownLabel setTextColor:[UIColor whiteColor]];
[pttPlayingCountdownLabel setText:@"5"];
[progressViewLeft addSubview:pttPlayingCountdownLabel];
// The button del cancel
pttCancelPlayingButton = [UIButton buttonWithType:UIButtonTypeCustom];
[pttCancelPlayingButton setTranslatesAutoresizingMaskIntoConstraints:NO];
[pttCancelPlayingButton setBackgroundImage:[UIImage imageNamed:@"ptt-button-delay-cancel.png"] forState:UIControlStateNormal];
// [pttCancelPlayingButton setBackgroundColor:[UIColor brownColor]];
// [pttCancelPlayingButton setTitle:@"C" forState:UIControlStateNormal];
[progressViewRight addSubview:pttCancelPlayingButton];
// Progress danker
[pttProgressContainer addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|[progressContainer1pxView(==1)]-[progressViewLeft]-|"
options:0
metrics:metrics
views:views]];
/*
[pttProgressContainer addConstraint:[NSLayoutConstraint constraintWithItem:progressViewRight
attribute:NSLayoutAttributeCenterY
relatedBy:NSLayoutRelationEqual
toItem:progressViewLeft
attribute:NSLayoutAttributeCenterY
multiplier:1.0
constant:0]];
[pttProgressContainer addConstraint:[NSLayoutConstraint constraintWithItem:progressViewRight
attribute:NSLayoutAttributeHeight
relatedBy:NSLayoutRelationEqual
toItem:progressViewLeft
attribute:NSLayoutAttributeHeight
multiplier:1.0
constant:0]];
*/
// Visual horizontal
[pttProgressContainer addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"|[progressContainer1pxView]|"
options:NSLayoutFormatAlignAllTop | NSLayoutFormatAlignAllBottom
metrics:metrics
views:views]];
[pttProgressContainer addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"|-[progressViewLeft]-[progressViewRight(==40)]-|"
options:NSLayoutFormatAlignAllBottom | NSLayoutFormatAlignAllTop
metrics:metrics
views:views]];
// Inner danker
[progressViewLeft addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|-[pttPlayingInLabel]-[pttProgressView(==3)]-|"
options:NSLayoutFormatAlignAllLeading
metrics:metrics
views:views]];
/*
[progressViewLeft addConstraint:[NSLayoutConstraint constraintWithItem:pttPlayingCountdownLabel
attribute:NSLayoutAttributeCenterY
relatedBy:NSLayoutRelationEqual
toItem:pttPlayingInLabel
attribute:NSLayoutAttributeCenterY
multiplier:1.0
constant:0]];
[progressViewLeft addConstraint:[NSLayoutConstraint constraintWithItem:pttPlayingCountdownLabel
attribute:NSLayoutAttributeHeight
relatedBy:NSLayoutRelationEqual
toItem:pttPlayingInLabel
attribute:NSLayoutAttributeHeight
multiplier:1.0
constant:0]];
*/
// Visual horizontal
[progressViewLeft addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"|-[pttPlayingInLabel]-[pttPlayingCountdownLabel(==20)]-|"
options:NSLayoutFormatAlignAllBaseline
metrics:metrics
views:views]];
[progressViewLeft addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"|-[pttProgressView]-|"
options:0
metrics:metrics
views:views]];
// Now for the inner danker right
[progressViewRight addConstraint:[NSLayoutConstraint constraintWithItem:pttCancelPlayingButton
attribute:NSLayoutAttributeHeight
relatedBy:NSLayoutRelationEqual
toItem:nil
attribute:NSLayoutAttributeNotAnAttribute
multiplier:1
constant:21.0]];
[progressViewRight addConstraint:[NSLayoutConstraint constraintWithItem:pttCancelPlayingButton
attribute:NSLayoutAttributeWidth
relatedBy:NSLayoutRelationEqual
toItem:nil
attribute:NSLayoutAttributeNotAnAttribute
multiplier:1
constant:21.0]];
[progressViewRight addConstraint:[NSLayoutConstraint constraintWithItem:pttCancelPlayingButton
attribute:NSLayoutAttributeCenterX
relatedBy:NSLayoutRelationEqual
toItem:progressViewRight
attribute:NSLayoutAttributeCenterX
multiplier:1
constant:0]];
[progressViewRight addConstraint:[NSLayoutConstraint constraintWithItem:pttCancelPlayingButton
attribute:NSLayoutAttributeCenterY
relatedBy:NSLayoutRelationEqual
toItem:progressViewRight
attribute:NSLayoutAttributeCenterY
multiplier:1
constant:0]];