你好,歡迎來到IOS教程網

 Ios教程網 >> IOS編程開發 >> IOS開發綜合 >> CoreText(二):段落CTParagraphStyle

CoreText(二):段落CTParagraphStyle

編輯:IOS開發綜合

一、定義

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、段前縮進

這裡寫圖片描述
不包含第一行<喎?/kf/ware/vc/" target="_blank" class="keylink">vcD4NCjxwcmUgY2xhc3M9"brush:java;"> //段前縮進(文字左側距離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;
  1. 上一頁:
  2. 下一頁:
蘋果刷機越獄教程| IOS教程問題解答| IOS技巧綜合| IOS7技巧| IOS8教程
Copyright © Ios教程網 All Rights Reserved