学习boundingRectWithSize:options:attributes:context:计算文本尺寸
来源:互联网 发布:微信淘宝客推广优惠券 编辑:程序博客网 时间:2024/05/18 00:48
返回文本绘制所占据的矩形空间。
- (CGRect)boundingRectWithSize:(CGSize)size options:(NSStringDrawingOptions)options context:(NSStringDrawingContext *)context
参数
size
宽高限制,用于计算文本绘制时占据的矩形块。
The width and height constraints to apply when computing the string’s bounding rectangle.
options
文本绘制时的附加选项。可能取值请参考“NSStringDrawingOptions”。
context
context上下文。包括一些信息,例如如何调整字间距以及缩放。最终,该对象包含的信息将用于文本绘制。该参数可为 nil 。
返回值
一个矩形,大小等于文本绘制完将占据的宽和高。
讨论
可以使用该方法计算文本绘制所需的空间。size 参数是一个constraint ,用于在绘制文本时作为参考。但是,如果绘制完整个文本需要更大的空间,则返回的矩形大小可能比 size 更大。一般,绘制时会采用constraint 提供的宽度,但高度则会根据需要而定。
特殊情况
为了计算文本块的大小,该方法采用默认基线。
如果 NSStringDrawingUsesLineFragmentOrigin未指定,矩形的高度将被忽略,同时使用单线绘制。(由于一个 bug,在 iOS6 中,宽度会被忽略)
兼容性
- iOS 6.0 以后支持。
声明于
NSStringDrawing.
另外,关于参数(NSStringDrawingOptions)options
typedef NS_ENUM(NSInteger, NSStringDrawingOptions) { NSStringDrawingTruncatesLastVisibleLine = 1 << 5, // 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 = 1 << 0, // The specified origin is the line fragment origin, not the base line origin NSStringDrawingUsesFontLeading = 1 << 1, // Uses the font leading for calculating line heights NSStringDrawingUsesDeviceMetrics = 1 << 3, // Uses image glyph bounds instead of typographic bounds } NS_ENUM_AVAILABLE_IOS(6_0);
NSStringDrawingTruncatesLastVisibleLine:
如果文本内容超出指定的矩形限制,文本将被截去并在最后一个字符后加上省略号。如果没有指定NSStringDrawingUsesLineFragmentOrigin选项,则该选项被忽略。
NSStringDrawingUsesLineFragmentOrigin:
绘制文本时使用 line fragement origin 而不是 baseline origin。
The origin specified when drawing the string is the line fragment origin and not the baseline origin.
NSStringDrawingUsesFontLeading:
计算行高时使用行距。(译者注:字体大小+行间距=行距)
NSStringDrawingUsesDeviceMetrics:
计算布局时使用图元字形(而不是印刷字体)。
Use the image glyph bounds (instead of the typographic bounds) when computing layout.
OS7中出现了新的方法计算UILabel中根据给定的Font以及str计算UILabel的frameSize的方法.本人提供category如下:
UILabel+StringFrame.h
//// UILabel+StringFrame.h// LabelHeight//// Copyright (c) 2014年 Y.X. All rights reserved.//#import <UIKit/UIKit.h>@interface UILabel (StringFrame)- (CGSize)boundingRectWithSize:(CGSize)size;@end
UILabel+StringFrame.m
//
// UILabel+StringFrame.m
// LabelHeight
//
// Copyright (c) 2014年 Y.X. All rights reserved.
//
#import "UILabel+StringFrame.h"
@implementation UILabel (StringFrame)
- (CGSize)boundingRectWithSize:(CGSize)size
{
NSDictionary *attribute = @{NSFontAttributeName: self.font};
CGSize retSize = [self.text boundingRectWithSize:size
options:\
NSStringDrawingTruncatesLastVisibleLine |
NSStringDrawingUsesLineFragmentOrigin |
NSStringDrawingUsesFontLeading
attributes:attribute
context:nil].size;
return retSize;
}
- TextKit学习 通过boundingRectWithSize:options:attributes:context:计算文本尺寸
- 学习boundingRectWithSize:options:attributes:context:计算文本尺寸
- 通过boundingRectWithSize:options:attributes:context:计算文本尺寸
- 通过boundingRectWithSize:options:attributes:context:计算文本尺寸
- iOS方法:boundingRectWithSize:options:attributes:context:(计算文本尺寸)
- TextKit学习(四)通过boundingRectWithSize:options:attributes:context:计算文本尺寸
- TextKit学习(四)通过boundingRectWithSize:options:attributes:context:计算文本尺寸
- TextKit学习(四)通过boundingRectWithSize:options:attributes:context:计算文本尺寸
- TextKit学习(四)通过boundingRectWithSize:options:attributes:context:计算文本尺寸
- TextKit学习(四)通过boundingRectWithSize:options:attributes:context:计算文本尺寸
- TextKit学习(四)通过boundingRectWithSize:options:attributes:context:计算文本尺寸
- TextKit学习(四)通过boundingRectWithSize:options:attributes:context:计算文本尺寸
- TextKit学习(四)通过boundingRectWithSize:options:attributes:context:计算文本尺寸
- TextKit学习(四)通过boundingRectWithSize:options:attributes:context:计算文本尺寸
- TextKit学习(四)通过boundingRectWithSize:options:attributes:context:计算文本尺寸
- TextKit学习(四)通过boundingRectWithSize:options:attributes:context:计算文本尺寸
- TextKit学习(四)通过boundingRectWithSize:options:attributes:context:计算文本尺寸
- TextKit学习(四)通过boundingRectWithSize:options:attributes:context:计算文本尺寸
- js三级联动
- linux设备树使用手册
- MMORPG服务器架构
- android GestureListener手势,多指缩放
- HDU 5226 Tom and matrix
- 学习boundingRectWithSize:options:attributes:context:计算文本尺寸
- 小白来到公司两个月了。
- 怎么样利用栅格数据分类后的结果以行政区域统计各个地类的面积
- 网络攻击技术开篇——SQL Injection
- Linux中/var空间不足的解决办法
- 去掉UItableview headerview黏性(sticky)
- SNS关系分析系统
- linux 高并发socket通信模型
- 【C#】登录系统时判断是否是合法用户的核心代码