gpt4 book ai didi

core-animation - CATextLayer 包裹 sizeToFit?

转载 作者:行者123 更新时间:2023-12-04 08:50:15 26 4
gpt4 key购买 nike

如果我设置 textLayer.wrapped = YES , 如何调整大小 textLayer包含包装的文本?即,我如何获得 textLayer 的新高度?

基本上,我想要类似 -[UILabel sizeToFit] 的东西.

最佳答案

您需要做的第一件事是获取文本的大小。

值得庆幸的是,NSString UIKit Additions Reference提供了一种方法来做到这一点:

- (CGSize)sizeWithFont:(UIFont *)font constrainedToSize:(CGSize)size lineBreakMode:(UILineBreakMode)lineBreakMode

那会给你一个 CGSize然后您可以使用它来设置 UILabel 的框架或 UIView 的任何子类你正在使用的。

所以,假设 textLayerUILabel - 而不是 CALayer - 你最终会得到这样的结果:
UIFont *myFont = [UIFont boldSystemFontOfSize:12.0f];
CGSize myFontSize = [myString sizeWithFont:myFont];
myLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, myFontSize.width, myFontSize.height)];
myLabel.text = newTitle;
myLabel.font = myFont;

关于core-animation - CATextLayer 包裹 sizeToFit?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8219588/

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