字符串的size

CGSize lableSize = [str boundingRectWithSize: strSize options: NSStringDrawingUsesLineFragmentOrigin attributes: @{ NSForegroundColorAttributeName : [UIColor redColor] } context: nil ].size;

解释:

1 - boundingRectWithSize属性:限定文本在固定范围内进行显示

2 - options属性:

1 )  NSStringDrawingTruncatesLastVisibleLine:如果文本内容超出指定的矩形限制,文本将被截去并在最后一个字符后加上省略号。如果没有指定 NSStringDrawingUsesLineFragmentOrigin选项,则该选项被忽略。

2 ) NSStringDrawingUsesLineFragmentOrigin: 绘制文本时使用 line fragement origin 而不是 baseline origin。

3 ) NSStringDrawingUsesFontLeading: 计算行高时使用行距 , 字体大小+行间距=行距

4  ) NSStringDrawingUsesDeviceMetrics: 计算布局时使用图元字形(而不是印刷字体)。

3 - attributes属性:字体属性

NSForegroundColorAttributeName  -- 颜色

NSFontAttributeName -- 字体

NSParagraphStyleAttributeName -- 设置段落样式

NSBackgroundColorAttributeName -- 设置背景颜色

NSLigatureAttributeName - 连体

NSKernAttributeName  -- 调整字句 kerning 字句调整

NSStrikethroughStyleAttributeName -- 添加删除线

NSStrikethroughColorAttributeName -- 删除线颜色

NSUnderlineStyleAttributeName -- 添加下划线

NSUnderlineColorAttributeName -- 下划线颜色

NSStrokeColorAttributeName  -- 设置文字描边颜色

NSStrokeWidthAttributeName -- 设置描边宽度,这样就能使文字空心.

NSShadowAttributeName -- 设置阴影,单独设置不好使,必须和其他属性搭配才好使。和这三个任一个都好使,NSVerticalGlyphFormAttributeName,NSObliquenessAttributeName,NSExpansionAttributeName

NSTextEffectAttributeName --  特殊效果

NSAttachmentAttributeName

NSLinkAttributeName -- 网址

NSBaselineOffsetAttributeName -- 基线偏移量

NSWritingDirectionAttributeName -- 书写方向

NSObliquenessAttributeName -- 设置字体倾斜。Skew 斜

NSVerticalGlyphFormAttributeName -- 该属性所对应的值是一个 NSNumber 对象(整数)。0 表示横排文本。1 表示竖排文本。在 iOS 中,总是使用横排文本,0 以外的值都未定义。

NSExpansionAttributeName -- 设置文本扁平化

4 - context属性:文本上下文。可调整字间距以及缩放等。最终,该对象包含的信息将用于文本绘制。该参数可为nil

参考:

http://www.tuicool.com/articles/zquENb

http://www.jianshu.com/p/9ffcdc0003e0

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容