关于tableview高度自适应的一个方法

本文深入解析了在iOS7中,如何从使用sizeWithFont:constrainedToSize:lineBreakMode到采用boundingRectWithSize:options:attributes:context:方法来处理字符串绘制。详细介绍了各个参数的作用以及返回值特性,同时强调了正确绘制多行文本时需传递NSStringDrawingUsesLineFragmentOrigin作为参数的重要性。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

网上看了很多例子,都是用这个方法实现

- (CGSize)sizeWithFont:(UIFont *)font constrainedToSize:(CGSize)size lineBreakMode:(UILineBreakMode)lineBreakMode;

不过查阅apple文档发现,在iOS7中用

boundingRectWithSize:options:attributes:context:替代了。

- (CGRect)boundingRectWithSize:(CGSize)size options:(NSStringDrawingOptions)options attributes:(NSDictionary *)attributes context:(NSStringDrawingContext *)context

Parameters

参数:1 size label的大小 2 string 绘制选项 3 属性  4 上下文。返回一个给定的选项和特征值给这个矩形边界给接受者。这个矩形边界的origin值为第一个符号的origin值。 下面是原文:

size

The size of the rectangle to draw in.

options

String drawing options.

attributes

A dictionary of text attributes to be applied to the string. These are the same attributes that can be applied to an NSAttributedString object, but in the case of NSString objects, the attributes apply to the entire string, rather than ranges within the string.

context

The string drawing context to use for the receiver, specifying minimum scale factor and tracking adjustments.

Return Value

The bounding rect for the receiver drawn using the given options and display characteristics. The rect origin returned from this method is the first glyph origin.

Discussion

To correctly draw and size multi-line text, pass NSStringDrawingUsesLineFragmentOrigin in the options parameter.

This method returns fractional sizes (in the size component of the returned CGRect); to use a returned size to size views, you must use raise its value to the nearest higher integer using the ceil function.



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值