简介
CATextLayer提供了简单的文本布局,第一行是靠近顶部的。支持普通文本和属性文本。
属性
string
id, No Animatable。要显示的文本,类型可以使NSString
或者NSAttributedString
,默认值为nil。
font
CFTypeRef, No Animatable。使用的字体,只有当string类型为NSString
时才有效。默认Helvetica字体。CFTypeRef
只是一个占位符,font支持的类型有NSString
、CTFontRef
、CGFontRef
,其中NSString
必须是支持的字体名称。
<code class="hljs cs has-numbering" style="display: block; padding: 0px; background-color: transparent; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-top-left-radius: 0px; border-top-right-radius: 0px; border-bottom-right-radius: 0px; border-bottom-left-radius: 0px; word-wrap: normal; background-position: initial initial; background-repeat: initial initial;"><span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">// 设置textLayer的字体为Bradley Hand</span> textLayer.font = (__bridge CFTypeRef _Nullable)(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">@"Bradley Hand"</span>);</code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; background-color: rgb(238, 238, 238); top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right;"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li></ul><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; background-color: rgb(238, 238, 238); top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right;"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li></ul>
fontSize
CGFloat, Animatable。字体大小,只有当string类型为NSString
时才有效。默认36.0。
foregroundColor
CGColorRef, Animatable。文本颜色,默认不透明白色。动画效果仅支持OS X 10.6以上版本,不支持iOS。
wrapped(isWrapped)
BOOL, No Animatable。自动换行。
truncationMode
NSString *, No Animatable。当字符串显示不全的时候的裁剪方式。可选以下值:
<code class="hljs objectivec has-numbering" style="display: block; padding: 0px; background-color: transparent; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-top-left-radius: 0px; border-top-right-radius: 0px; border-bottom-right-radius: 0px; border-bottom-left-radius: 0px; word-wrap: normal; background-position: initial initial; background-repeat: initial initial;"><span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">NSString</span> * <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">const</span> kCATruncationNone; <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">// 不剪裁,默认</span> <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">NSString</span> * <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">const</span> kCATruncationStart; <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">// 剪裁开始部分</span> <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">NSString</span> * <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">const</span> kCATruncationEnd; <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">// 剪裁结束部分</span> <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">NSString</span> * <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">const</span> kCATruncationMiddle; <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">// 剪裁中间部分</span></code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; background-color: rgb(238, 238, 238); top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right;"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li></ul><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; background-color: rgb(238, 238, 238); top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right;"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li></ul>
alignmentMode
NSString *, No Animatable。字符串对齐方式。
<code class="hljs objectivec has-numbering" style="display: block; padding: 0px; background-color: transparent; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-top-left-radius: 0px; border-top-right-radius: 0px; border-bottom-right-radius: 0px; border-bottom-left-radius: 0px; word-wrap: normal; background-position: initial initial; background-repeat: initial initial;"><span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">NSString</span> * <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">const</span> kCAAlignmentNatural; <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">// 自然对齐,默认</span> <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">NSString</span> * <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">const</span> kCAAlignmentLeft; <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">// 左对齐</span> <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">NSString</span> * <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">const</span> kCAAlignmentRight; <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">// 右对齐</span> <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">NSString</span> * <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">const</span> kCAAlignmentCenter; <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">// 居中</span> <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">NSString</span> * <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">const</span> kCAAlignmentJustified; <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">// 两端对齐</span></code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; background-color: rgb(238, 238, 238); top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right;"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li></ul><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; background-color: rgb(238, 238, 238); top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right;"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li></ul>