【问题标题】:Can't make UILabel clip无法制作 UILabel 剪辑
【发布时间】:2012-01-19 05:05:12
【问题描述】:

我已经调整了字符串的大小以最大限度地适应高度(1 行),这样任何过大的宽度都会被截断(如在中间字形中),这是 lineBreakMode:UILineBreakModeClip 应该做的。相反,文本似乎通过仅绘制适合(不期望或不期望)的完整字符来截断(?)。基本上:

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

【问题讨论】:

    标签: ios


    【解决方案1】:

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

    说明:假设您希望标签在 100 像素处被截断。将 UILabel 设置为 200 的宽度,它会愉快地渲染你想要的 100 像素。它是否在 200 处截断完整的字母,你不在乎。您将此标签添加到设置了 clipsToBounds 的 100px 的 UIView 中,它只会向您显示所需的 100px 标签,然后在字形中间将其切断。

    改编自另一篇文章,以说明:

    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];
    

    【讨论】:

      【解决方案2】:

      尝试使用:

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

      得到你想要的效果。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-02-22
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-09-12
        • 1970-01-01
        相关资源
        最近更新 更多