如何防止 UILabel 被“...”切断

发布于 2024-11-03 23:01:59 字数 617 浏览 2 评论 0原文

我想知道是否有任何方法可以防止 UILabel 被“...”切断?我有一个宽 55、高 20 的 CGRect,我希望它在 55 之后简单地切断(或剪掉内容),而不用“...”指示还有更多。

        UILabel *btnTitle = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 55, 20)];
    btnTitle.text = labelMe;
    btnTitle.textColor = [UIColor whiteColor];
    btnTitle.backgroundColor = [UIColor clearColor];
    btnTitle.transform = CGAffineTransformMakeRotation( ( 90 * M_PI ) / 180 );

我通过将 UILabel (宽度增加,即 100 x 20)放入 UIView (55 x 20)并将 ClipsToBounds 设置为 YES 来实现我想要的(即剪辑),结果我无法再单击按钮 -因为我正在使用标签来标记按钮。包含标签的 UIView 隐藏了我的按钮...

有没有办法解决这个问题,而不使用 UIView 来剪辑 UILabel 的内容?

I was wondering if there is any way to prevent an UILabel from cutting off with '...'? I have a CGRect which is 55 in width and 20 in height and I would like it to simply cut off after 55 (or clip the contents off) without indicating with '...' that there is more.

        UILabel *btnTitle = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 55, 20)];
    btnTitle.text = labelMe;
    btnTitle.textColor = [UIColor whiteColor];
    btnTitle.backgroundColor = [UIColor clearColor];
    btnTitle.transform = CGAffineTransformMakeRotation( ( 90 * M_PI ) / 180 );

I achieved what I wanted (i.e. the clipping) by putting the UILabel (with increased width, i.e. 100 x 20) into an UIView (55 x 20) and set clipsToBounds to YES with the result that I couldn't click my buttons anymore - because I was using the label to label a button. The UIView containing the label was hiding my buttons...

Is there a way around this without using an UIView to clip the contents of my UILabel?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

烟酒忠诚 2024-11-10 23:01:59

尝试一下:

label.lineBreakMode = NSLineBreakByClipping;

有关更多信息,请参阅 UILabel 类参考

希望这会有所帮助

Try this out:

label.lineBreakMode = NSLineBreakByClipping;

For more information, refer UILabel Class Reference

Hope this helps

苏佲洛 2024-11-10 23:01:59

使用 UILineBreakModeClip 或其他选项之一。使用 UILabel lineBreakMode 属性进行设置。

Use UILineBreakModeClip or one of the other options. Set it with the UILabel lineBreakMode property.

哽咽笑 2024-11-10 23:01:59

您可以告诉包含标签的视图忽略触摸并将它们发送到下一个可用的响应者,只需将此方法添加到您的 view.m 文件中即可

- (BOOL)pointInside:(CGPoint)point withEvent:(UIEvent *)event{
    return NO;
}

You can tell your view that contains your label to ignore touches and send them to the next available responder to do this just add this method to your view.m file

- (BOOL)pointInside:(CGPoint)point withEvent:(UIEvent *)event{
    return NO;
}
花期渐远 2024-11-10 23:01:59

Swift4 版本的 Ole Begemann/eddyce 的答案:

label.linebreakMode = NSLineBreakMode.byClipping

Swift4 version of Ole Begemann/eddyce's answer:

label.linebreakMode = NSLineBreakMode.byClipping
北方的韩爷 2024-11-10 23:01:59

Ole Begemann 答案的 Swift 5 版本:

label.lineBreakMode = .byClipping

Swift 5 version of Ole Begemann's answer:

label.lineBreakMode = .byClipping
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文