MPITextKit
MPITextKit 是一个强大的基于 TextKit 的文本框架,用于在 iOS 上显示富文本,灵感来自 Texture 和 YYText。
功能
- 与 UILabel API 兼容
- 支持 Autolayout
- 使用 TextKit 渲染,因此处理所有文本属性,例如 NSAttachmentAttributeName,NSStrikethroughStyleAttributeName,NSUnderlineStyleAttributeName 等。
- 高性能异步文本布局和渲染
- 使用 UIImage、UIView 和 CALayer 的文本附件
- 排除路径
- 自定义属性
- 自定义截断令牌
- 调试文本布局
- 文本选择
需求
- iOS 9+
安装
MPITextKit 支持多种方法将库安装到项目中。
使用 CocoaPods 安装
pod 'MPITextKit'
使用 Swift Package Manager 安装
一旦设置了 Swift 包,将 MPITextKit 作为依赖项添加就只需要将其添加到 Package.swift
的 dependencies
值中。
dependencies: [
.package(url: "https://github.com/meitu/MPITextKit.git")
]
使用 Carthage 安装
Carthage 是一个去中心化的依赖项管理器,它构建您的依赖项并提供二进制框架。要将 MPITextKit 集成到项目中,将以下内容添加到您的 Cartfile
。
github "meitu/MPITextKit"
用法
基本
MPILabel *label = [MPILabel new];
label.text = @"foo";
label.font = [UIFont systemFontOfSize:16];
label.textAlignment = NSTextAlignmentCenter;
label.textVerticalAlignment = MPITextVerticalAlignmentTop;
label.textColor = [UIColor whiteColor];
label.textContainerInset = UIEdgeInsetsMake(8, 8, 8, 8);
属性化文字
NSMutableAttributedString *attributedText = [[NSMutableAttributedString alloc] initWithString:@"bar"];
// Append Attachment.
MPITextAttachment *attachment = [MPITextAttachment new];
attachment.content = [UIImage imageNamed:@"img"];
attachment.verticalAligment = MPITextVerticalAlignmentCenter;
NSAttributedString *attachmetText = [NSAttributedString attributedStringWithAttachment:attachment];
[text appendAttributedString:attachmetText];
MPILabel *label = [MPILabel new];
label.numberOfLines = 0;
label.delegate = self; /// Interact Links.
label.attributedText = attributedText;
// Truncation Token ...
label.truncationAttributedToken = MPITextDefaultTruncationAttributedToken();
// Additional Truncation Message.
label.additionalTruncationAttributedMessage = [[NSAttributedString alloc] initWithString:@"more" attributes:@{NSForegroundColorAttributeName: [UIColor colorWithRed:0.000 green:0.449 blue:1.000 alpha:1.000], MPITextLinkAttributeName: [MPITextLink new]}];
[label sizeToFit];
label.center = self.view.center;
[self.view addSubview:label];
尺寸计算
NSAttributedString *attributedText = [[NSAttributedString alloc] initWithString:text attributes:@{NSFontAttributeName: [UIFont preferredFontForTextStyle:UIFontTextStyleBody]}];
NSAttributedString *token = MPITextDefaultTruncationAttributedToken();
NSAttributedString *additionalMessage =
[[NSAttributedString alloc] initWithString:@"more" attributes:@{NSForegroundColorAttributeName: [UIColor colorWithRed:0.000 green:0.449 blue:1.000 alpha:1.000], MPITextLinkAttributeName: [MPITextLink new]}];
NSAttributedString *truncationAttriubtedText = MPITextTruncationAttributedTextWithTokenAndAdditionalMessage(attributedText, token, additionalMessage);
CGSize fitsSize = CGSizeMake(CGRectGetWidth(self.view.frame) - 30, CGFLOAT_MAX);
// You can change it for testing
NSUInteger numberOfLines = 5;
MPITextRenderAttributes *renderAttributes = [MPITextRenderAttributes new];
renderAttributes.attributedText = attributedText;
renderAttributes.truncationAttributedText = truncationAttriubtedText;
renderAttributes.maximumNumberOfLines = numberOfLines;
// Result of calculation, you can use it do something.
CGSize textSize = MPITextSuggestFrameSizeForAttributes(renderAttributes, fitsSize, UIEdgeInsetsZero);
调试
MPITextDebugOption *debugOptions = [MPITextDebugOption new];
debugOptions.baselineColor = [[UIColor redColor] colorWithAlphaComponent:0.5];
debugOptions.lineFragmentBorderColor = [[UIColor redColor] colorWithAlphaComponent:0.200];
debugOptions.lineFragmentUsedBorderColor = [UIColor colorWithRed:0.000 green:0.463 blue:1.000 alpha:0.200];
debugOptions.glyphBorderColor = [UIColor colorWithRed:1.000 green:0.524 blue:0.000 alpha:0.200];
[MPITextDebugOption setSharedDebugOption:debugOptions];
注意
- 在使用AutoLayout多行文本显示时,建议设置
preferredMaxLayoutWidth
以提高性能。 - 不要同时使用
text
和attributedText
。 - 自定义属性时,应重写
- isEqual:
和- hash
方法。
参考
- Texture:iOS应用的流畅异步用户界面。
- YYText:iOS强大的富文本显示和编辑框架。
- STULabel:一个速度更快、更灵活的iOS标签视图。
- Neat:修复TextKit的行高问题。
- Cocoa文本系统:Cocoa文本系统的介绍。
许可协议
MPITextKit
在 MIT 许可下发布。请参阅 LICENSE 文件以获取详细信息。