YYText使用篇(三)

版本記錄

版本號(hào) 時(shí)間
V1.0 2017.06.06

前言

YYText是一個(gè)專(zhuān)門(mén)處理文字的框架,有了它處理文字變得非常方便,這一篇我繼續(xù)介紹YYText的使用方法,希望對(duì)大家能有所幫助。大家如感興趣還可以參考:
1.YYText使用篇(一)
2.YYText使用篇(二)

一、YYText圖文混排

下面首先看所需要的方法

/**
 Creates and returns an attachment.
 
 
 Example: ContentMode:bottom Alignment:Top.
 
      The text      The attachment holder
         ↓                ↓
     ─────────┌──────────────────────┐───────
        / \   │                      │ / ___|
       / _ \  │                      │| |
      / ___ \ │                      │| |___     ←── The text line
     /_/   \_\│    ██████████████    │ \____|
     ─────────│    ██████████████    │───────
              │    ██████████████    │
              │    ██████████████ ←───────────────── The attachment content
              │    ██████████████    │
              └──────────────────────┘

 @param content        The attachment (UIImage/UIView/CALayer).
 @param contentMode    The attachment's content mode in attachment holder
 @param attachmentSize The attachment holder's size in text layout.
 @param fontSize       The attachment will align to this font.
 @param alignment      The attachment holder's alignment to text line.
 
 @return An attributed string, or nil if an error occurs.
 @since YYText:6.0
 */
+ (NSMutableAttributedString *)yy_attachmentStringWithContent:(nullable id)content
                                                  contentMode:(UIViewContentMode)contentMode
                                               attachmentSize:(CGSize)attachmentSize
                                                  alignToFont:(UIFont *)font
                                                    alignment:(YYTextVerticalAlignment)alignment;

下面我們看個(gè)例子

#import "JJTextVC.h"
#import "YYText.h"

@interface JJTextVC ()

@end

@implementation JJTextVC

#pragma mark - Override Base Function

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    self.view.backgroundColor = [UIColor lightTextColor];
    
    self.title = @"YYText";

    //圖文混排
    [self imageTextLayout];
}

#pragma mark - Object Private Function

//圖文混排
- (void)imageTextLayout
{
    NSMutableAttributedString *text = [[NSMutableAttributedString alloc] initWithString:@"舊時(shí)月色,算幾番照我,梅邊吹笛?喚起玉人,不管清寒與攀摘.何遜而今漸老,都忘卻,春風(fēng)詞筆. "];
    UIFont *font = [UIFont systemFontOfSize:18];
    
    // 嵌入 UIImage
    UIImage *image = [UIImage imageNamed:@"group_prooerty_selected"];
    NSMutableAttributedString *attachment = nil;
    attachment = [NSMutableAttributedString yy_attachmentStringWithContent:image contentMode:UIViewContentModeCenter attachmentSize:image.size alignToFont:font alignment:YYTextVerticalAlignmentCenter];
    [text appendAttributedString: attachment];
    
    NSMutableAttributedString *text1 = [[NSMutableAttributedString alloc] initWithString:@"但怪得,竹外疏花,春冷入瑤席.江國(guó),正寂寂.嘆寄與路遙,夜雪初積.翠尊易泣,紅萼無(wú)言耿相憶.常記曾攜手處,千樹(shù)壓.梅湖寒碧,又片片吹盡也,何時(shí)得見(jiàn)?"];
    [text appendAttributedString: text1];
    
    // 嵌入 UIImage
    UIImage *image1 = [UIImage imageNamed:@"live_score_image"];
    NSMutableAttributedString *attachment1 = nil;
    attachment1 = [NSMutableAttributedString yy_attachmentStringWithContent:image1 contentMode:UIViewContentModeCenter attachmentSize:image.size alignToFont:font alignment:YYTextVerticalAlignmentCenter];
    [text appendAttributedString: attachment1];
    
    YYLabel *yyLabel = [[YYLabel alloc] init];
    yyLabel.attributedText = text;
    yyLabel.numberOfLines = 0;
    yyLabel.textColor = [UIColor blueColor];
    yyLabel.font = [UIFont boldSystemFontOfSize:20.0];
    yyLabel.frame = CGRectMake(0.0, 64.0, self.view.bounds.size.width, 500.0);
    [self.view addSubview:yyLabel];
}

@end

下面看輸出結(jié)果

圖文混排

二、YYText布局計(jì)算

下面看YYText的圖文混排布局計(jì)算,還是先看代碼吧。

#import "JJTextVC.h"
#import "YYText.h"

@interface JJTextVC ()

@end

@implementation JJTextVC

#pragma mark - Override Base Function

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    self.view.backgroundColor = [UIColor lightTextColor];
    
    self.title = @"YYText";

    //布局計(jì)算
    [self layoutCaculation];
}

#pragma mark - Object Private Function

//布局計(jì)算
- (void)layoutCaculation
{
    NSMutableAttributedString *text = [[NSMutableAttributedString alloc] initWithString:@"舊時(shí)月色,算幾番照我,梅邊吹笛?喚起玉人,不管清寒與攀摘.何遜而今漸老,都忘卻,春風(fēng)詞筆.但怪得,竹外疏花,春冷入瑤席.江國(guó),正寂寂.嘆寄與路遙,夜雪初積.翠尊易泣,紅萼無(wú)言耿相憶.常記曾攜手處,千樹(shù)壓.梅湖寒碧,又片片吹盡也,何時(shí)得見(jiàn)? "];
    text.yy_font = [UIFont boldSystemFontOfSize:18.0];
    text.yy_color = [UIColor blueColor];

    
    CGSize size = CGSizeMake(300, CGFLOAT_MAX);
    YYTextLayout *layout = [YYTextLayout layoutWithContainerSize:size text:text];
    [layout lineIndexForPoint:CGPointMake(10,10)];
    [layout closestLineIndexForPoint:CGPointMake(10,10)];
    [layout closestPositionToPoint:CGPointMake(10,10)];
    [layout textRangeAtPoint:CGPointMake(10,10)];
    [layout rectForRange:[YYTextRange rangeWithRange:NSMakeRange(10,20)]];
    [layout selectionRectsForRange:[YYTextRange rangeWithRange:NSMakeRange(10,20)]];
    
    YYLabel *label = [[YYLabel alloc] init];
    label.textColor = [UIColor blueColor];
    label.frame = CGRectMake(0.0, 64.0, self.view.bounds.size.width, 500.0);
    label.attributedText= text;
    label.textLayout = layout;
    [self.view addSubview:label];
}
@end

下面看結(jié)果

布局計(jì)算

??我也不清楚這個(gè)例子舉的對(duì)不對(duì),如果有不對(duì)的地方希望大家能批評(píng)真正,歡迎留言。

后記

??最近項(xiàng)目挺忙的,所以能每天寫(xiě)點(diǎn)已經(jīng)抽出很多時(shí)間了,難免有錯(cuò)誤,不對(duì)的地方希望能給我指正出來(lái),謝謝大家。

東北雪景
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 版本記錄 前言 YYText是一個(gè)專(zhuān)門(mén)處理文字的框架,有了它處理文字變得非常方便,這一篇我繼續(xù)介紹YYText的使...
    刀客傳奇閱讀 1,885評(píng)論 9 4
  • 版本記錄 前言 YYText是一個(gè)專(zhuān)門(mén)處理文字的框架,有了它處理文字變得非常方便,這一篇我繼續(xù)介紹YYText的使...
    刀客傳奇閱讀 4,589評(píng)論 0 9
  • 版本記錄 前言 YYText是一個(gè)專(zhuān)門(mén)處理文字的框架,作者是國(guó)內(nèi)的一個(gè)技術(shù)大牛,他有很多框架,還有我們知道的YYM...
    刀客傳奇閱讀 25,997評(píng)論 4 37
  • 版本記錄 前言 YYText是一個(gè)專(zhuān)門(mén)處理文字的框架,有了它處理文字變得非常方便,這一篇我繼續(xù)介紹YYText的使...
    刀客傳奇閱讀 2,210評(píng)論 0 2
  • 這是本月參加的第五場(chǎng)“聚餐”。兩家生娃,兩家婚禮,一家白事。 禮金不多,每次二百。聚餐規(guī)格湊合,差不多幾百一桌。 ...
    Jane漂漂閱讀 1,141評(píng)論 9 12

友情鏈接更多精彩內(nèi)容