小编cra*_*zie的帖子

缩放文本以适合iPhone

我在制作应用程序中文本的"最佳"方法时遇到了一些麻烦.

我的主视图包含一个文本视图,应用程序的设计决定了一些事情:

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

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

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

mylabel.font = [self getFontForString:txt];
mylabel.adjustsFontSizeToFitWidth = YES;
mylabel.numberOfLines = 0;
[mylabel setText:txt];
Run Code Online (Sandbox Code Playgroud)

和:

- (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]]; …
Run Code Online (Sandbox Code Playgroud)

iphone layout text uilabel core-text

8
推荐指数
1
解决办法
2133
查看次数

标签 统计

core-text ×1

iphone ×1

layout ×1

text ×1

uilabel ×1