iOS系统弃用方法更新方法
-boundingRectWithSize:options:attributes:context:用法
- (CGSize)sizeWithFont:(UIFont *)font constrainedToSize:(CGSize)size lineBreakMode:(NSLineBreakMode)lineBreakModeNS_DEPRECATED_IOS(2_0,7_0,"Use -boundingRectWithSize:options:attributes:context:");
提示用:boundingRectWithSize:options:attributes:context:这个方法
这个方法:
- (CGRect)boundingRectWithSize:(CGSize)size options:(NSStringDrawingOptions)options attributes:(NSDictionary *)attributes context:(NSStringDrawingContext *)context
几个参数:
size:范围自己决定
options :这是一个枚举类型
typedefNS_ENUM(NSInteger, NSStringDrawingOptions) {
NSStringDrawingTruncatesLastVisibleLine = 1 << 5,
NSStringDrawingUsesLineFragmentOrigin = 1 <<0,
NSStringDrawingUsesFontLeading = 1 <<1,
NSStringDrawingUsesDeviceMetrics = 1 <<3,
} NS_ENUM_AVAILABLE_IOS(6_0);
自己选一个适合的
attributes:字典
NSDictionary *attributes = @{NSFontAttributeName:[UIFontsystemFontOfSize:20]};
context:文本绘制的规范定义,一般为nil就可以。
替换后:
NSDictionary *attributes = @{NSFontAttributeName:[UIFontsystemFontOfSize:20]}; CGSize textSize = [@"字符串" boundingRectWithSize:CGSizeMake(100, 100) options:NSStringDrawingTruncatesLastVisibleLine attributes:attributes context:nil].size;
drawInRect: withAttributes: 新方法的使用
- (CGSize)drawInRect:(CGRect)rect withFont:(UIFont *)font lineBreakMode:(NSLineBreakMode)lineBreakMode alignment:(NSTextAlignment)alignment NS_DEPRECATED_IOS(2_0, 7_0, "Use -drawInRect:withAttributes:"
drawInRect: withAttributes:
- (void)drawInRect:(CGRect)rect withAttributes:(nullable NSDictionary<NSString *, id> *)attrs NS_AVAILABLE(10_0, 7_0);
之前的用法
[self.placeHolder drawInRect:placeHolderRect withFont:self.font lineBreakMode:NSLineBreakByTruncatingTail alignment:self.textAlignment];
7.0以后
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}];