根据文本动态改变labelframe
来源:互联网 发布:mac上如何删除文件 编辑:程序博客网 时间:2024/05/16 02:24
- (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
- – drawInRect:withAttributes:
- 根据文本动态改变labelframe
- Winfrom固定Label宽度,根据文本动态改变Label的高度
- ios开发笔记之如何让tableView根据文本内容动态改变cell的高度
- iOS--根据文本内容的长度动态改变label的宽度(有最大值)
- ios tableview 根据文本改变lable大小
- select根据值动态改变选中项
- ABAP 根据按钮动态显示/改变屏幕
- Flash-如何改变动态文本透明度?
- 动态改变选择屏幕选择文本
- js 动态改变文本的颜色
- 关于textarea根据文本内容自动改变高度
- 让CStatic控件根据自己的需要改变,文本,文本颜色,填充背景色
- 报表根据选择数据动态改变数据列
- Qt动态进度条[根据值大小改变进度]
- js实现根据下拉列表动态改变其他层
- UITextview 根据输入的内容动态改变高度
- 根据不同的情况动态改变menu菜单项
- 根据数据动态改变Android列表背景颜色
- 软件开发学习
- Git学习笔记
- 红心大战玩法盛天下棋牌
- 斯蒂芬金斯柯达个那事看了个那事刘德华你
- java学习--JVM理解
- 根据文本动态改变labelframe
- 时间字符串比较大小
- 如何在iPhone App中发送短息?
- ubuntu goagent 使用
- 如何禁止ROOT用户登录
- Linux笔记 7 (用户管理)
- ctags的用法
- sqlmap源码分析
- WPF 继承自定义的控件