gpt4 book ai didi

ios - AutoLayout 链接两个 UILabel 以具有相同的字体大小

转载 作者:IT王子 更新时间:2023-10-29 07:59:35 25 4
gpt4 key购买 nike

我有两个相邻的 UILabel,并进行了左右调整,如下所示。

 |-Some text left adjusted----------some other text right adjusted-|

两个标签都有 adjustsFontSizeToFitWidth = YES 并且通过以下约束相互链接

[NSLayoutConstraint constraintWithItem:_rightLabel
attribute:NSLayoutAttributeLeft
relatedBy:NSLayoutRelationGreaterThanOrEqual
toItem:_leftLabel
attribute:NSLayoutAttributeRight
multiplier:1
constant:10]

因此它们会占用尽可能多的空间,如果没有足够的空间容纳原始字体大小,则由于 adjustsFontSizeToFitWidth 将降低其大小,这样就不会截断任何文本。

我的问题是,当一个标签由于文本较长而需要减小其字体大小时,我希望另一个标签也减小其字体大小,以便两者的大小相同,而不是一个可能是另一个的两倍。我也想限制字体大小以匹配,但可惜我不知道该怎么做,有什么想法吗?

最佳答案

来自UILabel documentation on adjustsFontSizeToWidth :

Normally, the label text is drawn with the font you specify in the font property. If this property is set to YES, however, and the text in the text property exceeds the label’s bounding rectangle, the receiver starts reducing the font size until the string fits or the minimum font size is reached.

我由此推断,更新的字体是在绘图时计算的,font 属性只被读取,没有被写入。因此,我相信 Andrew 关于在 font 属性上使用 KVO 的建议是行不通的。

因此,为了达到您想要的结果,您需要计算调整后的字体大小。

正如 Jackson 在评论中指出的那样,this very convenient NSString method获取实际字体在 iOS 7 中已被弃用。从技术上讲,您仍然可以使用它直到它被删除。

另一种方法是遍历字体比例,直到找到适合两个标签的字体比例。我能够让它正常工作; here's a sample project that shows how I did it .

此外,这里是防止链接停止工作的代码:

- (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;
}

这种方法可以简单地重构为类别方法,以取代 Jackson 链接到的已弃用方法。

关于ios - AutoLayout 链接两个 UILabel 以具有相同的字体大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20262156/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com