2

我一直在插话,我需要使用 sizeWithFont: 方法来正确排列我的布局,但它似乎不适用于自定义字体。有没有办法让它工作,或者我可以使用另一种方法?

我对此感到很困惑。任何帮助表示赞赏。

4

2 回答 2

1

sizeWithFont 肯定可以工作,但如果它给您带来问题,那么有一个解决方法。您可以将文本放在 UITextView 中,将其添加到布局中,然后检索实际文本大小并进行相应调整。这是我的一个项目中的一些示例代码:

     // Put in the title in
    self.titleView = [[UITextView alloc] initWithFrame:CGRectMake(0, 0, 195, 195)];
    self.titleView.font = [UIFont fontWithName:@"Bliss Pro" size:20.0];
    [self addSubview:self.titleView];

    self.titleView.text = @"Add your text here";

    // Now get the actual size of the text and resize the title view's frame
    CGRect frame = self.titleView.frame;
    frame.size.height = self.titleView.contentSize.height;
    self.titleView.frame = frame;

这有点骇人听闻,但它确实有效。

于 2012-07-24T05:24:59.840 回答
-1
#define kFontSize 14.0    //modify your font size
CGSize size = [yourView sizeWithFont:[UIFont boldSystemFontOfSize:kFontSize]];
于 2012-07-24T05:46:45.200 回答