3 回答
TA贡献1812条经验 获得超5个赞
NSString+UIKit
sizewithFont:...
UIStringDrawing
UIKit
boundingRectWithSize:...
NSAttributedString
NSStringDrawing
NSString
boundingRectWithSize:...
NSAttributeString
NSString
NSAttributeString
NSString
sizeWithFont:...
NSAttributeString
boundingRectWithSize
NSString
sizeWithFont:constrainedToSize:
:
过去的情况是:
NSString *text = ...;CGFloat width = ...;UIFont *font = ...;CGSize size = [text sizeWithFont:font constrainedToSize:(CGSize){width, CGFLOAT_MAX}];
可替换为:
NSString *text = ...;CGFloat width = ...;UIFont *font = ...;NSAttributedString *attributedText = [[NSAttributedString alloc] initWithString:text attributes:@{NSFontAttributeName: font}];CGRect rect = [attributedText boundingRectWithSize:(CGSize){width, CGFLOAT_MAX} options:NSStringDrawingUsesLineFragmentOrigin context:nil];CGSize size = rect.size;
在IOS 7和更高版本中,此方法返回小数大小(在返回的大小组件中)。 CGRect
);若要使用返回的大小来调整视图的大小,必须使用ceil函数将其值提高到最近的较高整数。
CGFloat height = ceilf(size.height);CGFloat width = ceilf(size.width);
TA贡献1773条经验 获得超3个赞
sizeWithFont
sizeWithAttributes
.
#define SYSTEM_VERSION_LESS_THAN(v) ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] == NSOrderedAscending)NSString *text = @"Hello iOS 7.0";if (SYSTEM_VERSION_LESS_THAN(@"7.0")) { // code here for iOS 5.0,6.0 and so on CGSize fontSize = [text sizeWithFont:[UIFont fontWithName:@"Helvetica" size:12]];} else { // code here for iOS 7.0 CGSize fontSize = [text sizeWithAttributes: @{NSFontAttributeName: [UIFont fontWithName:@"Helvetica" size:12]}];}
- 3 回答
- 0 关注
- 630 浏览
添加回答
举报