gpt4 book ai didi

iphone - 替换弃用的 sizeWithFont :minFontSIze:actualFontSize in ios 7

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

在 iOS 6 中我使用了这个方法:

[self.handText sizeWithFont:font 
minFontSize:10.0f
actualFontSize:&maxFontSize
forWidth:handWidth/2
lineBreakMode:UILineBreakModeClip];

xcode 5 表示 'sizeWithFont:minFontSIze:actualFontSize:forWidth:lineBreakMode:' 已弃用:首先在 iOS 7 中弃用

现在我是这样实现的:

[self.handText sizeWithAttributes:@{NSFontAttributeName:font} 
minFontSize:10.0f
actualFontSize:&maxFontSize
forWidth:handWidth/2
lineBreakMode:NSLineBreakByClipping];

这里 xcode 抛出另一个警告:'实例方法-sizeWithAttributed:minFontSize:forWidth:lineBreakMode:'未找到(返回类型默认为'id')

任何人都可以帮我解决这个警告吗?

最佳答案

改用这个辅助方法:

-(CGSize)frameForText:(NSString*)text sizeWithFont:(UIFont*)font constrainedToSize:(CGSize)size lineBreakMode:(NSLineBreakMode)lineBreakMode  {

NSMutableParagraphStyle * paragraphStyle = [[NSMutableParagraphStyle defaultParagraphStyle] mutableCopy];
paragraphStyle.lineBreakMode = lineBreakMode;

NSDictionary * attributes = @{NSFontAttributeName:font,
NSParagraphStyleAttributeName:paragraphStyle
};


CGRect textRect = [text boundingRectWithSize:size
options:NSStringDrawingUsesLineFragmentOrigin
attributes:attributes
context:nil];

//Contains both width & height ... Needed: The height
return textRect.size;
}

如果您需要同时支持 iOS 6 和 iOS 7,请像这样使用:

#ifdef __IPHONE_7_0

titleSize = [self frameForText:self.titleLabel.text sizeWithFont:self.titleLabel.font constrainedToSize:CGSizeMake(labelMaxWidth,self.titleLabel.font.lineHeight) lineBreakMode:self.titleLabel.lineBreakMode ];

subtitleSize = [self frameForText:self.subtitleLabel.text sizeWithFont:self.subtitleLabel.font constrainedToSize:CGSizeMake(labelMaxWidth,self.subtitleLabel.font.lineHeight) lineBreakMode:self.subtitleLabel.lineBreakMode];

#else


titleSize = [self.titleLabel.text sizeWithFont:self.titleLabel.font
constrainedToSize:CGSizeMake(labelMaxWidth,self.titleLabel.font.lineHeight)
lineBreakMode:self.titleLabel.lineBreakMode];

subtitleSize = [self.subtitleLabel.text sizeWithFont:self.subtitleLabel.font
constrainedToSize:CGSizeMake(labelMaxWidth,self.subtitleLabel.font.lineHeight)
lineBreakMode:self.subtitleLabel.lineBreakMode];
#endif

关于iphone - 替换弃用的 sizeWithFont :minFontSIze:actualFontSize in ios 7,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20786067/

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