iOS-文本自适应宽高废弃方法更新
Posted MinggeQingchun
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了iOS-文本自适应宽高废弃方法更新相关的知识,希望对你有一定的参考价值。
1、-boundingRectWithSize:options:attributes:context:用法
- (CGSize)sizeWithFont:(UIFont *)font forWidth:(CGFloat)width lineBreakMode:(NSLineBreakMode)lineBreakMode API_DEPRECATED_WITH_REPLACEMENT("boundingRectWithSize:options:attributes:context:", ios(2.0, 7.0)) API_UNAVAILABLE(tvos);
提示用:boundingRectWithSize:options:attributes:context:这个方法
- (CGRect)boundingRectWithSize:(CGSize)size options:(NSStringDrawingOptions)options attributes:(nullable NSDictionary<NSAttributedStringKey, id> *)attributes context:(nullable NSStringDrawingContext *)context API_AVAILABLE(macos(10.11), ios(7.0));
使用
//之前用法
CGSize titleSize = [title sizeWithFont:font forWidth:200 lineBreakMode:kLineYardsBreakMode];
//当前用法
CGFloat textWide = 200;//设置文字可显示宽度
CGSize size = CGSizeMake(textWide,CGFLOAT_MAX);
NSMutableParagraphStyle *textStyle = [[NSMutableParagraphStyle defaultParagraphStyle] mutableCopy];
textStyle.lineBreakMode = NSLineBreakByTruncatingTail;
// textStyle.alignment = self.textAlignment;
//获取当前文本的属性
NSDictionary * dic = [NSDictionary dictionaryWithObjectsAndKeys:font,NSFontAttributeName,textStyle,NSParagraphStyleAttributeName,nil];
CGSize titleSize = [title boundingRectWithSize:size options:NSStringDrawingUsesLineFragmentOrigin attributes:dic context:nil].size;
2、drawInRect: withAttributes:用法
- (CGSize)drawInRect:(CGRect)rect withFont:(UIFont *)font lineBreakMode:(NSLineBreakMode)lineBreakMode API_DEPRECATED_WITH_REPLACEMENT("drawInRect:withAttributes:", ios(2.0, 7.0)) API_UNAVAILABLE(tvos);
提示用drawInRect: withAttributes:方法
- (void)drawInRect:(CGRect)rect withAttributes:(nullable NSDictionary<NSAttributedStringKey, id> *)attrs API_AVAILABLE(macos(10.0), ios(7.0));
使用
//之前用法
[self.placeHolder drawInRect:placeHolderRect
withFont:self.font
lineBreakMode:NSLineBreakByTruncatingTail
alignment:self.textAlignment];
//最新用法
NSMutableParagraphStyle *textStyle = [[NSMutableParagraphStyle defaultParagraphStyle] mutableCopy];
textStyle.lineBreakMode = NSLineBreakByTruncatingTail;
textStyle.alignment = self.textAlignment;
UIFont *textFont = self.font;
[self.placeHolder drawInRect:placeHolderRect withAttributes:@{NSFontAttributeName:textFont, NSParagraphStyleAttributeName:textStyle}];
以上是关于iOS-文本自适应宽高废弃方法更新的主要内容,如果未能解决你的问题,请参考以下文章