简体   繁体   English

根据UILabel框架大小动态确定字体大小

[英]Dynamically determine size of font based on UILabel frame size

I am trying to solve a problem that i've looked around for but can't figure out. 我正在尝试解决一个我一直在寻找但无法解决的问题。 I have a UILabel control inside of a UIScrollView control. 我在UIScrollView控件内部有一个UILabel控件。 I have set the height of the label to the height of the scrollview in which it is contained, now I need to determine the correct height (dynamically) of text that will fill the label. 我已经将标签的高度设置为包含标签的scrollview的高度,现在我需要(动态)确定将填充标签的文本的正确高度。 So whenever the scrollView height is, the text height inside of the label will match and the width will expand to prevent the text from truncating (like the image below). 因此,无论何时scrollView高度为高,标签内部的文本高度都将匹配,并且宽度将扩大以防止文本被截断(如下面的图像)。

I've tried things like sizeToFit , constrainedSize , but i couldn't get it to work properly, any direction on how to achieve something like this? 我已经尝试过诸如sizeToFitconstrainedSize类的东西,但是我无法使其正常工作,如何实现这样的目标有何方向?

在此处输入图片说明

Try this simple code,it's helps you 试试这个简单的代码,它可以帮助您

 UILabel *label=[[UILabel alloc]initWithFrame:CGRectMake(0, 0, 100, 100)];
    label.backgroundColor=[UIColor clearColor];
    label.text= yourString;
    label.numberOfLines=0;
    label.adjustsFontSizeToFitWidth=YES;
    label.minimumFontSize=6;
    label.textAlignment=UITextAlignmentCenter;
    [self.View addSubview:label];

    CGFloat fontSize = 20;
    while (fontSize > 0.0)
    {
        CGSize size = [yourString sizeWithFont:[UIFont fontWithName:@"Rockwell-Bold" size:fontSize] constrainedToSize:CGSizeMake(label.frame.size.width, 10000) lineBreakMode:UILineBreakModeWordWrap];

        if (size.height <= label.frame.size.height) break;

        fontSize -= 1.0;
    }

    //set font size
    label.font = [UIFont fontWithName:@"Rockwell-Bold" size:fontSize];
    [label release];

adjustsFontSizeToFitWidth

在Interface Builder中,它是“调整以适合”复选框。

I noticed you want to make the text fill the full width and the full height. 我注意到您想使文本填充整个宽度和整个高度。 It is probably difficult to talk the text into doing that. 谈论文本可能很难做到这一点。 There is no way to set the exact bounding rectangle of text directly. 无法直接设置文本的确切边界矩形。 You can only set the font as a point size; 您只能将字体设置为磅值。 it will scale the text with more or less a fixed aspect ratio. 它将以固定的纵横比缩放文本。

You should be able to achieve what you want with the label's transform property. 您应该能够通过标签的transform属性实现所需的功能。 First, build a label of a fixed size and choose a font so that the text fills the frame. 首先,建立固定大小的标签并选择一种字体,以使文本填充框架。 (If your text will vary, it's a little more complicated. Use [text sizeWithFont:font] to get the right size, and set the label frame from that.) (如果您的文本会有所不同,则会稍微复杂一些。请使用[text sizeWithFont:font]来获得正确的大小,然后从中设置标签框架。)

Say your label size does not match your scroll view size. 假设标签尺寸与滚动视图尺寸不匹配。 Eg the label is 100x10 and the scrollview is 200x15. 例如,标签为100x10,滚动视图为200x15。 If you set your label's frame to 200x15, you will not get the effect you want. 如果将标签的框架设置为200x15,则不会获得想要的效果。 The label will get bigger but the text inside it will stay the same size. 标签将变大,但其中的文本将保持相同大小。 However, if you do this 但是,如果您这样做

label.transform = CGAffineTransformMakeScale(2.0, 1.5);

then your whole label and its contents will get stretched. 那么您的整个标签及其内容将被拉伸。 You might also need to adjust the label's center property to make it positioned correctly within the parent view. 您可能还需要调整标签的center属性,以使其在父视图中正确定位。 You will need to have your code do some calculations to get this right every time. 您将需要让您的代码进行一些计算才能每次都正确。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM