转自http://blog.csdn.net/xiaoxiaobukuang/article/details/52384800
一、定义
1、段落样式定义
CTParagraphStyleSpecifier
typedef CF_ENUM(uint32_t, CTParagraphStyleSpecifier) {
kCTParagraphStyleSpecifierAlignment = 0, //对齐属性
kCTParagraphStyleSpecifierFirstLineHeadIndent = 1, //首行缩进
kCTParagraphStyleSpecifierHeadIndent = 2, //段头缩进
kCTParagraphStyleSpecifierTailIndent = 3, //段尾缩进
kCTParagraphStyleSpecifierTabStops = 4, //制表符模式
kCTParagraphStyleSpecifierDefaultTabInterval = 5, //默认tab间隔
kCTParagraphStyleSpecifierLineBreakMode = 6, //换行模式
kCTParagraphStyleSpecifierLineHeightMultiple = 7, //多行高
kCTParagraphStyleSpecifierMaximumLineHeight = 8, //最大行高
kCTParagraphStyleSpecifierMinimumLineHeight = 9, //最小行高
kCTParagraphStyleSpecifierLineSpacing = 10, //行距
kCTParagraphStyleSpecifierParagraphSpacing = 11, //段落间距 在段的未尾(Bottom)加上间隔,这个值为负数。
kCTParagraphStyleSpecifierParagraphSpacingBefore = 12, //段落前间距 在一个段落的前面加上间隔。TOP
kCTParagraphStyleSpecifierBaseWritingDirection = 13, //基本书写方向
kCTParagraphStyleSpecifierMaximumLineSpacing = 14, //最大行距
kCTParagraphStyleSpecifierMinimumLineSpacing = 15, //最小行距
kCTParagraphStyleSpecifierLineSpacingAdjustment = 16, //行距调整
kCTParagraphStyleSpecifierLineBoundsOptions = 17,
kCTParagraphStyleSpecifierCount
};
2、对其属性
CTTextAlignment
typedef CF_ENUM(uint8_t, CTTextAlignment) {
kCTTextAlignmentLeft CT_ENUM_AVAILABLE(10_8, 6_0) = 0,//左对齐
kCTTextAlignmentRight CT_ENUM_AVAILABLE(10_8, 6_0) = 1,//右对齐
kCTTextAlignmentCenter CT_ENUM_AVAILABLE(10_8, 6_0) = 2,//居中对齐
kCTTextAlignmentJustified CT_ENUM_AVAILABLE(10_8, 6_0) = 3,//文本对齐
kCTTextAlignmentNatural CT_ENUM_AVAILABLE(10_8, 6_0) = 4,//自然文本对齐
kCTLeftTextAlignment CT_ENUM_DEPRECATED(10_5, 10_11, 3_2, 9_0) = kCTTextAlignmentLeft,
kCTRightTextAlignment CT_ENUM_DEPRECATED(10_5, 10_11, 3_2, 9_0) = kCTTextAlignmentRight,
kCTCenterTextAlignment CT_ENUM_DEPRECATED(10_5, 10_11, 3_2, 9_0) = kCTTextAlignmentCenter,
kCTJustifiedTextAlignment CT_ENUM_DEPRECATED(10_5, 10_11, 3_2, 9_0) = kCTTextAlignmentJustified,
kCTNaturalTextAlignment CT_ENUM_DEPRECATED(10_5, 10_11, 3_2, 9_0) = kCTTextAlignmentNatural
};
3、换行属性
CTLineBreakMode
typedef CF_ENUM(uint8_t, CTLineBreakMode) {
kCTLineBreakByWordWrapping = 0,
//出现在单词边界时起作用,如果该单词不在能在一行里显示时,整体换行。此为段的默认值。
kCTLineBreakByCharWrapping = 1,
//当一行中最后一个位置的大小不能容纳一个字符时,才进行换行。
kCTLineBreakByClipping = 2,
//超出画布边缘部份将被截除。
kCTLineBreakByTruncatingHead = 3,
//截除前面部份,只保留后面一行的数据。前部份以...代替。
kCTLineBreakByTruncatingTail = 4,
//截除后面部份,只保留前面一行的数据,后部份以...代替。
kCTLineBreakByTruncatingMiddle = 5
//在一行中显示段文字的前面和后面文字,中间文字使用...代替。
};
二、代码
1、对齐方式
//对齐方式
CTTextAlignment textAlignment = kCTTextAlignmentLeft;
CTParagraphStyleSetting alignmentStyleSetting;
alignmentStyleSetting.spec = kCTParagraphStyleSpecifierAlignment;
alignmentStyleSetting.valueSize = sizeof(textAlignment);
alignmentStyleSetting.value = &textAlignment;
2、首行缩进
//首行缩进
CGFloat firstLineIndentSize = 25.0f;
CTParagraphStyleSetting firstLineIndent;
firstLineIndent.spec = kCTParagraphStyleSpecifierFirstLineHeadIndent;
firstLineIndent.value = &firstLineIndentSize;
firstLineIndent.valueSize = sizeof(CGFloat);
3、段前缩进
不包含第一行
//段前缩进(文字左侧距离context最左侧的距离)
CGFloat headIndentSize = 25.0f;
CTParagraphStyleSetting headIndent;
headIndent.spec = kCTParagraphStyleSpecifierHeadIndent;
headIndent.valueSize = sizeof(CGFloat);
headIndent.value = &headIndentSize;
4、断尾缩进
//断尾缩进(文字右侧距离context最左侧的距离)
CGFloat tailIndentSize = 251.0f; //文字最左侧到文字最右侧的距离
CTParagraphStyleSetting tailIndent;
tailIndent.spec = kCTParagraphStyleSpecifierTailIndent;
tailIndent.value = &tailIndentSize;
tailIndent.valueSize = sizeof(float);
5、换行模式
其他换行模式可自行查看
//换行模式
CTLineBreakMode lineBreak = kCTLineBreakByCharWrapping;
CTParagraphStyleSetting lineBreakMode;
lineBreakMode.spec = kCTParagraphStyleSpecifierLineBreakMode;
lineBreakMode.value = &lineBreak;
lineBreakMode.valueSize = sizeof(CTLineBreakMode);
6、最大行高&最小行高
//最大行高
CGFloat maxHeightSize = 50.0f; //最大行高不能超过50个像素,超过按照最大像素来
CTParagraphStyleSetting maxHeightSetting;
maxHeightSetting.spec = kCTParagraphStyleSpecifierMaximumLineHeight;
maxHeightSetting.value = &maxHeightSize;
maxHeightSetting.valueSize = sizeof(CGFloat);
//最小行高
CGFloat minHeightSize = 50.0f; //最小行高不能小于50个像素,小于按照最小像素来
CTParagraphStyleSetting minHeightSetting;
minHeightSetting.spec = kCTParagraphStyleSpecifierMinimumLineHeight;
minHeightSetting.value = &minHeightSize;
minHeightSetting.valueSize = sizeof(CGFloat);
7、多行高
//多行高
CGFloat multipleHeight = 1.2f; //1.2倍原来的高度
CTParagraphStyleSetting multipleHeightSetting;
multipleHeightSetting.spec = kCTParagraphStyleSpecifierLineHeightMultiple;
multipleHeightSetting.value = &multipleHeight;
multipleHeightSetting.valueSize = sizeof(CGFloat);
8、最大行距&最小行距
//最大行距
CGFloat maxLineSpace = 5.0f;//最大行距不能超过5像素,超过了按最大行距画图,最小行距同理,行距调整只在中间值中进行
CTParagraphStyleSetting maxLineSpaceSetting;
maxLineSpaceSetting.spec = kCTParagraphStyleSpecifierMaximumLineSpacing;
maxLineSpaceSetting.valueSize = sizeof(CGFloat);
maxLineSpaceSetting.value = &maxLineSpace;
//最小行距
CGFloat minLineSpace = 5.0f;//最小行距不能小于5像素,小于按最小行距画图
CTParagraphStyleSetting minLineSpaceSetting;
minLineSpaceSetting.spec = kCTParagraphStyleSpecifierMinimumLineSpacing;
minLineSpaceSetting.valueSize = sizeof(CGFloat);
minLineSpaceSetting.value = &minLineSpace;
9、行距
//行距
CGFloat lineSpace = 25.0f; //行距25像素
CTParagraphStyleSetting lineSpaceSetting;
lineSpaceSetting.spec = kCTParagraphStyleSpecifierLineSpacing;
lineSpaceSetting.value = &lineSpace;
lineSpaceSetting.valueSize = sizeof(float);
10、段前间隔
段与段之间的距离
//段前间隔
CGFloat paragraghSpace = 15.0f;
CTParagraphStyleSetting paragraghInterval;
paragraghInterval.spec = kCTParagraphStyleSpecifierParagraphSpacing;
paragraghInterval.valueSize = sizeof(CGFloat);
paragraghInterval.value = ¶graghSpace;