// return 'best' size to fit given size. does not actually resize view. Default is return existing view size - (CGSize)sizeThatFits:(CGSize)size;
UILabel *testLabel = [[UILabel alloc] init]; testLabel.font = [UIFont systemFontOfSize:30]; testLabel.text = @"Today is a fine day"; CGSize size = [testLabel sizeThatFits:CGSizeMake(200, 30)]; NSLog(@"size = %@", NSStringFromCGSize(size));
// calls sizeThatFits: with current view bounds and changes bounds size. - (void)sizeToFit;
UILabel *testLabel = [[UILabel alloc] init]; testLabel.font = [UIFont systemFontOfSize:30]; testLabel.text = @"Today is a fine day"; [testLabel sizeToFit]; NSLog(@"size = %@", NSStringFromCGSize(testLabel.frame.size));
- (CGSize)sizeWithAttributes:(nullable NSDictionary<NSString *, id> *)attrs NS_AVAILABLE(10_0, 7_0);
NSString *text = @"Today is a fine day";
UIFont *font = [UIFont systemFontOfSize:30];
CGSize size = [text sizeWithAttributes:@{
NSFontAttributeName : font
}];
NSLog(@"size = %@", NSStringFromCGSize(size));
// NOTE: All of the following methods will default to drawing on a baseline, limiting drawing to a single line. // To correctly draw and size multi-line text, pass NSStringDrawingUsesLineFragmentOrigin in the options parameter. - (CGRect)boundingRectWithSize:(CGSize)size options:(NSStringDrawingOptions)options attributes:(nullable NSDictionary<NSString *, id> *)attributes context:(nullable NSStringDrawingContext *)context NS_AVAILABLE(10_11, 7_0);
typedef NS_OPTIONS(NSInteger, NSStringDrawingOptions) {
// The specified origin is the line fragment origin, not the base line origin
// 整个文本将以每行组成的矩形为单位计算整个文本的尺寸
NSStringDrawingUsesLineFragmentOrigin = 1 << 0,
// Uses the font leading for calculating line heights
// 使用字体的行间距来计算文本占用的范围,即每一行的底部到下一行的底部的距离计算
NSStringDrawingUsesFontLeading = 1 << 1,
// Uses image glyph bounds instead of typographic bounds
// 将文字以图像符号计算文本占用范围,而不是排版的边界
NSStringDrawingUsesDeviceMetrics = 1 << 3,
// Truncates and adds the ellipsis character to the last visible line if the text doesn't fit into the bounds specified.
// Ignored if NSStringDrawingUsesLineFragmentOrigin is not also set.
// 如果文本内容超出指定的矩形限制,文本将被截去并在最后一个字符后加上省略号。
// 如果 NSStringDrawingUsesLineFragmentOrigin 没有设置,则该选项不生效
NSStringDrawingTruncatesLastVisibleLine NS_ENUM_AVAILABLE(10_5, 6_0) = 1 << 5,
} NS_ENUM_AVAILABLE(10_0, 6_0);
NSString *text = @"Today is a fine day";
UIFont *font = [UIFont systemFontOfSize:30];
CGRect suggestedRect = [text boundingRectWithSize:CGSizeMake(800, MAXFLOAT)
options:NSStringDrawingUsesFontLeading
attributes:@{ NSFontAttributeName : font }
context:nil];
NSLog(@"size = %@", NSStringFromCGSize(suggestedRect.size));
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有