UILabel sizeWithFont:问题。剪切斜体文本

发布于 2024-09-14 01:15:46 字数 1176 浏览 3 评论 0原文

我创建了一个显示单个大字符的 UILabel。即使使用 ClipsToBounds = NO;我仍然得到剪辑。

请参阅链接: http://img341.imageshack.us/img341/5310/screenshot20100814at243.png

我使用了以下代码:

 CGSize fBounds = [myLabel.text sizeWithFont:cFont];

获取字体的边框。标签的绘制方式如下:

myLabel = [[UILabel alloc] initWithFrame:CGRectMake(20, 20, 280, 300)];
 myLabel.clipsToBounds = NO;
 myLabel.numberOfLines = 1;
 myLabel.textAlignment = UITextAlignmentCenter;
 myLabel.adjustsFontSizeToFitWidth = YES;
 myLabel.minimumFontSize = 10;
 myLabel.text = @"A";
 myLabel.font = [UIFont fontWithName:@"CourierNewPSMT" size:300];
 myLabel.textColor = [UIColor blackColor];
 myLabel.backgroundColor = [UIColor colorWithRed:1 green:.5 blue:0 alpha:.5];

在下图中,从 sizeWithFont 返回的大小由半透明的蓝色矩形覆盖层呈现。正如您所看到的,使用斜体字体(在本例中为 Verdana-BoldItalic),字符会超出 sizeWithFont 返回的内容。此外,UILabel 的框架(橙色)也会剪辑角色。想法?也许我可以重写一些文本绘制例程。另外,不确定这是否与此处的问题相同:

UIButton.titleLabel 剪切文本问题

I have created a UILabel that displays a single large character. Even with clipsToBounds = NO; I still get clipping.

See link: http://img341.imageshack.us/img341/5310/screenshot20100814at243.png

I used the following code:

 CGSize fBounds = [myLabel.text sizeWithFont:cFont];

To get what should be the bounding rectangle of the font. And the label is drawn with:

myLabel = [[UILabel alloc] initWithFrame:CGRectMake(20, 20, 280, 300)];
 myLabel.clipsToBounds = NO;
 myLabel.numberOfLines = 1;
 myLabel.textAlignment = UITextAlignmentCenter;
 myLabel.adjustsFontSizeToFitWidth = YES;
 myLabel.minimumFontSize = 10;
 myLabel.text = @"A";
 myLabel.font = [UIFont fontWithName:@"CourierNewPSMT" size:300];
 myLabel.textColor = [UIColor blackColor];
 myLabel.backgroundColor = [UIColor colorWithRed:1 green:.5 blue:0 alpha:.5];

In the image below, the size returned from sizeWithFont is rendered by the semi-transparent blue rectangle overlay. As you can see, with an italic font (in this case Verdana-BoldItalic), the character extends past what sizeWithFont returns. Further, the UILabel's frame (the orange color) also clips the character. Thoughts? Maybe I could override some text drawing routine. Also, not sure if this is the same problem as here:

UIButton.titleLabel clipping text problem

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

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

发布评论

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

评论(2

↘人皮目录ツ 2024-09-21 01:15:46

使用属性文本+缩进...

Use attributed text + indent...

小帐篷 2024-09-21 01:15:46

看来这是苹果的问题。最终使用 CoreText 进行自定义绘图。

Looks like this is an apple problem. Ended up doing custom drawing with CoreText.

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