我想采用标准UILabel并增加字体大小以填充垂直空间.从这个问题的接受答案中汲取灵感,我使用这个drawRect实现在UILabel上定义了一个子类:
- (void)drawRect:(CGRect)rect
{
// Size required to render string
CGSize size = [self.text sizeWithFont:self.font];
// For current font point size, calculate points per pixel
float pointsPerPixel = size.height / self.font.pointSize;
// Scale up point size for the height of the label
float desiredPointSize = rect.size.height * pointsPerPixel;
// Create and assign new UIFont with desired point Size
UIFont *newFont = [UIFont fontWithName:self.font.fontName size:desiredPointSize];
self.font = newFont;
// Call super
[super drawRect:rect];
}
Run Code Online (Sandbox Code Playgroud)
但这不起作用,因为它将字体缩放到标签底部之外.如果你想复制它,我开始使用标签289x122(wxh),Helvetica作为字体,起始点大小为60,适合标签.以下是标准UILabel和我的子类使用字符串"{Hg"的示例输出:


我看过字体下降器和上升器,尝试按照不同的组合考虑这些,但仍然没有任何运气.任何想法,这是否与具有不同下降和上升长度的不同字体有关?