CoreText進階(五)- 文字排版樣式和效果

本文的內容主要是文字的排版樣式的文本的繪製效果,排版樣式會涉及到內容的水平對其、行間距、段間距相關的討論;繪製效果會涉及到文本內容的字體、顏色、陰影的相關討論git

其它文章:
CoreText入門(一)-文本繪製
CoreText入門(二)-繪製圖片
CoreText進階(三)-事件處理
CoreText進階(四)-文字行數限制和顯示更多
CoreText進階(五)- 文字排版樣式和效果
CoreText進階(六)-內容大小計算和自動佈局
CoreText進階(七)-添加自定義View和對其app

效果

Demo:CoreTextDemo佈局

如下是四張設置了不一樣屬性的效果圖字體

  • 第一個設置了文字顏色爲紅色,字體爲16號
  • 第二個設置了文字顏色爲灰色,字體爲16號,對其爲居中
  • 第三個設置了文字顏色爲灰色,字體爲14號,對其爲居中,行間距爲10
  • 第四個設置了陰影效果

效果圖

實現的代碼以下:.net

CGRect frame = CGRectMake(0, 20, self.view.bounds.size.width, 100);
    YTDrawView *textDrawView = [[YTDrawView alloc] initWithFrame:frame];
    textDrawView.backgroundColor = [UIColor whiteColor];
    textDrawView.text = @"color:red font:16\n這是一個最好的時代,也是一個最壞的時代;這是明智的時代,這是愚昧的時代;這是信任的紀元,這是懷疑的紀元;這是光明的季節,這是黑暗的季節;這是但願的春日,這是失望的冬日;咱們面前應有盡有,咱們面前一無全部;咱們都將直上天堂,咱們都將直下地獄。";
    textDrawView.textColor = [UIColor redColor];
    textDrawView.font = [UIFont systemFontOfSize:16];
    [self.view addSubview:textDrawView];
    
    frame = CGRectMake(0, 140, self.view.bounds.size.width, 100);
    textDrawView = [[YTDrawView alloc] initWithFrame:frame];
    textDrawView.backgroundColor = [UIColor whiteColor];
    textDrawView.text = @"color:gray font:16 align:center\n這是一個最好的時代,也是一個最壞的時代;這是明智的時代,這是愚昧的時代;這是信任的紀元,這是懷疑的紀元;這是光明的季節,這是黑暗的季節;這是但願的春日,這是失望的冬日;咱們面前應有盡有,咱們面前一無全部;咱們都將直上天堂,咱們都將直下地獄。";
    textDrawView.textColor = [UIColor darkGrayColor];
    textDrawView.font = [UIFont systemFontOfSize:16];
    textDrawView.textAlignment = kCTTextAlignmentCenter;
    [self.view addSubview:textDrawView];
    
    frame = CGRectMake(0, 260, self.view.bounds.size.width, 100);
    textDrawView = [[YTDrawView alloc] initWithFrame:frame];
    textDrawView.backgroundColor = [UIColor whiteColor];
    textDrawView.text = @"color:gray font:14 align:center lineSpacing:10\n這是一個最好的時代,也是一個最壞的時代;這是明智的時代,這是愚昧的時代;這是信任的紀元,這是懷疑的紀元;這是光明的季節,這是黑暗的季節;這是但願的春日,這是失望的冬日;咱們面前應有盡有,咱們面前一無全部;咱們都將直上天堂,咱們都將直下地獄。";
    textDrawView.textColor = [UIColor darkGrayColor];
    textDrawView.font = [UIFont systemFontOfSize:14];
    textDrawView.textAlignment = kCTTextAlignmentCenter;
    textDrawView.lineSpacing = 10;
    [self.view addSubview:textDrawView];
    
    frame = CGRectMake(0, 380, self.view.bounds.size.width, 100);
    textDrawView = [[YTDrawView alloc] initWithFrame:frame];
    textDrawView.backgroundColor = [UIColor whiteColor];
    textDrawView.text = @"color:gray font:14 align:center lineSpacing:10\n這是一個最好的時代,也是一個最壞的時代;這是明智的時代,這是愚昧的時代;這是信任的紀元,這是懷疑的紀元;這是光明的季節,這是黑暗的季節;這是但願的春日,這是失望的冬日;咱們面前應有盡有,咱們面前一無全部;咱們都將直上天堂,咱們都將直下地獄。";
    textDrawView.textColor = [UIColor cyanColor];
    textDrawView.font = [UIFont systemFontOfSize:20];
    textDrawView.textAlignment = kCTTextAlignmentCenter;
    textDrawView.lineSpacing = 10;
    textDrawView.shadowColor = [UIColor blackColor];
    textDrawView.shadowOffset = CGSizeMake(2, 2);
    textDrawView.shadowAlpha = 1.0;
    [self.view addSubview:textDrawView];

全局排版效果

全局排版效果有如下幾種指針

  • 字體
  • 顏色
  • 陰影
  • 行間距
  • 對其
  • 段間距

全局的排版效果是針對所有內容的進行設置的,本質上都是設置NSMutableAttributedString的屬性,特別地code

  • 行間距對其段間距行高是段落屬性,使用kCTParagraphStyleAttributeNamekey設置對應的屬性
  • 陰影使用須要使用CoreGraphics的API CGContextSetShadowWithColor 進行設置的
  • 字體使用kCTFontAttributeName進行設置;顏色使用kCTForegroundColorAttributeName進行設置

行間距、對其、段間距、行高 的設置

使用kCTParagraphStyleAttributeNamekey設置NSMutableAttributedString的屬性,屬性的值是一個CTParagraphStyleRef對象,使用CTParagraphStyleCreate方法建立CTParagraphStyleRef對象,第一個參數是CTParagraphStyleSetting的指針,第二個參數是設置CTParagraphStyleSetting的個數,能夠設置一個或者多個的CTParagraphStyleSetting屬性orm

/**
 設置排版樣式
 */
- (void)setStyleToAttributeString:(NSMutableAttributedString *)attributeString {
    CTParagraphStyleSetting settings[] =
    {
        {kCTParagraphStyleSpecifierAlignment,sizeof(self.textAlignment),&_textAlignment},
        {kCTParagraphStyleSpecifierMaximumLineSpacing,sizeof(self.lineSpacing),&_lineSpacing},
        {kCTParagraphStyleSpecifierMinimumLineSpacing,sizeof(self.lineSpacing),&_lineSpacing},
        {kCTParagraphStyleSpecifierParagraphSpacing,sizeof(self.paragraphSpacing),&_paragraphSpacing},
    };
    CTParagraphStyleRef paragraphStyle = CTParagraphStyleCreate(settings, sizeof(settings) / sizeof(settings[0]));
    [attributeString addAttribute:(id)kCTParagraphStyleAttributeName
                       value:(__bridge id)paragraphStyle
                       range:NSMakeRange(0, [attributeString length])];
    CFRelease(paragraphStyle);
}

陰影的處理

使用CGContextSetShadowWithColor方法進行繪製陰影,參數須要傳入陰影的顏色、陰影的偏移、陰影的透明度,這裏有個注意點:繪製陰影的代碼須要在繪製文字以前先調用,不然會沒喲效果對象

/**
 繪製陰影
 */
- (void)drawShadowInContext:(CGContextRef)context {
    if (self.data.shadowColor == nil
        || CGSizeEqualToSize(self.data.shadowOffset, CGSizeZero)) {
        return;
    }
    CGContextSetShadowWithColor(context, self.data.shadowOffset, self.data.shadowAlpha, self.data.shadowColor.CGColor);
}

- (void)drawRect:(CGRect)rect {
    [super drawRect:rect];
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextSetTextMatrix(context, CGAffineTransformIdentity);
    CGContextTranslateCTM(context, 0, self.bounds.size.height);
    CGContextScaleCTM(context, 1, -1);
    
    // 處理數據
    [self.data composeDataToDrawWithBounds:self.bounds];
    
    // 繪製陰影
    [self drawShadowInContext:context];
    
    // 繪製文字
    [self drawTextInContext:context];
    
    // 繪製圖片
    [self drawImagesInContext:context];
}

字體和顏色的處理

字體顏色的處理其實就是設置NSAttributedString的屬性,yt_setFont方法和yt_setTextColor方法是定義在分類中的兩個方法,方便設置NSAttributedString的屬性blog

- (void)setText:(NSString *)text {
    _text = text;
    [self.attributeString appendAttributedString:[[NSAttributedString alloc] initWithString:_text attributes:nil]];
    [self.attributeString yt_setFont:_font];
    [self.attributeString yt_setTextColor:_textColor];
}

- (void)setTextColor:(UIColor *)textColor {
    _textColor = textColor;
    [self.attributeString yt_setTextColor:_textColor];
}

- (void)setFont:(UIFont *)font {
    _font = font;
    [self.attributeString yt_setFont:_font];
}
相關文章
相關標籤/搜索