gpt4 book ai didi

ios - 大小与字体 : ConstrainedToSize: lineBreakMode: method is deprecated in iOS 7

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:52:00 26 4
gpt4 key购买 nike

sizeWithFont: ConstrainedToSize: lineBreakMode: 方法在 iOS 7 中已弃用,我有点不确定如何准确处理这个问题。在互联网上做了一些研究后,我发现有一种新的方法可以处理这个问题,即:

[txt drawWithRect: options: attributes: context:]

这是我目前正在尝试运行的方法:

+ (CGSize)textSizeForText:(NSString *)txt
{
CGFloat width = [UIScreen mainScreen].applicationFrame.size.width * 0.75f;
CGFloat height = MAX([JSBubbleView numberOfLinesForMessage:txt],
[txt numberOfLines]) * [JSMessageInputView textViewLineHeight];


return [txt sizeWithFont:[JSBubbleView font]
constrainedToSize:CGSizeMake(width - kJSAvatarSize, height + kJSAvatarSize)
lineBreakMode:NSLineBreakByWordWrapping];

}

但我很难将其转换为新方法。主要是 lineBreakMode: 新方法中没有。有任何想法吗?

最佳答案

在新方法中,对于换行,你必须先创建一个 NSMutableParagraphStyle Style:

  NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init]; 
paragraphStyle.lineBreakMode = NSLineBreakByWordWrapping;

然后通过传递所有必要的参数来简单地使用新方法

  CGRect textRect = [text boundingRectWithSize:CGSizeMake(width - kJSAvatarSize, height + kJSAvatarSize)
options:NSStringDrawingUsesLineFragmentOrigin
attributes:@{NSParagraphStyleAttributeName: paragraphStyle.copy}
context:nil];

return textRect.size;

如果你想让这成为潮流,你可以做到

  return ([text boundingRectWithSize:CGSizeMake(width - kJSAvatarSize, height + kJSAvatarSize)
options:NSStringDrawingUsesLineFragmentOrigin
attributes:@{NSParagraphStyleAttributeName: paragraphStyle.copy}
context:nil]).size;

希望对你有帮助

我将在上面的答案中添加字体属性

return ([text boundingRectWithSize:CGSizeMake(width - kJSAvatarSize, height + kJSAvatarSize)
options:NSStringDrawingUsesLineFragmentOrigin
attributes:@{NSParagraphStyleAttributeName: paragraphStyle.copy, NSFontAttributeName:[UIFont fontWithName:@"HelveticaNeue" size:14]}
context:nil]).size;

关于ios - 大小与字体 : ConstrainedToSize: lineBreakMode: method is deprecated in iOS 7,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21654671/

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