我发现对boundingRectWithSize
的调用是非常不正确的,在使用NSFontAttributeName : [UIFont preferredFontForTextStyle:UIFontTextStyleBody]
调用时,缺少了整个额外的行。然而,使用字体[UIFont fontWithName:@"Helvetica Neue" size:17.f]
,这是很好的。
下面是显示bug的测试代码:
- (void)viewDidLoad {
[super viewDidLoad];
NSString *measureText = @"I'm the king of Portmanteaus ... My students slow clap";
CGSize maxSize = CGSizeMake(226.f, CGFLOAT_MAX);
UIFont *targetFont = [UIFont preferredFontForTextStyle:UIFontTextStyleBody];
CGRect stringRect = [measureText boundingRectWithSize:maxSize
options:(NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading)
attributes:@{ NSFontAttributeName : targetFont }
context:nil];
UILabel *drawLabel = [[UILabel alloc] initWithFrame:stringRect];
drawLabel.font = targetFont;
[self.view addSubview:drawLabel];
drawLabel.textColor = [UIColor blackColor];
drawLabel.text = measureText;
drawLabel.numberOfLines = 0;
}
以及产出:
但是,如果我做了targetFont = [UIFont fontWithName:@"Helvetica Neue" size:17.f];
它适当地渲染:
在第一种情况下,大小为{226.20200000000008,42.281000000000006},但在正确的第二种情况下,大小为{228.64999999999998,60.366999999999997}。因此,这不是一个四舍五入的问题;这是缺少一整条新行。
将[UIFont preferredFontForTextStyle:UIFontTextStyleBody]
作为参数传递给boundingRectWithSize
是错误的吗
编辑
根据以下答复中的评论,我相信iOS如何处理与后续单词相关的三个句点存在缺陷。我添加了以下代码:
measureText = [measureText stringByReplacingOccurrencesOfString:@" ..." withString:@"…"];
而且工作正常。
发布于 2015-06-18 04:36:55
标签在文本的外部添加了一点空白,而您不允许这样做。
如果您使用的是与字符串rect大小完全相同的自定义UIView,您将看到文本非常适合:
这是我用的密码。在视图控制器中:
- (void)viewDidLoad {
[super viewDidLoad];
NSString *measureText = @"I'm the king of Portmanteaus ... My students slow clap";
CGSize maxSize = CGSizeMake(226.f, CGFLOAT_MAX);
UIFont *targetFont = [UIFont preferredFontForTextStyle:UIFontTextStyleBody];
CGRect stringRect = [measureText boundingRectWithSize:maxSize
options:(NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading)
attributes:@{ NSFontAttributeName : targetFont }
context:nil];
stringRect.origin = CGPointMake(100,100);
StringDrawer* sd = [[StringDrawer alloc] initWithFrame:stringRect];
[self.view addSubview:sd];
[sd draw:[[NSAttributedString alloc] initWithString:measureText attributes:@{ NSFontAttributeName : targetFont }]];
}
下面是字符串抽屉(一个UIView子类):
@interface StringDrawer()
@property (nonatomic, copy) NSAttributedString* text;
@end
@implementation StringDrawer
- (instancetype) initWithFrame: (CGRect) frame {
self = [super initWithFrame:frame];
self.backgroundColor = [UIColor yellowColor];
return self;
}
- (void) draw: (NSAttributedString*) text {
self.text = text;
[self setNeedsDisplay];
}
- (void)drawRect:(CGRect)rect {
[self.text drawInRect:rect];
}
@end
另外,如果您的目的是通过调整文本的高度来制作包含文本的标签,为什么不让Auto来完成它的任务呢?下一个屏幕截图是一个UILabel,其宽度约束为226,没有高度约束。我给它分配了你的字体和文字,代码。正如您所看到的,它调整了自己的大小,以适应所有文本:
这是通过这一守则实现的,不需要更多:
NSString *measureText = @"I'm the king of Portmanteaus ... My students slow clap";
UIFont *targetFont = [UIFont preferredFontForTextStyle:UIFontTextStyleBody];
self.lab.font = targetFont;
self.lab.text = measureText;
https://stackoverflow.com/questions/30906188
复制相似问题