0

我需要向 ViewController 添加可变数量的 UIView;UIView 包含一个使用 CoreText 绘制的字符串。我目前正在通过执行以下操作添加单个 UIView:

    CGRect frame;
    frame.origin.x = x;
    frame.origin.y = y;
    frame.size.width = 100;
    frame.size.height = 100;

    CTLabelView *label=[[CTLabelView alloc]  initWithFrame:frame];
    [label setLabelText:@"my string"];

我想避免指定框架,让标签根据它的长度占据它需要的空间。避免指定宽度和高度的最佳方法是什么?

4

1 回答 1

2

您可以获得指定字体的字符串大小。

// get your font
UIFont *font = label.font;

// Find width of a string ...
CGSize size = [@"my string" sizeWithFont:font];
于 2012-12-08T22:52:40.937 回答