无法制作UILabel剪辑

问题描述:

我调整了我的字符串以最大化高度拟合(1行),以便截断任何过多宽度​​(如在中间字形中)lineBreakMode:UILineBreakModeClip应该做的事情。相反,文本似乎通过只绘制适合的完整字符(不是预期的或期望的)来截断(?)。基本上是:无法制作UILabel剪辑

[label setNumberOfLines:1]; 
label.font = [UIFont systemFontOfSize:64.0f]; 
label.lineBreakMode = UILineBreakModeClip; 
[label setText:@"ABCDEIIIIII"]; 

A related, more complicated question给出了一个解决方案:创建一个宽度较大的标签并将其添加到所需宽度的UIView中。 (然后将UIView添加到您的应用程序中。)设置UIView的clipsToBounds属性将为您提供所需的中间字形剪辑。

说明:假设您希望标签在100px处截断。将UILabel设置为200的宽度,它将愉快地呈现所需的100像素。无论是在200的全部字母截断,你不在乎。您可以使用clipsToBounds设置将此标签添加到100px的UIView中,并且只会显示标签所需的100像素,然后在中间字形上将其剪下。

从其他后改编,来说明:

UIView *topContainer = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 100, 50)]; 
topContainer.clipsToBounds = YES; 
topContainer.opaque = NO; 
topContainer.backgroundColor = [UIColor clearColor]; 

UILabel *topLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 200, 50)]; 
[topLabel setText:@"ABCDEIIIIII"]; 
topLabel.opaque = NO; 

[topContainer addSubview:topLabel]; 
[self.view addSubview:topContainer]; 

尝试使用:

label.clipsToBounds = YES; 
label.layer.masksToBounds = YES; 

得到你想要的效果。