• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

objective-c - UIImageView 文本中的 UILabel 被截断

[复制链接]
菜鸟教程小白 发表于 2022-12-12 22:57:04 | 显示全部楼层 |阅读模式 打印 上一主题 下一主题

我将 UILabel 放在自定义 UIImageView 中,文本被截断:

screenshot

代码:

//self.frame.size.width and height are the same value, self is a square
self.displayNameLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, (self.frame.size.width - 12), (self.frame.size.height - 12))];
self.displayNameLabel.center = CGPointMake((self.frame.size.width / 2), (self.frame.size.height / 2));
self.displayNameLabel.backgroundColor = [UIColor whiteColor];
self.displayNameLabel.textAlignment = UITextAlignmentCenter;
self.displayNameLabel.font = [UIFont fontWithName:labelFont size:40];
self.displayNameLabel.adjustsFontSizeToFitWidth = YES;

我将字体大小设置为大,然后使用 adjustsFontSizeToFitWidth以确保它尽可能大。它指出:

If this property is set to YES, however, and the text in the text property exceeds the label’s bounding rectangle, the receiver starts reducing the font size until the string fits

如果我将 label.frame 设置为:

self.displayNameLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, (self.frame.size.width - 0), (self.frame.size.height - 0))];

然后它按预期工作。

如何将框架设置为:

self.frame.size.width
self.frame.size.height 

代替:

self.frame.size.width - 14 
self.frame.size.height - 14

工作?

为什么会被夹在上面?



Best Answer-推荐答案


adjustsFontSizeToFitWidth 属性只会导致标签调整字体大小,直到字符串的宽度适合框架。如果标签的高度不足以容纳最终的字体大小,它将被剪裁。

关于objective-c - UIImageView 文本中的 UILabel 被截断,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12710980/

回复

使用道具 举报

懒得打字嘛,点击右侧快捷回复 【右侧内容,后台自定义】
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

关注0

粉丝2

帖子830918

发布主题
阅读排行 更多
广告位

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap