根据text字体类型计算全部高度
来源:互联网 发布:7.0神器数据库 编辑:程序博客网 时间:2024/06/07 16:32
iOS_根据文字字数动态确定Label宽高
标签:ios
iOS7中用以下方法- (CGSize)sizeWithAttributes:(NSDictionary *)attrs;
替代过时的iOS6中的- (CGSize)sizeWithFont:(UIFont *)font 方法
// iOS7_API_根据文字 字数动态确定Label宽高 // 设置Label的字体 HelveticaNeue Courier UIFont *fnt = [UIFont fontWithName:@"HelveticaNeue" size:24.0f]; _nameLabel.font = fnt; // 根据字体得到NSString的尺寸 CGSize size = [_nameLabel.text sizeWithAttributes:[NSDictionary dictionaryWithObjectsAndKeys:fnt,NSFontAttributeName, nil]]; // 名字的H CGFloat nameH = size.height; // 名字的W CGFloat nameW = size.width; _nameLabel.frame = CGRectMake(0, 0, nameW,nameH);
iOS7中用以下方法boundingRectWithSize:options:attributes:context:替代过时的iOS6中的sizeWithFont:constrainedToSize:lineBreakMode:方法
// 4,根据正文内容多少,动态确定正文content的frame // 宽度W CGFloat contentW = self.bounds.size.width - _content.frame.origin.x - kMargin; // label的字体 HelveticaNeue Courier UIFont *fnt = [UIFont fontWithName:@"HelveticaNeue" size:18.0f]; _content.font = fnt; _content.numberOfLines = 0; _content.lineBreakMode = NSLineBreakByWordWrapping; // iOS7中用以下方法替代过时的iOS6中的sizeWithFont:constrainedToSize:lineBreakMode:方法 CGRect tmpRect = [_content.text boundingRectWithSize:CGSizeMake(contentW, 1000) options:NSStringDrawingUsesLineFragmentOrigin attributes:[NSDictionary dictionaryWithObjectsAndKeys:fnt,NSFontAttributeName, nil] context:nil]; // 高度H CGFloat contentH = tmpRect.size.height; NSLog(@"调整后的显示宽度:%f,显示高度:%f"contentW,contentH); _content.frame = CGRectMake(0, 0, contentW,contentH);
附:API文档参考
boundingRectWithSize:options:attributes:context:
Calculates and returns the bounding rect for the receiver drawn using the given options and display characteristics, within the specified rectangle in the current graphics context.
- (CGRect)boundingRectWithSize:(CGSize)size options:(NSStringDrawingOptions)options attributes:(NSDictionary *)attributes context:(NSStringDrawingContext *)context
Parameters
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 anNSAttributedString object, but in the case ofNSString 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 returnedCGRect); to use a returned size to size views, you must raise its value to the nearest higher integer using theceil function.
This method returns the actual bounds of the glyphs in the string. Some of the glyphs (spaces, for example) are allowed to overlap the layout constraints specified by the size passed in, so in some cases the width value of the size component of the returned CGRect can exceed the width value of the size parameter.
Availability
- Available in iOS 7.0 and later.
See Also
- – drawInRect:withAttributes:
Declared In
NSStringDrawing.h
iOS_根据文字字数动态确定Label宽高,布布扣,bubuko.com
iOS_根据文字字数动态确定Label宽高
标签:ios
- 根据text字体类型计算全部高度
- IOS 根据 指定的 字体类型 ,size ,计算 绘制 文本 所需要的 宽度与高度
- IOS 根据 指定的 字体类型 ,size ,计算 绘制 文本 所需要的 宽度与高度
- 关于TableViewCell的高度根据text来计算
- 根据字体数量和字体的型号 动态计算Label的高度
- text字体高度的探究
- iOS 根据内容计算高度
- text根据公式自动计算。
- 根据文字长度改变text高度
- C# 根据字体高度变化的textBox
- 指定宽度,根据字体获得高度。
- ios 自动计算text的高度
- android 绘制计算text高度,宽度大小
- 根据字符长度计算UILabel高度
- UILabel、UITextView根据字符串动态计算高度
- iOS 根据文字长度计算Lable高度
- 根据文字和字体大小计算高度
- iOS UILabel根据内容动态计算高度
- Synchronized 静态方法和非静态方法的异同
- android developer tiny share-20160812
- sigaction函数的使用
- Activity详解
- 少年黑客发现美联航漏洞,各挣得一百多万的飞机里程
- 根据text字体类型计算全部高度
- python 跨文件夹引用
- 软键盘的显示与隐藏
- sigaction() 使用
- MyBatis学习(一)--宏观上把握MyBatis框架
- 如何使用JSONP实现跨域请求?
- LeetCode刷题(2)
- 【剑指offer】查找单链表的中间节点
- [SQLServer 2000 Driver for JDBC][SQLServer]传入的表格格式数据流(TDS)远程过程调用(RPC)协议流不正确