gpt4 book ai didi

iphone - 缩放文本以适合 iPhone

转载 作者:行者123 更新时间:2023-12-03 18:36:18 24 4
gpt4 key购买 nike

我在确定在应用程序中呈现文本的“最佳”方式时遇到了一些麻烦。

我的主视图由 TextView 组成,应用程序的设计规定了一些内容:

  • 文本的(字体)大小应该是动态的
  • 文本框架应在 View 中垂直居中
  • 连字符应该是自动的,并且仅在需要时进行(如果可能的话应避免)

目前,我正在使用 UILabel 和以下代码来尝试猜测用于文本量的最佳字体大小:

txt  = @"this is just some sample text";

mylabel.font = [self getFontForString:txt];
mylabel.adjustsFontSizeToFitWidth = YES;
mylabel.numberOfLines = 0;
[mylabel setText:txt];

还有:

- (UIFont *) getFontForString:(NSString *)txt {
CGFloat textLength = txt.length;
CGFloat maxFontSize = 71;
CGFloat minFontSize = 27;
CGFloat newFontSize = 0;

NSArray *chunks = [txt componentsSeparatedByString:@" "];
NSSortDescriptor *sortDescriptor = [[[NSSortDescriptor alloc] initWithKey:@"length" ascending:NO] autorelease];
NSArray *sortedChunks = [chunks sortedArrayUsingDescriptors:[NSArray arrayWithObject:sortDescriptor]];

CGSize labelSize = theThingLabel.bounds.size;
CGSize projectedSize = [[sortedChunks objectAtIndex:0] sizeWithFont:[UIFont boldSystemFontOfSize:maxFontSize]];

if (projectedSize.width > labelSize.width) {
CGFloat percentageDifference = ((projectedSize.width - labelSize.width)/labelSize.width)*100;
if (percentageDifference > 50) {
newFontSize = ((minFontSize/percentageDifference)*100) - 10;
if (newFontSize < minFontSize) newFontSize = minFontSize;
} else {
newFontSize = ((percentageDifference/maxFontSize)*100) - 10;
if(newFontSize < (maxFontSize/2)) newFontSize = maxFontSize - abs(newFontSize);
}
} else {
if ( textLength > 11 && textLength < 255) {
newFontSize = (maxFontSize - ((maxFontSize - minFontSize) * ((textLength- 11) / 100)));
} else if (textLength <= 11) {
newFontSize = maxFontSize;
} else if (textLength >= 255) {
newFontSize = minFontSize;
}
}

return [UIFont boldSystemFontOfSize:newFontSize];
}

这在一定程度上是有效的,但当文本有点长时经常会失败,这两个示例显示它渲染以下字符串:

  • “文本量较短”
  • “我仍然想很好地呈现更长的文本量。”

short text longer text

正如您在第二个示例(文本更长)中看到的,存在许多问题:

  • 最初的寡妇
  • 被丢弃的 y
  • 缺少“很好”。

因此,考虑到所有这些,我的选择是什么,如果这是正确的解决方案,我愿意转向使用 coretext,但不知道从哪里开始,也有可能我犯了一个错误,我只是在我的“字体大小猜测”代码中看不到。

最佳答案

以下方法将使用 full 来获取特定矩形(区域)的特定字符串的字体大小。

-(float) maxFontSizeThatFitsForString:(NSString*)_string inRect:(CGRect)rect withFont:(NSString *)fontName onDevice:(int)device {   
// this is the maximum size font that will fit on the device
float _fontSize = maxFontSize;
float widthTweak;

// how much to change the font each iteration. smaller
// numbers will come closer to an exact match at the
// expense of increasing the number of iterations.
float fontDelta = 2.0;

// sometimes sizeWithFont will break up a word
// if the tweak is not applied. also note that
// this should probably take into account the
// font being used -- some fonts work better
// than others using sizeWithFont.
if(device == IPAD)
widthTweak = 0.2;
else
widthTweak = 0.2;

CGSize tallerSize = CGSizeMake(rect.size.width-(rect.size.width*widthTweak), 100000);
CGSize stringSize = CGSizeZero;

if([[UIDevice currentDevice].systemVersion floatValue]>=7.0){
NSDictionary *stringAttributes = [NSDictionary dictionaryWithObject:[UIFont boldSystemFontOfSize:17] forKey: NSFontAttributeName];
stringSize = [_string boundingRectWithSize: tallerSize options:NSStringDrawingUsesLineFragmentOrigin attributes:stringAttributes context:nil].size;
}
else{
stringSize = [_string sizeWithFont:[UIFont fontWithName:fontName size:_fontSize] constrainedToSize:tallerSize];
}

while (stringSize.height >= rect.size.height)
{
_fontSize -= fontDelta;
stringSize = [_string sizeWithFont:[UIFont fontWithName:fontName size:_fontSize] constrainedToSize:tallerSize];
}

return _fontSize;
}

使用它并获取字体大小并分配给标签。

关于iphone - 缩放文本以适合 iPhone,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4199710/

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