iOS7 - 调整多行标签的字体大小以适合其框架
iOS7 - Adjusting font size of multiline label to fit its frame
我需要将多行属性文本放入 UILabel 的框架中。
阅读此线程 How to adjust font size of label to fit the rectangle? 我从 Niels 的解决方案开始,以便使用属性文本。
代码如下: 'prospect' 的 't' 换行,而我需要不截断的单词;在此示例中,我希望将字体大小再减小一点,以便在一行中适应“前景”。
UILabel *myLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 180, 180)];
myLabel.font = [UIFont fontWithName:@"Arial" size:5];
myLabel.adjustsFontSizeToFitWidth = YES;
myLabel.backgroundColor = [UIColor orangeColor];
myLabel.numberOfLines = -1;
myLabel.lineBreakMode = NSLineBreakByWordWrapping;
NSMutableParagraphStyle *paragraphStyle = [[NSParagraphStyle defaultParagraphStyle] mutableCopy];
[paragraphStyle setLineSpacing:8];
[paragraphStyle setAlignment:NSTextAlignmentLeft];
NSDictionary *attributes = @{ NSFontAttributeName:myLabel.font, NSParagraphStyleAttributeName: paragraphStyle };
NSAttributedString *attributedString = [[NSAttributedString alloc] initWithString:@"New Prospect" attributes:attributes];
myLabel.attributedText = attributedString;
myLabel.font = [UIFont fontWithName:@"Arial" size:160];
[self sizeLabel:myLabel toRect:myLabel.frame];
[self.view addSubview:myLabel];
sizeLabel方法实现如下:
- (void) sizeLabel {
int fontSize = self.font.pointSize;
int minFontSize = 5;
// Fit label width wize
CGSize constraintSize = CGSizeMake(self.frame.size.width, MAXFLOAT);
do {
// Set current font size
self.font = [UIFont fontWithName:self.font.fontName size:fontSize];
// Find label size for current font size
CGRect textRect = [[self attributedText] boundingRectWithSize:constraintSize
options:NSStringDrawingUsesLineFragmentOrigin
context:nil];
CGSize labelSize = textRect.size;
// Done, if created label is within target size
if( labelSize.height <= self.frame.size.height )
break;
// Decrease the font size and try again
fontSize -= 2;
} while (fontSize > minFontSize);
}
有什么想法吗?
谢谢,
丹
您还需要检查每个单词以确保它们没有被截断,因为 boundingRectWithSize:
永远不会 return 比您提供的宽度更宽的 CGRect(即使单个单词没有被截断) '适合宽度)。这是一个类似的循环,其中包含该机制:
- (void)setButtonTitle:(NSString *)title
{
CGFloat fontSize = 20; // The max font size you want to use
CGFloat minimumFontSize = 5; // The min font size you want to use
CGFloat labelHeightWithFont = 0;
CGFloat longestWordWidth = 0;
UIFont *labelFont = nil;
CGFloat buttonLabelMaxWidth = self.size.width;
do {
if (fontSize < minimumFontSize) {
// Handle exception where the title just won't fit
break;
}
// Trying the current font size if it fits
labelFont = [UIFont systemFontOfSize:fontSize--];
CGSize boundingSize = [self boundingSizeForString:title font:labelFont];
labelHeightWithFont = boundingSize.height;
// Be sure that words are not truncated (that they fits in the maximum width)
longestWordWidth = 0;
for (NSString *word in [title componentsSeparatedByCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]]) {
CGSize wordSize = [word sizeWithAttributes:@{NSFontAttributeName: labelFont}];
longestWordWidth = MAX(longestWordWidth, wordSize.width);
}
// Loop until the text at the current size fits the maximum width/height.
} while (labelHeightWithFont > buttonLabelMaxWidth || longestWordWidth > buttonLabelMaxWidth);
self.buttonLabel.text = title;
self.buttonLabel.font = labelFont;
}
- (CGSize)boundingSizeForString:(NSString *)string font:(UIFont *)font
{
CGRect boundingRect = [string boundingRectWithSize:CGSizeMake([self buttonLabelMaxWidth], MAXFLOAT)
options:NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading
attributes:@{NSFontAttributeName: font}
context:nil];
return CGSizeMake(ceilf(boundingRect.size.width), ceilf(boundingRect.size.height));
}
我需要将多行属性文本放入 UILabel 的框架中。 阅读此线程 How to adjust font size of label to fit the rectangle? 我从 Niels 的解决方案开始,以便使用属性文本。
代码如下: 'prospect' 的 't' 换行,而我需要不截断的单词;在此示例中,我希望将字体大小再减小一点,以便在一行中适应“前景”。
UILabel *myLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 180, 180)];
myLabel.font = [UIFont fontWithName:@"Arial" size:5];
myLabel.adjustsFontSizeToFitWidth = YES;
myLabel.backgroundColor = [UIColor orangeColor];
myLabel.numberOfLines = -1;
myLabel.lineBreakMode = NSLineBreakByWordWrapping;
NSMutableParagraphStyle *paragraphStyle = [[NSParagraphStyle defaultParagraphStyle] mutableCopy];
[paragraphStyle setLineSpacing:8];
[paragraphStyle setAlignment:NSTextAlignmentLeft];
NSDictionary *attributes = @{ NSFontAttributeName:myLabel.font, NSParagraphStyleAttributeName: paragraphStyle };
NSAttributedString *attributedString = [[NSAttributedString alloc] initWithString:@"New Prospect" attributes:attributes];
myLabel.attributedText = attributedString;
myLabel.font = [UIFont fontWithName:@"Arial" size:160];
[self sizeLabel:myLabel toRect:myLabel.frame];
[self.view addSubview:myLabel];
sizeLabel方法实现如下:
- (void) sizeLabel {
int fontSize = self.font.pointSize;
int minFontSize = 5;
// Fit label width wize
CGSize constraintSize = CGSizeMake(self.frame.size.width, MAXFLOAT);
do {
// Set current font size
self.font = [UIFont fontWithName:self.font.fontName size:fontSize];
// Find label size for current font size
CGRect textRect = [[self attributedText] boundingRectWithSize:constraintSize
options:NSStringDrawingUsesLineFragmentOrigin
context:nil];
CGSize labelSize = textRect.size;
// Done, if created label is within target size
if( labelSize.height <= self.frame.size.height )
break;
// Decrease the font size and try again
fontSize -= 2;
} while (fontSize > minFontSize);
}
有什么想法吗? 谢谢, 丹
您还需要检查每个单词以确保它们没有被截断,因为 boundingRectWithSize:
永远不会 return 比您提供的宽度更宽的 CGRect(即使单个单词没有被截断) '适合宽度)。这是一个类似的循环,其中包含该机制:
- (void)setButtonTitle:(NSString *)title
{
CGFloat fontSize = 20; // The max font size you want to use
CGFloat minimumFontSize = 5; // The min font size you want to use
CGFloat labelHeightWithFont = 0;
CGFloat longestWordWidth = 0;
UIFont *labelFont = nil;
CGFloat buttonLabelMaxWidth = self.size.width;
do {
if (fontSize < minimumFontSize) {
// Handle exception where the title just won't fit
break;
}
// Trying the current font size if it fits
labelFont = [UIFont systemFontOfSize:fontSize--];
CGSize boundingSize = [self boundingSizeForString:title font:labelFont];
labelHeightWithFont = boundingSize.height;
// Be sure that words are not truncated (that they fits in the maximum width)
longestWordWidth = 0;
for (NSString *word in [title componentsSeparatedByCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]]) {
CGSize wordSize = [word sizeWithAttributes:@{NSFontAttributeName: labelFont}];
longestWordWidth = MAX(longestWordWidth, wordSize.width);
}
// Loop until the text at the current size fits the maximum width/height.
} while (labelHeightWithFont > buttonLabelMaxWidth || longestWordWidth > buttonLabelMaxWidth);
self.buttonLabel.text = title;
self.buttonLabel.font = labelFont;
}
- (CGSize)boundingSizeForString:(NSString *)string font:(UIFont *)font
{
CGRect boundingRect = [string boundingRectWithSize:CGSizeMake([self buttonLabelMaxWidth], MAXFLOAT)
options:NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading
attributes:@{NSFontAttributeName: font}
context:nil];
return CGSizeMake(ceilf(boundingRect.size.width), ceilf(boundingRect.size.height));
}