在開發(fā)中我們經(jīng)常會需要用到富文本,如果每次使用都去創(chuàng)建設(shè)置的話比較繁瑣,這個時候一個好的框架能幫我們省去不少時間.YYText 就是一個使用比較成熟的框架,現(xiàn)在我們就開始學(xué)習(xí)YYText.
安裝
- 1 CocoasPod 安裝
pod 'YYText'
- 2 手動導(dǎo)入
[YYText] (https://github.com/ibireme/YYText)
手動導(dǎo)入需要添加依賴庫
UIKit
CoreFoundation
CoreText
QuartzCore
Accelerate
MobileCoreServices
內(nèi)容
打開YYText.h
#import <YYText/YYLabel.h>
#import <YYText/YYTextView.h>
#import <YYText/YYTextAttribute.h>
#import <YYText/YYTextArchiver.h>
#import <YYText/YYTextParser.h>
#import <YYText/YYTextRunDelegate.h>
#import <YYText/YYTextRubyAnnotation.h>
#import <YYText/YYTextLayout.h>
#import <YYText/YYTextLine.h>
#import <YYText/YYTextInput.h>
#import <YYText/YYTextDebugOption.h>
#import <YYText/YYTextKeyboardManager.h>
#import <YYText/YYTextUtilities.h>
#import <YYText/NSAttributedString+YYText.h>
#import <YYText/NSParagraphStyle+YYText.h>
#import <YYText/UIPasteboard+YYText.h>
我們主要用到的都在YYLabel 和YYTextView
二者用法類似 我們主要研究YYLabel
@interface YYLabel : UIView <NSCoding>
@property (nullable, nonatomic, copy) IBInspectable NSString *text;
@property (null_resettable, nonatomic, strong) IBInspectable UIColor *textColor;
@property (nullable, nonatomic, strong) IBInspectable NSString *fontName_;
@property (nonatomic) IBInspectable CGFloat fontSize_;
@property (nonatomic) IBInspectable BOOL fontIsBold_;
@property (nonatomic) IBInspectable NSUInteger numberOfLines;
@property (nonatomic) IBInspectable NSInteger lineBreakMode;
@property (nonatomic) IBInspectable CGFloat preferredMaxLayoutWidth;
@property (nonatomic, getter=isVerticalForm) IBInspectable BOOL verticalForm;
@property (nonatomic) IBInspectable NSInteger textAlignment;
@property (nonatomic) IBInspectable NSInteger textVerticalAlignment;
@property (nullable, nonatomic, strong) IBInspectable UIColor *shadowColor;
@property (nonatomic) IBInspectable CGPoint shadowOffset;
@property (nonatomic) IBInspectable CGFloat shadowBlurRadius;
@property (nullable, nonatomic, copy) IBInspectable NSAttributedString *attributedText;
@property (nonatomic) IBInspectable CGFloat insetTop_;
@property (nonatomic) IBInspectable CGFloat insetBottom_;
@property (nonatomic) IBInspectable CGFloat insetLeft_;
@property (nonatomic) IBInspectable CGFloat insetRight_;
@property (nonatomic) IBInspectable BOOL debugEnabled_;
@property (null_resettable, nonatomic, strong) UIFont *font;
@property (nullable, nonatomic, copy) NSAttributedString *truncationToken;
@property (nullable, nonatomic, strong) id<YYTextParser> textParser;
@property (nullable, nonatomic, strong) YYTextLayout *textLayout;
@property (nullable, nonatomic, copy) UIBezierPath *textContainerPath;
@property (nullable, nonatomic, copy) NSArray<UIBezierPath*> *exclusionPaths;
@property (nonatomic) UIEdgeInsets textContainerInset;
@property (nullable, nonatomic, copy) id<YYTextLinePositionModifier> linePositionModifier;
@property (nonnull, nonatomic, copy) YYTextDebugOption *debugOption;
@property (nullable, nonatomic, copy) YYTextAction textTapAction;
@property (nullable, nonatomic, copy) YYTextAction textLongPressAction;
@property (nullable, nonatomic, copy) YYTextAction highlightTapAction;
@property (nullable, nonatomic, copy) YYTextAction highlightLongPressAction;
@property (nonatomic) BOOL displaysAsynchronously;
@property (nonatomic) BOOL clearContentsBeforeAsynchronouslyDisplay;
@property (nonatomic) BOOL fadeOnAsynchronouslyDisplay;
@property (nonatomic) BOOL fadeOnHighlight;
@property (nonatomic) BOOL ignoreCommonProperties;
@end
叢類里面我們可以看到聲明了各種屬性,通過設(shè)置不同的屬性達到要求,這個具體問題的時候我們講解.
使用和測試
- 1 基本用法
YYLabel *label = [YYLabel new];
label.frame = ...
label.font = ...
label.textColor = ...
label.textAlignment = ...
label.lineBreakMode = ...
label.numberOfLines = ...
label.text = ...
- 2 屬性文本
先上代碼
#import <YYText/YYText.h>
NSString* YuJian= @"聽見 冬天的離開 我在某年某月醒過來 我想我等我期待 未來卻不能理智安排 -- 陰天 傍晚車窗外 未來有一個人在等待 向左向右向前看 愛要拐幾個彎才來 我遇見誰會有怎樣的對白 我等的人他在多遠的未來 我聽見風(fēng)來自地鐵和人海 我排著隊拿著愛的號碼牌 我往前飛飛過一片時間海 我們也常在愛情里受傷害 我遇見誰會有怎樣的對白 我等的人他在多遠的未來 我聽見風(fēng)來自地鐵和人海 我排著隊拿著愛的號碼牌 我往前飛飛過一片時間海 我們也常在愛情里受傷害 我看著路夢的入口有點窄 我遇見你是最美麗的意外 @終有一天我的謎底會揭開";
#define kScreenHeight [UIScreen mainScreen].bounds.size.height
#define kScreenWidth [UIScreen mainScreen].bounds.size.width
文字屬性
/**
文字屬性
*/
-(void)test1
{
YYLabel* label=[[YYLabel alloc]initWithFrame:CGRectMake(30, 50, kScreenWidth-60, kScreenHeight-100)];
[self.view addSubview:label];
label.numberOfLines=0;
NSMutableAttributedString* atext=[[NSMutableAttributedString alloc]initWithString:YuJian];
//設(shè)置字體大小
[atext yy_setFont:[UIFont systemFontOfSize:20] range:atext.yy_rangeOfAll];
//局部不同顏色
NSRange range0=[[atext string]rangeOfString:@"冬天的離開"];
[atext yy_setColor:[UIColor blueColor] range:range0];
//設(shè)置行間距
atext.yy_lineSpacing=10;
//設(shè)置下劃線
NSRange range1=[[atext string]rangeOfString:@"我等的人" ];
YYTextDecoration* deco=[YYTextDecoration decorationWithStyle:(YYTextLineStyleSingle) width:[NSNumber numberWithInt:1] color:[UIColor redColor]];
[atext yy_setTextUnderline:deco range:range1];
//陰影
NSRange range2=[[atext string]rangeOfString:@"傍晚車窗外" options:(nil)];
NSShadow* shadow=[[NSShadow alloc]init];
[shadow setShadowColor:[UIColor redColor]];
[shadow setShadowBlurRadius:1];
[shadow setShadowOffset:CGSizeMake(2, 2)];
[atext yy_setShadow:shadow range:range2];
//文本內(nèi)陰影
NSRange range3=[[atext string]rangeOfString:@"我在某年某月醒過來"];
YYTextShadow* dow=[YYTextShadow new];
dow.color=[UIColor yellowColor];
dow.offset=CGSizeMake(0, 2);
dow.radius=1;
[atext yy_setTextShadow:dow range:range3];
label.attributedText=atext;
}
效果
高亮和點擊事件
//高亮文本和點擊事件
-(void)test2
{
YYLabel* label=[[YYLabel alloc]initWithFrame:CGRectMake(30, 50, kScreenWidth-60, kScreenHeight-100)];
[self.view addSubview:label];
label.numberOfLines=0;
NSMutableAttributedString* atext=[[NSMutableAttributedString alloc]initWithString:YuJian];
//設(shè)置字體大小
[atext yy_setFont:[UIFont systemFontOfSize:20] range:atext.yy_rangeOfAll];
NSRange range4=[[atext string] rangeOfString:@"向左向右向前看"];
[atext yy_setTextHighlightRange:range4 color:[UIColor redColor] backgroundColor:[UIColor grayColor] tapAction:^(UIView * _Nonnull containerView, NSAttributedString * _Nonnull text, NSRange range, CGRect rect) {
NSString* str=text.string;
NSLog(@"你點擊了 %@ ---range %ld", [str substringWithRange:range] ,range.length);
}];
label.attributedText=atext;
}
效果
簡單的用法就是這些了,YYText很強大,還有圖文混排等高大上的功能,等樓主抽空再研究講解