auto size font size uilabel

- (void)drawPageLabel {
  self.pageLabel = [[UILabel alloc] init];

  [self addSubview: self.pageLabel];

  self.pageLabel.textColor = [UIColor blackColor];
  [self.pageLabel setAlpha:0.5];
  self.pageLabel.font = [self.pageLabel.font fontWithSize:140];
  [self.pageLabel setNumberOfLines:1];
  self.pageLabel.adjustsFontSizeToFitWidth = YES;
  self.pageLabel.minimumScaleFactor = 0.5f;

  // get a approiate font size using "1" char
  self.pageLabel.text = @"1";

  // make a frame for it to adjust it width for suitable fontSize
  CGFloat innerCircleWidth = self.radius * 0.2f * 2;
  self.pageLabel.frame = CGRectMake(0, 0, innerCircleWidth, innerCircleWidth); // maintain a square size

  // remeber the recommended font size
  CGFloat actualFontSize;
  [self.pageLabel.text sizeWithFont:self.pageLabel.font
               minFontSize:self.pageLabel.minimumFontSize
            actualFontSize:&actualFontSize
                  forWidth:self.pageLabel.bounds.size.width
             lineBreakMode:self.pageLabel.lineBreakMode];

  // use the recommended font size and dun adjust anymore
  [self.pageLabel setFont:[UIFont systemFontOfSize:actualFontSize]];
  [self.pageLabel sizeToFit];

  self.pageLabel.adjustsFontSizeToFitWidth = NO;
}

-(void)updatePageLabel:(int)currentNumber {
  self.pageLabel.text = [NSString stringWithFormat:@"%d", currentNumber];
  [self.pageLabel sizeToFit];
  self.pageLabel.frame = CGRectMake(self.pageLabel.superview.frame.size.width/2 - self.pageLabel.frame.size.width/2,
                                    self.pageLabel.superview.frame.size.height/2 - self.pageLabel.frame.size.height/2,
                                    self.pageLabel.frame.size.width,
                                    self.pageLabel.frame.size.height);
}

CGFloat actualFontSize;
[self.pageLabel.text sizeWithFont:self.pageLabel.font
         minFontSize:self.pageLabel.minimumFontSize
      actualFontSize:&actualFontSize
            forWidth:self.pageLabel.bounds.size.width
       lineBreakMode:self.pageLabel.lineBreakMode];

this make my life … get the recommend font size after adjustsFontSizeToFitWidth
stupid sizeToFit only fit for the setted font size

kvo , key value observer script

in swipeView:

pair set:
– willChangeValueForKey:
– didChangeValueForKey:

or call this
- setValue:forKey:

[self.swipeView addObserver:self
                 forKeyPath:@"currentItemIndex"
                    options:NSKeyValueObservingOptionNew
                    context:nil];

self 個到有

-(void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context {
     NSLog(@"hi"); 
}