【问题标题】:iOS. Method adjustsFontSizeToFitWidth doesn't work properlyIOS。方法 adjustsFontSizeToFitWidth 不能正常工作
【发布时间】:2011-11-03 07:06:38
【问题描述】:

我需要用一些文字填写一个矩形。 rect的大小是固定的。

我使用以下函数:

- (void)drawText:(NSString *)text onView:(UIView *)view inRect:(CGRect)rect
{
    UILabel *lbl = [[UILabel alloc] initWithFrame:rect];
    lbl.minimumFontSize = 1;
    lbl.font = [UIFont systemFontOfSize:30];
    [lbl setText:text];
    lbl.adjustsFontSizeToFitWidth = YES;
    [view addSubview:lbl];
    [lbl release];
}

然后我写类似这样的东西:

[self drawText:@"Asdfghjkl;" onView:view inRect:CGRectMake(45, 40, 85, 13)];
[self drawText:@"qwertyui" onView:view inRect:CGRectMake(173, 31, 126, 22)];

我得到了剪切的文本。我的错在哪里?

【问题讨论】:

    标签: ios text fonts size uilabel


    【解决方案1】:

    正如 Gargolev 所说,它会调整以适应宽度

    因此,您应该选择正确的字体大小以适应您提供的高度。

    要在运行时执行此操作而不事先了解您要使用的字体,一个简单的解决方案是:

    UILabel *label = [[UILabel alloc] initWithFrame:myView.bounds];
    label.font = [UIFont fontWithName:@"Helvetica" size:1.f];
    float fontAspectFactor = 1.f/label.font.lineHeight;
    label.font = [UIFont fontWithName:@"Helvetica" size:myView.height*fontAspectFactor];
    [myView addSubview:label];
    

    【讨论】:

      【解决方案2】:

      我认为主要的问题是某些文本只适合宽度,但它必须同时适合宽度和高度。 所以对我来说最快的解决方案是制作一个更大的透明 UILabel 并使用对齐在其中放置一个文本

      【讨论】:

        猜你喜欢
        • 2017-01-08
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-08-01
        • 2012-11-08
        • 1970-01-01
        • 1970-01-01
        • 2015-06-29
        相关资源
        最近更新 更多