UILabel sizeWithFont:问题。剪切斜体文本
我创建了一个显示单个大字符的 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 的框架(橙色)也会剪辑角色。想法?也许我可以重写一些文本绘制例程。另外,不确定这是否与此处的问题相同:
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:
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
使用属性文本+缩进...
Use attributed text + indent...
看来这是苹果的问题。最终使用 CoreText 进行自定义绘图。
Looks like this is an apple problem. Ended up doing custom drawing with CoreText.