简体   繁体   中英

How do you know how much space an NSString (using default font) will occupy on a UITableView section header?

Basically I want to copy the behavior in iPhone's Calendar where the day of week (Mon, Tue, Wed) is on the left side of the table's section header (left justified) while the formatted date (Apr 1, 2010, May 1, 2010) based on locale is on the right side of the table's section header (right justified).

I was thinking of inserting a variable amount of space in between the day of week and formatted date but I first need to know the actual space consumed by the text on both sides to figure out how much space to add.

Hope to hear your thoughts on this.

Thanks.

There're some additions made in UIKit to NSString class, among them functions allowing to determine string size when drawing with specific font. See this function in docs for a start:

- (CGSize)sizeWithFont:(UIFont *)font

也许您可以创建一个临时UILabel,将日期字符串设置为标签的文本,然后通过frame属性(-> label.size.width)获取标签的宽度。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM