版本记录
版本号 | 时间 |
---|---|
V1.0 | 2017.06.06 |
前言
YYText是一个专门处理文字的框架,有了它处理文字变得非常方便,这一篇我继续介绍YYText的使用方法,希望对大家能有所帮助。大家如感兴趣还可以参考:
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;
下面我们看个例子
#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:@"旧时月色,算几番照我,梅边吹笛?唤起玉人,不管清寒与攀摘.何逊而今渐老,都忘却,春风词笔. "];
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:@"但怪得,竹外疏花,春冷入瑶席.江国,正寂寂.叹寄与路遥,夜雪初积.翠尊易泣,红萼无言耿相忆.常记曾携手处,千树压.梅湖寒碧,又片片吹尽也,何时得见?"];
[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
下面看输出结果
二、YYText布局计算
下面看YYText的图文混排布局计算,还是先看代码吧。
#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 layoutCaculation];
}
#pragma mark - Object Private Function
//布局计算
- (void)layoutCaculation
{
NSMutableAttributedString *text = [[NSMutableAttributedString alloc] initWithString:@"旧时月色,算几番照我,梅边吹笛?唤起玉人,不管清寒与攀摘.何逊而今渐老,都忘却,春风词笔.但怪得,竹外疏花,春冷入瑶席.江国,正寂寂.叹寄与路遥,夜雪初积.翠尊易泣,红萼无言耿相忆.常记曾携手处,千树压.梅湖寒碧,又片片吹尽也,何时得见? "];
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
下面看结果
我也不清楚这个例子举的对不对,如果有不对的地方希望大家能批评真正,欢迎留言。
后记
最近项目挺忙的,所以能每天写点已经抽出很多时间了,难免有错误,不对的地方希望能给我指正出来,谢谢大家。