当前位置: 代码迷 >> 综合 >> -boundingRectWithSize:options:attributes:context:用法
  详细解决方案

-boundingRectWithSize:options:attributes:context:用法

热度:28   发布时间:2023-12-17 12:02:59.0

原文链接:http://www.th7.cn/Program/IOS/201502/386995.shtml

导入第三方一些类库后,出现一些警告就是某些方法被弃用了:

如:

- (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:[UIFont systemFontOfSize:20]};


context:文本绘制的规范定义,一半为nil就可以


替换后:

NSDictionary *attributes = @{NSFontAttributeName:[UIFontsystemFontOfSize:20]};

CGSize textSize = [@"字符串" boundingRectWithSize:CGSizeMake(100, 100) options:NSStringDrawingTruncatesLastVisibleLine attributes:attributes context:nil].size;


  相关解决方案