CocoaPods trunk is moving to be read-only. Read more on the blog, there are 19 months to go.

YPSeniorLabel 0.5.2

YPSeniorLabel 0.5.2

Maintained by lypJTD, Doit, zhenglibao.



  • By
  • liuyaping

Build Status Cocopods Pod License iOS Version

Features

  • 图文混排、链接点击、图片点击
  • emoji、链接(网址、电话、邮箱)自动检测
  • 文字选择、自定义UIMenuItem
  • 放大镜
  • 异步绘制
  • 自定义文字边框
  • 链接的颜色、点击高亮颜色、点击背景高亮颜色等颜色高度订制
  • NSMutableAttributedString 扩展,提高开发效率
  • 独立的富文本size计算方法
  • 无需了解CoreText

接入

pod 'YPSeniorLabel'

简单用法介绍

YPSeniorLabel的核心是通过自定义富文本,然后交由YPSeniorLabel来做显示。我们要实现各种自定义样式,就需要通过NSMutableAttributedString+YP.h这个扩展来创建各种富文本对象;而NSMutableAttributedString+YP.h类中实现高亮链接,文字背景颜色,点击背景颜色,图片都是通过YPSeniorAttribute.h类里面的YPLinkAttribute、YPAttachmentAttribute、YPBorderAttribute来实现自定义。

  • 链接

根据实际应用,选择以下合适的方式:

方式一:创建YPLinkAttribute对象方法

NSMutableAttributedString *allString = [[NSMutableAttributedString alloc] initWithString:@"我们来写一个最复杂的连接"];
NSMutableAttributedString *stringAttribute = [[NSMutableAttributedString alloc] initWithString:@"这是一个连接"];
YPLinkAttribute *link = [[YPLinkAttribute alloc] init];
//点击后文本高亮的颜色
link.highlightColor = [UIColor greenColor];
//点击后文本背景高亮颜色
link.highlightBackViewColor = [UIColor redColor];
//链接事件
link.clickAction = ^(NSAttributedString *attrStr, NSRange textRange) {
//响应事件
};

[stringAttribute setLink:link range:NSMakeRange(0, stringAttribute.length)];

[allString appendAttributedString:stringAttribute];

方式二:NSMutableAttributedString+YP.h 扩展里的方法

NSMutableAttributedString *allString = [[NSMutableAttributedString alloc] initWithString:@"我们来写一个最复杂的连接"];
NSMutableAttributedString *stringAttribute = [[NSMutableAttributedString alloc] initWithString:@"这是一个连接"];

[stringAttribute configHighLightColor:[UIColor greenColor] tapBackViewColor:[UIColor redColor] clickAction:^(NSAttributedString *attrStr, NSRange textRange) {

}];
[allString appendAttributedString:stringAttribute];
  • 文字边界样式

同链接,使用创建YPBorderAttribute对象方法 或者 使用NSMutableAttributedString+YP.h 扩展里的方法。

  • 配置图片和emoji

/*
 Config attachment
 
 @param content                 Temporary support only image.
 @param attachmentSize          The content size.
 @param font                    The content will align to the font.
 @param attachmentAlignment     Alignment.
 @param clickAction             Click action call back.
 
 return attachment's attributed string

 */
+ (NSMutableAttributedString *)configAttachmentStringContent:(id)content
                                              attachmentSize:(CGSize)attachmentSize
                                                 alignToFont:(UIFont *)font
                                         attachmentAlignment:(YPAttachmentAlignment)attachmentAlignment
                                                 clickAction:(nullable YPActionBlock)clickAction;
/*
 Config Emoji
 
 @param image           Image.
 @param imageSize       Image size.
 @param font            The content will align to the font.
 @param emojiString     A string used for original emoji string.
 
 return image's attributed string
 */

+ (NSMutableAttributedString *)configEmojiStringImage:(UIImage *)image
                                            imageSize:(CGSize)imageSize
                                          alignToFont:(UIFont *)font
                                          emojiString:(NSString *)emojiString;
  • 快捷配置聊天富文本

根据实际应用,选择以下合适的方式:

方式一:通过YPSeniorLabel类

/**
 Setting with detecting config
 
 @param detectType       The detect type
 @param emojiDic         The emoji dictionary which key is string and value is iamge name.
 @param emojiSize        Emoji size. If CGSizeZero, use the emoji image size.
 @param emojiBundle      Emoji bundle. If nil, use main bundle.
 @param linkColor        The link color.
 @param underlineStyle   The link underline style.
 @param underlineColor   The link underline color, default is nil which same as link color.
 */
- (void)configAutoDetectWithDetectType:(YPAutoDetectCheckType)detectType
                              emojiDic:(NSDictionary *)emojiDic
                             emojiSize:(CGSize)emojiSize
                           emojiBundle:(NSBundle *)emojiBundle
                             linkColor:(UIColor *)linkColor
                        underlineStyle:(NSUnderlineStyle)underlineStyle
                        underlineColor:(UIColor *)underlineColor;

方式二:通过YPSeniorLayout类配置富文本

/*
 Create auto detect attribute string.
 
 @param originalAttr  The original attribte string.
 @param detectConfig  The auto detect config.
 */
+ (NSMutableAttributedString *)createAutoDetectAttrWithOriginalAttr:(NSAttributedString*)originalAttr
                                                       detectConfig:(YPAutoDetectConfig *)detectConfig;
  • 获取文本Size

根据实际应用,选择以下合适的方式:

方式一:通过YPSeniorLabel类

/*
 Get current label object content size.
 
 @param constraintWidth The constraint width.
 */
- (CGSize)seniorLabelContentSizeWithConstraintWidth:(CGFloat)constraintWidth;

/*
 Get current label object content size.
 
 @param constraintSize The constraint size.
 */
- (CGSize)seniorLabelContentSizeWithConstraintSize:(CGSize)constraintSize;

方式二:通过YPSeniorLayout类

/*
 @param attribureStr        The string with attributes.
 @param constraintWidth     The restrict width.
 @param numberOfLines       The numberOfLines.
 
 return The content height.
 */
+ (CGFloat)contentHeightWithAttribureStr:(NSAttributedString *)attribureStr
                         constraintWidth:(CGFloat)constraintWidth
                           numberOfLines:(NSInteger)numberOfLines;

/*
 @param attribureStr        The string with attributes.
 @param constraintSize      The restrict size.
 @param numberOfLines       The numberOfLines.
 
 return The content size.
 */
+ (CGSize)contentSizeWithAttribureStr:(NSAttributedString *)attribureStr
                       constraintSize:(CGSize)constraintSize
                        numberOfLines:(NSInteger)numberOfLines;
  • YPSeniorLabel Properties

YPSeniorLabel类里的属性均有详细注释,重点讲解以下几个:

@property (nonatomic, assign) BOOL clearContentWhenRedraw;

在绘制之前是否先清除,画布上的内容。然后再计算文本,进行绘制。默认是YES
@property (nonatomic, assign) BOOL seniorDrawsAsynchronously;

是否开启异步绘制,不阻塞主线程。默认是开启的,如果内容太多,刷新时候感觉闪动,则设置为NO即可。
@property (nonatomic, assign) BOOL ignorGeneralProperties;

这是一个优化开关。在使用-configAutoDetectWithDetectType:emojiDic:emojiSize:tapBackViewColor:highLightColor:linkColor这个方法时候才需要设置。
YPSeniorLabel 一些有关于文本样式的属性在设置(attributedString,font,textColor,numberOfLines,lineBreakMode,alignment,truncationAttrStr)的时候我会忽略他的单次重绘,直到configAutoDetectWithDetectType方法调用的时候才进行绘制。
  • NSMutableAttributedString+YP.h

此扩展里有许多便捷方法,提高开发效率。例如:

NSMutableAttributedString *test = [[NSMutableAttributedString alloc] initWithString:@"Boss直聘"];
//点语法设置 字体
test.font = [UIFont systemFontOfSize:26];
//点语法设置 颜色
test.color = COLOR_333333;
//段落属性
test.lineSpacing = 4;
  • 选择

支持长按选择 、支持自定义YPMenuItem只需要遵循YPSeniorLabel里面的协议即可、支持放大镜。具体用法请参考demo。

  • 总结

以上为简单介绍,详细用法请参考demo、查阅YPSeniorLabel里的各种属性。

Licenses

All source code is licensed under the MIT License.