Dal UILabel documentation on adjustsFontSizeToWidth
:
Normalmente, il testo dell'etichetta viene disegnato con il carattere specificato in proprietà del carattere. Se questa proprietà è impostata su SÌ, tuttavia, e il testo nella proprietà text supera il rettangolo di delimitazione dell'etichetta, il ricevitore inizia a ridurre la dimensione del carattere fino a quando la stringa non si adatta o viene raggiunta la dimensione minima del carattere.
Ne deduco che il carattere aggiornato è calcolato in tempo a disegnare, e la proprietà font
è di sola lettura, non è scritto a. Pertanto, credo che il suggerimento di Andrew di utilizzare KVO sulla proprietà font
non funzionerà.
Di conseguenza, per ottenere il risultato desiderato, è necessario calcolare la dimensione del carattere regolato.
Come nota Jackson nei commenti, this very convenient NSString method per ottenere il carattere effettivo è stato deprecato in iOS 7. Tecnicamente, è ancora possibile utilizzarlo fino a quando non viene rimosso.
Un'altra alternativa consiste nel fare il ciclo delle scale dei caratteri finché non ne trovi uno adatto ad entrambe le etichette. Sono stato in grado di farlo funzionare bene; here's a sample project that shows how I did it.
Inoltre, ecco il codice in caso che collegano mai smette di funzionare:
- (void)viewDidLoad
{
[super viewDidLoad];
NSLayoutConstraint *constraint = [NSLayoutConstraint constraintWithItem:_rightLabel
attribute:NSLayoutAttributeLeft
relatedBy:NSLayoutRelationGreaterThanOrEqual
toItem:_leftLabel
attribute:NSLayoutAttributeRight
multiplier:1
constant:10];
[self.view addConstraint:constraint];
}
- (IBAction)makeRightLabelLongerPressed:(id)sender {
self.rightLabel.text = @"some much longer right label text";
}
- (IBAction)adjustLabelSizes:(id)sender {
NSLog(@"Attempting to adjust label sizes…");
CGFloat minimumScaleFactor = fmaxf(self.rightLabel.minimumScaleFactor, self.leftLabel.minimumScaleFactor);;
UIFont * startingFont = self.rightLabel.font;
for (double currentScaleFactor = 1.0; currentScaleFactor > minimumScaleFactor; currentScaleFactor -= 0.05) {
UIFont *font = [startingFont fontWithSize:startingFont.pointSize * currentScaleFactor];
NSLog(@" Attempting font with scale %f (size = %f)…", currentScaleFactor, font.pointSize);
BOOL leftLabelWorks = [self wouldThisFont:font workForThisLabel:self.leftLabel];
BOOL rightLabelWorks = [self wouldThisFont:font workForThisLabel:self.rightLabel];
if (leftLabelWorks && rightLabelWorks) {
NSLog(@" It fits!");
self.leftLabel.font = font;
self.rightLabel.font = font;
return;
} else {
NSLog(@" It didn't fit. :-(");
}
}
NSLog(@" It won't fit without violating the minimum scale (%f), so set both to minimum. Some text may get truncated.", minimumScaleFactor);
UIFont *minimumFont = [self.rightLabel.font fontWithSize:self.rightLabel.font.pointSize * self.rightLabel.minimumScaleFactor];
self.rightLabel.font = minimumFont;
self.leftLabel.font = minimumFont;
}
- (BOOL) wouldThisFont:(UIFont *)testFont workForThisLabel:(UILabel *)testLabel {
NSDictionary *attributes = [NSDictionary dictionaryWithObjectsAndKeys:testFont, NSFontAttributeName, nil];
NSAttributedString *as = [[NSAttributedString alloc] initWithString:testLabel.text attributes:attributes];
CGRect bounds = [as boundingRectWithSize:CGSizeMake(CGRectGetWidth(testLabel.frame), CGFLOAT_MAX) options:(NSStringDrawingUsesLineFragmentOrigin) context:nil];
BOOL itWorks = [self doesThisSize:bounds.size fitInThisSize:testLabel.bounds.size];
return itWorks;
}
- (BOOL)doesThisSize:(CGSize)aa fitInThisSize:(CGSize)bb {
if (aa.width > bb.width) return NO;
if (aa.height > bb.height) return NO;
return YES;
}
Questo approccio potrebbe essere banalmente riscritta in un metodo categoria che sostituisce il metodo deprecato collegato a da Jackson.
Sono riuscito a ottenere qualcosa di simile all'effetto desiderato imponendo un vincolo di larghezza uguale tra le due etichette .. (il ridimensionamento dei caratteri è lo stesso in entrambi gli ultimi due caratteri sono troncati ..) – Aodh