Frage

I've been plugging away and I need to use the sizeWithFont: method to properly line up my layout, but it doesn't seem to work with custom fonts. Is there a way to get it to work, or maybe another method I can use?

I'm pretty stumped on this. Any help is appreciated.

War es hilfreich?

Lösung

sizeWithFont should definitely work, but if it's giving you a problem then there is a workaround. You can put the text in a UITextView, add it to your layout, and then retrieve the actual text size and adjust accordingly. Here's some sample code from one of my projects:

     // 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;

It's a bit hackish, but it certainly works.

Andere Tipps

#define kFontSize 14.0    //modify your font size
CGSize size = [yourView sizeWithFont:[UIFont boldSystemFontOfSize:kFontSize]];
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top