在ios7中,sizeWithFont:现在已弃用。我现在如何在UIFont对象传递到替换方法sizeWithAttributes:?
当前回答
// max size constraint
CGSize maximumLabelSize = CGSizeMake(184, FLT_MAX)
// font
UIFont *font = [UIFont fontWithName:TRADE_GOTHIC_REGULAR size:20.0f];
// set paragraph style
NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
paragraphStyle.lineBreakMode = NSLineBreakByWordWrapping;
// dictionary of attributes
NSDictionary *attributes = @{NSFontAttributeName:font,
NSParagraphStyleAttributeName: paragraphStyle.copy};
CGRect textRect = [string boundingRectWithSize: maximumLabelSize
options:NSStringDrawingUsesLineFragmentOrigin
attributes:attributes
context:nil];
CGSize expectedLabelSize = CGSizeMake(ceil(textRect.size.width), ceil(textRect.size.height));
其他回答
如果有人需要的话,这里有一个单一的对等物:
/// <summary>
/// Measures the height of the string for the given width.
/// </summary>
/// <param name="text">The text.</param>
/// <param name="font">The font.</param>
/// <param name="width">The width.</param>
/// <param name="padding">The padding.</param>
/// <returns></returns>
public static float MeasureStringHeightForWidth(this string text, UIFont font, float width, float padding = 20)
{
NSAttributedString attributedString = new NSAttributedString(text, new UIStringAttributes() { Font = font });
RectangleF rect = attributedString.GetBoundingRect(new SizeF(width, float.MaxValue), NSStringDrawingOptions.UsesLineFragmentOrigin, null);
return rect.Height + padding;
}
可以这样使用:
public override float GetHeightForRow(UITableView tableView, NSIndexPath indexPath)
{
//Elements is a string array
return Elements[indexPath.Row].MeasureStringHeightForWidth(UIFont.SystemFontOfSize(UIFont.LabelFontSize), tableView.Frame.Size.Width - 15 - 30 - 15);
}
最好使用自动尺寸(Swift):
tableView.estimatedRowHeight = 68.0
tableView.rowHeight = UITableViewAutomaticDimension
注: 1. UITableViewCell原型应该正确设计(对于实例不要忘记设置UILabel。numberolines = 0 etc) 2. 删除hightforrowatindexpath方法
视频: https://youtu.be/Sz3XfCsSb6k
改用sizeWithAttributes:,它现在接受一个NSDictionary。传入key UITextAttributeFont和你的字体对象,如下所示:
CGRect rawRect = {};
rawRect.size = [string sizeWithAttributes: @{
NSFontAttributeName: [UIFont systemFontOfSize:17.0f],
}];
// Values are fractional -- you should take the ceil to get equivalent values
CGSize adjustedSize = CGRectIntegral(rawRect).size;
正如@Ayush的回答:
正如你在苹果开发者网站上看到的sizeWithFont,它已被弃用,所以我们需要使用sizeWithAttributes。
好吧,假设在2019+你可能会使用Swift和String而不是Objective-c和NSString,下面是使用预定义字体获取字符串大小的正确方法:
let stringSize = NSString(string: label.text!).size(withAttributes: [.font : UIFont(name: "OpenSans-Regular", size: 15)!])
试试这个语法:
NSAttributedString *attributedText =
[[NSAttributedString alloc] initWithString:text
attributes:@{NSFontAttributeName: font}];
推荐文章
- buildSessionFactory()配置方法在Hibernate中已弃用?
- 在实现API时,我如何避免在块中捕获自我?
- 从另一个应用程序打开设置应用程序
- 如何应用梯度的背景视图的iOS Swift应用程序
- 使用NSURLSession发送POST请求
- 如果用户强制退出,iOS会将我的应用启动到后台吗?
- 如何更改导航栏上“后退”按钮的标题
- 有没有办法从UITableView中移除分隔线?
- 如何在Objective-C中声明类级属性?
- 我如何获得iOS 7默认的蓝色编程?
- 在Objective-C中@property保留,赋值,复制,非原子
- 我如何在NSAttributedString中创建一个可点击的链接?
- 停止UIWebView垂直“弹跳”?
- 对未渲染的视图进行快照,结果是一个空快照
- 如何隐藏iOS状态栏