版本记录
版本号 | 时间 |
---|---|
V1.0 | 2017.06.08 |
前言
YYText是一个专门处理文字的框架,有了它处理文字变得非常方便,这一篇我继续介绍YYText的使用方法,希望对大家能有所帮助。大家如感兴趣还可以参考:
1.YYText使用篇(一)
2.YYText使用篇(二)
3.YYText使用篇(三)
4.YYText使用篇(四)
一、YYText示例
下面我们就举一个Demo,主要是对不同文字的处理,包括中空、阴影和高亮等多种状态,还是先看代码吧。
#import "YYTextAttributeExample.h"
#import "YYText.h"
#import "YYTextExampleHelper.h"
#import "UIImage+YYWebImage.h"
#import "UIView+YYAdd.h"
#import "NSString+YYAdd.h"
@implementation YYTextAttributeExample
#pragma mark - Override Base Function
- (void)viewDidLoad
{
[super viewDidLoad];
self.view.backgroundColor = [UIColor whiteColor];
__weak typeof(self) _self = self;
[YYTextExampleHelper addDebugOptionToViewController:self];
NSMutableAttributedString *text = [NSMutableAttributedString new];
{
//文字1
NSMutableAttributedString *one = [[NSMutableAttributedString alloc] initWithString:@"Shadow"];
one.yy_font = [UIFont boldSystemFontOfSize:30];
one.yy_color = [UIColor whiteColor];
YYTextShadow *shadow = [YYTextShadow new];
shadow.color = [UIColor colorWithWhite:0.000 alpha:0.490];
shadow.offset = CGSizeMake(0, 1);
shadow.radius = 5;
one.yy_textShadow = shadow;
[text appendAttributedString:one];
[text appendAttributedString:[self padding]];
}
//文字2
{
NSMutableAttributedString *one = [[NSMutableAttributedString alloc] initWithString:@"Inner Shadow"];
one.yy_font = [UIFont boldSystemFontOfSize:30];
one.yy_color = [UIColor whiteColor];
YYTextShadow *shadow = [YYTextShadow new];
shadow.color = [UIColor colorWithWhite:0.000 alpha:0.40];
shadow.offset = CGSizeMake(0, 1);
shadow.radius = 1;
one.yy_textInnerShadow = shadow;
[text appendAttributedString:one];
[text appendAttributedString:[self padding]];
}
//文字3
{
NSMutableAttributedString *one = [[NSMutableAttributedString alloc] initWithString:@"Multiple Shadows"];
one.yy_font = [UIFont boldSystemFontOfSize:30];
one.yy_color = [UIColor colorWithRed:1.000 green:0.795 blue:0.014 alpha:1.000];
YYTextShadow *shadow = [YYTextShadow new];
shadow.color = [UIColor colorWithWhite:0.000 alpha:0.20];
shadow.offset = CGSizeMake(0, -1);
shadow.radius = 1.5;
YYTextShadow *subShadow = [YYTextShadow new];
subShadow.color = [UIColor colorWithWhite:1 alpha:0.99];
subShadow.offset = CGSizeMake(0, 1);
subShadow.radius = 1.5;
shadow.subShadow = subShadow;
one.yy_textShadow = shadow;
YYTextShadow *innerShadow = [YYTextShadow new];
innerShadow.color = [UIColor colorWithRed:0.851 green:0.311 blue:0.000 alpha:0.780];
innerShadow.offset = CGSizeMake(0, 1);
innerShadow.radius = 1;
one.yy_textInnerShadow = innerShadow;
[text appendAttributedString:one];
[text appendAttributedString:[self padding]];
}
//文字4
{
NSMutableAttributedString *one = [[NSMutableAttributedString alloc] initWithString:@"Background Image"];
one.yy_font = [UIFont boldSystemFontOfSize:30];
one.yy_color = [UIColor colorWithRed:1.000 green:0.795 blue:0.014 alpha:1.000];
CGSize size = CGSizeMake(20, 20);
UIImage *background = [UIImage yy_imageWithSize:size drawBlock:^(CGContextRef context) {
UIColor *c0 = [UIColor colorWithRed:0.054 green:0.879 blue:0.000 alpha:1.000];
UIColor *c1 = [UIColor colorWithRed:0.869 green:1.000 blue:0.030 alpha:1.000];
[c0 setFill];
CGContextFillRect(context, CGRectMake(0, 0, size.width, size.height));
[c1 setStroke];
CGContextSetLineWidth(context, 2);
for (int i = 0; i < size.width * 2; i+= 4) {
CGContextMoveToPoint(context, i, -2);
CGContextAddLineToPoint(context, i - size.height, size.height + 2);
}
CGContextStrokePath(context);
}];
one.yy_color = [UIColor colorWithPatternImage:background];
[text appendAttributedString:one];
[text appendAttributedString:[self padding]];
}
//文字5
{
NSMutableAttributedString *one = [[NSMutableAttributedString alloc] initWithString:@"Border"];
one.yy_font = [UIFont boldSystemFontOfSize:30];
one.yy_color = [UIColor colorWithRed:1.000 green:0.029 blue:0.651 alpha:1.000];
YYTextBorder *border = [YYTextBorder new];
border.strokeColor = [UIColor colorWithRed:1.000 green:0.029 blue:0.651 alpha:1.000];
border.strokeWidth = 3;
border.lineStyle = YYTextLineStylePatternCircleDot;
border.cornerRadius = 3;
border.insets = UIEdgeInsetsMake(0, -4, 0, -4);
one.yy_textBackgroundBorder = border;
[text appendAttributedString:[self padding]];
[text appendAttributedString:one];
[text appendAttributedString:[self padding]];
[text appendAttributedString:[self padding]];
[text appendAttributedString:[self padding]];
[text appendAttributedString:[self padding]];
}
//文字6
{
NSMutableAttributedString *one = [[NSMutableAttributedString alloc] initWithString:@"Link"];
one.yy_font = [UIFont boldSystemFontOfSize:30];
one.yy_underlineStyle = NSUnderlineStyleSingle;
/// 1. you can set a highlight with these code
/*
one.yy_color = [UIColor colorWithRed:0.093 green:0.492 blue:1.000 alpha:1.000];
YYTextBorder *border = [YYTextBorder new];
border.cornerRadius = 3;
border.insets = UIEdgeInsetsMake(-2, -1, -2, -1);
border.fillColor = [UIColor colorWithWhite:0.000 alpha:0.220];
YYTextHighlight *highlight = [YYTextHighlight new];
[highlight setBorder:border];
highlight.tapAction = ^(UIView *containerView, NSAttributedString *text, NSRange range, CGRect rect) {
[_self showMessage:[NSString stringWithFormat:@"Tap: %@",[text.string substringWithRange:range]]];
};
[one yy_setTextHighlight:highlight range:one.yy_rangeOfAll];
*/
/// 2. or you can use the convenience method
[one yy_setTextHighlightRange:one.yy_rangeOfAll
color:[UIColor colorWithRed:0.093 green:0.492 blue:1.000 alpha:1.000]
backgroundColor:[UIColor colorWithWhite:0.000 alpha:0.220]
tapAction:^(UIView *containerView, NSAttributedString *text, NSRange range, CGRect rect){
[_self showMessage:[NSString stringWithFormat:@"Tap: %@",[text.string substringWithRange:range]]];
}];
[text appendAttributedString:one];
[text appendAttributedString:[self padding]];
}
//文字7
{
NSMutableAttributedString *one = [[NSMutableAttributedString alloc] initWithString:@"Another Link"];
one.yy_font = [UIFont boldSystemFontOfSize:30];
one.yy_color = [UIColor redColor];
YYTextBorder *border = [YYTextBorder new];
border.cornerRadius = 50;
border.insets = UIEdgeInsetsMake(0, -10, 0, -10);
border.strokeWidth = 0.5;
border.strokeColor = one.yy_color;
border.lineStyle = YYTextLineStyleSingle;
one.yy_textBackgroundBorder = border;
YYTextBorder *highlightBorder = border.copy;
highlightBorder.strokeWidth = 0;
highlightBorder.strokeColor = one.yy_color;
highlightBorder.fillColor = one.yy_color;
YYTextHighlight *highlight = [YYTextHighlight new];
[highlight setColor:[UIColor whiteColor]];
[highlight setBackgroundBorder:highlightBorder];
highlight.tapAction = ^(UIView *containerView, NSAttributedString *text, NSRange range, CGRect rect) {
[_self showMessage:[NSString stringWithFormat:@"Tap: %@",[text.string substringWithRange:range]]];
};
[one yy_setTextHighlight:highlight range:one.yy_rangeOfAll];
[text appendAttributedString:one];
[text appendAttributedString:[self padding]];
}
//文字8
{
NSMutableAttributedString *one = [[NSMutableAttributedString alloc] initWithString:@"Yet Another Link"];
one.yy_font = [UIFont boldSystemFontOfSize:30];
one.yy_color = [UIColor whiteColor];
YYTextShadow *shadow = [YYTextShadow new];
shadow.color = [UIColor colorWithWhite:0.000 alpha:0.490];
shadow.offset = CGSizeMake(0, 1);
shadow.radius = 5;
one.yy_textShadow = shadow;
YYTextShadow *shadow0 = [YYTextShadow new];
shadow0.color = [UIColor colorWithWhite:0.000 alpha:0.20];
shadow0.offset = CGSizeMake(0, -1);
shadow0.radius = 1.5;
YYTextShadow *shadow1 = [YYTextShadow new];
shadow1.color = [UIColor colorWithWhite:1 alpha:0.99];
shadow1.offset = CGSizeMake(0, 1);
shadow1.radius = 1.5;
shadow0.subShadow = shadow1;
YYTextShadow *innerShadow0 = [YYTextShadow new];
innerShadow0.color = [UIColor colorWithRed:0.851 green:0.311 blue:0.000 alpha:0.780];
innerShadow0.offset = CGSizeMake(0, 1);
innerShadow0.radius = 1;
YYTextHighlight *highlight = [YYTextHighlight new];
[highlight setColor:[UIColor colorWithRed:1.000 green:0.795 blue:0.014 alpha:1.000]];
[highlight setShadow:shadow0];
[highlight setInnerShadow:innerShadow0];
[one yy_setTextHighlight:highlight range:one.yy_rangeOfAll];
[text appendAttributedString:one];
}
YYLabel *label = [YYLabel new];
label.attributedText = text;
label.width = self.view.width;
label.height = self.view.height - (kiOS7Later ? 64 : 44);
label.top = (kiOS7Later ? 64 : 0);
label.textAlignment = NSTextAlignmentCenter;
label.textVerticalAlignment = YYTextVerticalAlignmentCenter;
label.numberOfLines = 0;
label.backgroundColor = [UIColor colorWithWhite:0.933 alpha:1.000];
[self.view addSubview:label];
/*
If the 'highlight.tapAction' is not nil, the label will invoke 'highlight.tapAction'
and ignore 'label.highlightTapAction'.
If the 'highlight.tapAction' is nil, you can use 'highlightTapAction' to handle
all tap action in this label.
*/
label.highlightTapAction = ^(UIView *containerView, NSAttributedString *text, NSRange range, CGRect rect) {
[_self showMessage:[NSString stringWithFormat:@"Tap: %@",[text.string substringWithRange:range]]];
};
}
#pragma mark - Object Private Function
- (NSAttributedString *)padding
{
NSMutableAttributedString *pad = [[NSMutableAttributedString alloc] initWithString:@"\n\n"];
pad.yy_font = [UIFont systemFontOfSize:4];
return pad;
}
- (void)showMessage:(NSString *)msg
{
CGFloat padding = 10;
YYLabel *label = [YYLabel new];
label.text = msg;
label.font = [UIFont systemFontOfSize:16];
label.textAlignment = NSTextAlignmentCenter;
label.textColor = [UIColor whiteColor];
label.backgroundColor = [UIColor colorWithRed:0.033 green:0.685 blue:0.978 alpha:0.730];
label.width = self.view.width;
label.textContainerInset = UIEdgeInsetsMake(padding, padding, padding, padding);
label.height = [msg heightForFont:label.font width:label.width] + 2 * padding;
label.bottom = (kiOS7Later ? 64 : 0);
[self.view addSubview:label];
[UIView animateWithDuration:0.3 animations:^{
label.top = (kiOS7Later ? 64 : 0);
}
completion:^(BOOL finished) {
[UIView animateWithDuration:0.2 delay:2 options:UIViewAnimationOptionCurveEaseInOut animations:^{
label.bottom = (kiOS7Later ? 64 : 0);
} completion:^(BOOL finished) {
[label removeFromSuperview];
}];
}];
}
@end
二、YYText效果
下面看效果图片
后记
未完,待续,还会有很多的示例展示给大家。