iOS

最近更新时间:2025-12-23 16:43:42

我的收藏

功能概览

通过贴纸与文字操作的接口类和方法,包括创建、加载、移除贴纸,以及设置贴纸属性(如时间区间、动画模式、位置等)。本文还提供了使用示例和贴纸数据模型的说明。

相关接口类

接口类名
描述
TAVEditor
SDK 操作入口类。
ITAVStickerManager
贴纸操作入口类。
TAVMediaStickerItem
贴纸数据模型类。

贴纸操作相关方法

获取贴纸操作接口

贴纸操作封装到了单独的操作接口,可通过TAVEditor直接进行获取:
// 获取TAVEditor api
NSString *bundlePath = [[NSBundle mainBundle] pathForResource:@"LightCore" ofType:@"bundle"];
self.tavEditor = [[TAVEditor alloc] initWithRenderSize:CGSizeZero assetsPath:bundlePath];
// 获取贴纸操作接口
id<ITAVStickerManager> stickerManager = [self.tavEditor getStickerManager];

创建贴纸数据模型

贴纸的数据全部保存在贴纸数据模型TAVMediaStickerItem中,所以在操作贴纸之前,必须先要有TAVMediaStickerItem对象,
创建贴纸对象时,需要传入贴纸文件路径,并且设置贴纸显示的时间区间setTimeRange,不设置默认为整个资源时长。
// 1、创建一个文本贴纸
NSString *stickerPath = [[NSBundle mainBundle] pathForResource:@"text_default" ofType:@"bundle"];
TAVMediaStickerItem *stickerItem = [stickerManager createStickerItem:textPath stickerType:TAVStickerTypeText];
// 设置文本贴纸字体
// 获取字体font,fontPath为字体路径,fontFamily和fontStyle不知道可填空字符串
PAGFont *pagFont = [PAGFont RegisterFont:fontPath
family:fontFamily
style:fontStyle];
stickerItem.font = pagfont;
stickerItem.timeRange = timerange;

// 2、创建一个图片贴纸
NSString *stickerPath = [[NSBundle mainBundle] pathForResource:@"image_default" ofType:@"bundle"];
TAVMediaStickerItem *stickerItem = [stickerManager createStickerItem:stickerPath stickerType:TAVStickerTypeImage];
// 设置图片
NSString *imagePath = ....;
stickerItem.imageFilePath = imagePath;

// 3、创建一个动态类贴纸
// 这是指向main.pag的一个路径。sticker__plfttz_01.bundle这个下面有main.pag文件
NSString *stickerPath = [[NSBundle mainBundle] pathForResource:@"sticker__plfttz_01" ofType:@"bundle"];
TAVMediaStickerItem *stickerItem = [[TAVMediaStickerItem alloc] initWithStickerID:@"stickerPlfttz01Id" resourceInfoPath:stickerPath];


加载一个贴纸

通过 获取贴纸操作接口 取得的贴纸操作接口TAVStickerManager可以加载贴纸。
[stickerManager loadStickerItem:stickerItem];

创建自定义的贴纸编辑视图

SDK 内部内置了默认的贴纸编辑视图,但如果认为 SDK 内部默认的贴纸编辑视图不足以满足业务需求,可以自行实现创建贴纸编辑视图。
首先,设置贴纸视图的数据源(遵守 ITAVStickerViewDataSource 协议)代理:
@protocol ITAVStickerViewDataSource <NSObject>

/// 根据贴纸对象返回贴纸视图,可继承自TAVStickerView自定义某些控件
/// - Parameter stickerItem: 贴纸对象
- (TAVStickerView *)stickerViewWithStickerItem:(TAVMediaStickerItem *)stickerItem;
@end
- (void)setStickerViewDataSource:(id<ITAVStickerViewDataSource>)dataSource;


然后,在代理类内实现以下代理函数,返回创建之后的贴纸 View。
@interface MyStickerContentView : TAVStickerView
/// ...自行实现业务逻辑
@end


#pragma mark - ITAVStickerViewDataSource
- (TAVStickerView *)stickerViewWithStickerItem:(TAVMediaStickerItem *)stickerItem
{
// 自定义的贴纸View需要继承自TAVStickerView
MyStickerContentView *stickerView = [[TAVMagicStickerView alloc] initWithStickerItem:stickerItem];
return stickerView;
}

设置自定义的贴纸容器布局

SDK 内部内置了空的贴纸容器布局,如果认为 SDK 内部默认的贴纸容器布局不足以满足业务需求(例如需要实现贴纸对齐辅助线之类的需求),可以自行实现创建贴纸容器布局。
首先,实现一个自己的贴纸容器布局:
@interface MyStickerContentView : TAVStickerContentView
/// ...自行实现业务逻辑
@end
然后,调用TAVStickerManager中的setStickerContentView接口设置自定义的贴纸容器布局。
/// 绑定添加贴纸的容器视图
/// - Parameter stickerContentView: 需要继承自TAVStickerContentView
- (void)setStickerContentView:(TAVStickerContentView *)stickerContentView;

修改文字贴纸文字

修改文字贴纸中的文字只需要 .text 属性。
// 设置文字
stickerItem.text = @"";
// 设置字体
PAGFont *pagFont = [PAGFont RegisterFont:fontPath
family:fontFamily
style:fontStyle];
stickerItem.font = pagfont;
其他属性可以看 TAVMediaStickerItem.h:
@property (nonatomic, copy) NSString *text; //文本贴纸的文本
@property (nonatomic, strong, readonly) NSString *defaultText; //文本贴纸的默认文本
@property (nonatomic, strong) UIColor *textColor; //文本贴纸的文字颜色
@property (nonatomic, strong) UIColor *bgColor; //整个贴纸背景色
@property (nonatomic, strong) PAGFont *pagFont; //文字字体
/// 描边
@property (nonatomic, assign) BOOL applyTextStroke; //开启文字描边
@property (nonatomic, assign) CGFloat textStrokeWidth; //文字描边厚度
@property (nonatomic, strong) UIColor *textStrokeColor; //文字描边颜色

/// 文字背景
@property (nonatomic, strong) UIColor *textBgColor; //文字背景颜色
@property (nonatomic, assign) CGFloat textBgAlpha; //文字背景透明度,取值范围 0 - 1


修改自定义图片贴纸图片

图片贴纸是通过替换占位图层实现图片替换。
// 2、创建一个图片贴纸
NSString *stickerPath = [[NSBundle mainBundle] pathForResource:@"image_default" ofType:@"bundle"];
TAVMediaStickerItem *stickerItem = [[TAVMediaStickerItem alloc] initWithStickerID:@"imageDefaultId" resourceInfoPath:stickerPath];
// 切换图片
TAVStickerImageItem *imageItem = stickerItem.sticker.imageList.firstObject;
NSString *imagePath = ....;
imageItem.filePath = imagePath;