Core Graphics框架 : 文本属性Attributes及文本绘制

在Hello World中,我们对文字的绘制进行了简单的说明,这一篇文章就让我们看一下文字绘制中所需要的具体过程的知识点.


文本属性Attributes

- (void)drawAtPoint:(CGPoint)point withAttributes:(nullable NSDictionary<NSString *, id> *)attrs ;
- (void)drawInRect:(CGRect)rect withAttributes:(nullable NSDictionary<NSString *, id> *)attrs ;

上一篇博客中我们就知道文字的绘制有两种方式,但是不光哪一种方式都有attrs这样的一个字典类型的参数,没错,它就是今天的主题—文本属性Attributes,文本不同于label,label对它的自身的文本的设置都封装好了有着对应的属性,我们直接调用就可以了,但是文本却没有这样的属性设置,所以我们需要使用到Attributes.Attribute是一个字典类型的数据.使用的时候,我们把属性名称设置为key,属性详情设置为value即可.下面我们就看一下Attributes都有包含着文本哪些相关的属性.

Attributes的相关文本属性
NSKernAttributeName(文字间距)

NSKernAttributeName的数据类型是NSNumber类型,支持浮点型数据,设置的文字与文字之间的距离,修改默认字距调整。0表示字距调整是禁用的。键值对使用如下.

NSKernAttributeName: @8,
NSFontAttributeName(字体设置)

NSFontAttributeName所对应的value值为UIFont类型,UIFont相关属性的设置比较简单,这里就不多加说明了.使用示例如下所示.

NSFontAttributeName : [UIFont systemFontOfSize:17],
NSForegroundColorAttributeName(字体颜色)

NSFontAttributeName所对应的value值为UIColor类型.NSForegroundColorAttributeName设置的是字体的前景色.使用示例如下所示.

NSForegroundColorAttributeName :[UIColor blackColor],
NSParagraphStyleAttributeName(段落样式)

NSParagraphStyleAttributeName对应的值为NSMutableParagraphStyle类型,NSParagraphStyleAttributeName类中提供了了大量的段落相关属性.当然了,说实话,这里面好多属性我都没有使用过(从网上找到).不过还要展示给大家.毕竟不知道哪一天就会用到.如下所示.

    NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
    
    paragraphStyle.lineSpacing = 10;// 字体的行间距
    
    paragraphStyle.firstLineHeadIndent = 20.0f;//首行缩进
    
    paragraphStyle.alignment = NSTextAlignmentJustified;//(两端对齐的)文本对齐方式:(左,中,右,两端对齐,自然)
    
    paragraphStyle.lineBreakMode = NSLineBreakByTruncatingTail;//结尾部分的内容以……方式省略 ( "...wxyz" ,"abcd..." ,"ab...yz")
    
    paragraphStyle.headIndent = 20;//整体缩进(首行除外)
    
    paragraphStyle.tailIndent = 20;//尾部缩进
    
    paragraphStyle.minimumLineHeight = 10;//最低行高
    
    paragraphStyle.maximumLineHeight = 20;//最大行高
    
    paragraphStyle.paragraphSpacing = 15;//段与段之间的间距
    
    paragraphStyle.paragraphSpacingBefore = 22.0f;//段首行空白空间/* Distance between the bottom of the previous paragraph (or the end of its paragraphSpacing, if any) and the top of this paragraph. */
    
    paragraphStyle.baseWritingDirection = NSWritingDirectionLeftToRight;//从左到右的书写方向(一共➡️⬇️⬅️三种)
    
    paragraphStyle.lineHeightMultiple = 15;/* Natural line height is multiplied by this factor (if positive) before being constrained by minimum and maximum line height. */
    
    paragraphStyle.hyphenationFactor = 1;//连字属性 在iOS,唯一支持的值分别为0和1

段落风格属性设置完成之后,键值对形式如下所示.

NSParagraphStyleAttributeName:paragraphStyle,
NSBackgroundColorAttributeName(背景颜色)

NSFontAttributeName所对应的value值为UIColor类型.NSForegroundColorAttributeName设置的是字体的所占区域的背景颜色.使用示例如下所示.

NSBackgroundColorAttributeName: [UIColor clearColor] ,
NSStrokeColorAttributeName和NSStrokeWidthAttribute(字体边框颜色)

NSStrokeColorAttributeName是设置字体的边框的颜色,NSStrokeWidthAttribute是设置字体边框的宽度,字体边框的宽度是按照和字体的大小进行比例设置的.这两个属性设置完成之后会出现两种效果,如果NSStrokeWidthAttribute为负数,那么字体为描边,如果NSStrokeWidthAttribute为正整数,字体的效果就成了空心字.效果如下所示.

值得注意的是,这两个属性必须要配合着使用,只设置一方面是不能进行实现效果的.而且当NSStrokeWidthAttribute为整数的时候,字体就成了空心字,也就说,文字前景色就失效了.键值对使用示例如下所示.

NSStrokeColorAttributeName:[UIColor blackColor] ,
NSStrokeWidthAttribute:@3,
NSStrikethroughStyleAttributeName和NSUnderlineStyleAttributeName(字体线条)

NSStrikethroughStyleAttributeName所对应的value值为字典类型,可以给字体增加删除线.同时,NSUnderlineStyleAttributeName所对应的value值也为字典类型,可以给字体增加下划线.

删除线和下划线使用如下所示.

//删除线
NSStrikethroughStyleAttributeName:@{NSUnderlineStyleSingle},
//下划线
NSUnderlineStyleAttributeName:@{NSUnderlineStyleSingle},
NSVerticalGlyphFormAttributeName(文字排版方向)

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

NSVerticalGlyphFormAttributeName : @0,
NSObliquenessAttributeName(文字倾斜)

NSObliquenessAttributeName所对应的值是一个 NSNumber 对象.设置的文字的倾斜程度,默认的值为不倾斜.键值对使用如下所示.

NSObliquenessAttributeName: @5,
NSExpansionAttributeName(文字扁平化)

NSExpansionAttributeName所对应的值是一个 NSNumber 对象.设置的文字的扁平化程度,键值对使用如下所示.

NSExpansionAttributeName : @1,
NSShadowAttributeName(文字阴影)

NSShadowAttributeName对应的是NSShadow对象,单一使用不会有任何效果,需要配合着NSVerticalGlyphFormAttributeName(文字排版方向)、NSObliquenessAttributeName(文字倾斜)、NSExpansionAttributeName(文字扁平化)配合使用,NSShadow相关属性设置如下所示.

    NSShadow *shadow = [[NSShadow alloc]init];
    
    shadow.shadowBlurRadius = 5;//模糊度
    
    shadow.shadowColor = [UIColor whiteColor];//阴影颜色
    
    shadow.shadowOffset = CGSizeMake(1, 5);//阴影的大小
    

NSShadowAttributeName键值对示例如下所示.

NSShadowAttributeName:shadow,

Core Graphics框架中的文本绘制

上面我们看完了Attributes的一些常用的文本属性,接下来,我们就看看如何绘制我们的文本.我们使用方法是drawInRect:这个方法.

因为比较简单,所以我就在以前的工程中实现了,首先还是创建SDView继承与UIView类.

创建完成之后,我们就要在SDView的- (void)drawRect:(CGRect)rect绘制我们的文字.其中文本属性Attributes,我们只设置一个文本居中,其他属性都类似,我就不一一列举了.请小伙伴们自行测试.代码如下所示

#import "SDView.h"

@implementation SDView

- (void)drawRect:(CGRect)rect {

    //文字居中显示在画布上
    
    NSMutableParagraphStyle* paragraphStyle = [[NSParagraphStyle defaultParagraphStyle] mutableCopy];
    paragraphStyle.lineBreakMode = NSLineBreakByCharWrapping;
    paragraphStyle.alignment=NSTextAlignmentCenter;//文字居中
    NSString *string  = @"逆战";
    
    [string drawInRect:CGRectMake(100, 100, 100, 100) withAttributes:@{
                                                                       NSFontAttributeName:[UIFont systemFontOfSize:17],
                                                                       NSForegroundColorAttributeName:[UIColor blackColor],
                                                                       
                                                                       NSParagraphStyleAttributeName:
                                                                           
                                                                       paragraphStyle
                                                                       
                                                                       
                                                                       }];
    
}

@end

这样,我们不用借助其他UIKit控件直接显示我们的文本信息,效果图如下所示.



######Core Graphics框架中绘制文本信息整体来说比较简单,但是呢,我们需要对文本属性Attributes有着深入的了解,这样我们可以绘制出炫彩多变的文字来,最后附上本文的Demo传送门.下一篇我将对Core Graphics框架仿射变换的相关知识进行说明讲解,希望大家继续关注神经骚栋,谢谢.

文本属性Attributes以及绘制文本Demo的超级传送门💾
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

神经骚栋

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值