相关疑难解决方法(0)

缩放字体大小以在UILabel中垂直放置

我想采用标准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"的示例输出:

的UILabel

uilabel子类

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

fonts cocoa-touch uilabel ios

19
推荐指数
1
解决办法
6419
查看次数

标签 统计

cocoa-touch ×1

fonts ×1

ios ×1

uilabel ×1