版本記錄
版本號(hào) | 時(shí)間 |
---|---|
V1.0 | 2017.06.06 |
前言
YYText是一個(gè)專門處理文字的框架镊掖,有了它處理文字變得非常方便,這一篇我繼續(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í)得見(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í)得見(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)目挺忙的蒙谓,所以能每天寫點(diǎn)已經(jīng)抽出很多時(shí)間了,難免有錯(cuò)誤训桶,不對(duì)的地方希望能給我指正出來(lái)累驮,謝謝大家。
東北雪景