Latest 0.1.9
Homepage https://github.com/LYPDoit/YPSeniorLabel
License MIT
Platforms ios 7.0
Frameworks Foundation, CoreText, UIKit
Authors

Build Status
Cocopods
Pod License
iOS Version

Features

  • [x] 图文混排、链接点击、图片点击
  • [x] emoji、链接(网址、电话、邮箱)自动检测
  • [x] 文字选择、自定义UIMenuItem
  • [x] 放大镜
  • [x] 异步绘制
  • [x] 自定义文字边框
  • [x] 链接的颜色、点击高亮颜色、点击背景高亮颜色等颜色高度订制
  • [x] NSMutableAttributedString 扩展,提高开发效率
  • [x] 独立的富文本size计算方法
  • [x] 无需了解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) {
//响应事件
};
//链接范围
link.linkRange = NSMakeRange(allString.length, stringAttribute.length);

[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] textRange:NSMakeRange(allString.length, stringAttribute.length) 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类
```objective-c
/*
 Setting with detecting type

 @param detectType          The detect type
 @param emojiDic            The emoji dictionary which key is string and value is iamge name.
 @param emojiSize           Emoji size. If it is CGSizeZero ,use the emoji image size
 @param tapBackViewColor    Change back view color when click link.
 @param highLightColor      Change string color when click link.
 @param linkColor           Link string's color.
 @param clickLink           Customize the click link operation, callling [[UIApplication  sharedApplication] openURL when nil.

 */
- (void)configAutoDetectWithDetectType:(YPAutoDetectCheckType)detectType
                              emojiDic:(NSDictionary *)emojiDic
                             emojiSize:(CGSize)emojiSize
                      tapBackViewColor:(UIColor *)tapBackViewColor
                        highLightColor:(UIColor *)highLightColor
                             linkColor:(UIColor *)linkColor;

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

/*
 Setting with detecting type

 @param detectType The detect type
 @param emojiDic The emoji dictionary which key is string and value is iamge name.
 @param emojiSize emoji size. If it is CGSizeZero ,use the emoji image size
 @param tapBackViewColor Change back view color when click link.
 @param highLightColor Change string color when click link.
 @param linkColor Link string's color.
 @param clickLink Customize the click link operation, callling [[UIApplication sharedApplication] openURL when nil.

 */
+ (NSMutableAttributedString *)configAutoDetectWith:(NSMutableAttributedString *)atrributeString
                                         detectType:(YPAutoDetectCheckType)detectType
                                           emojiDic:(NSDictionary *)emojiDic
                                          emojiSize:(CGSize)emojiSize
                                   tapBackViewColor:(UIColor *)tapBackViewColor
                                     highLightColor:(UIColor *)highLightColor
                                          linkColor:(UIColor *)linkColor;
  • 获取文本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 numberOfLines.
 return The content size.
 */
+ (CGSize)contentHeightWithAttribureStr:(NSAttributedString *)attribureStr
                        constraintWidth:(CGFloat)constraintWidth
                          numberOfLines:(NSInteger)numberOfLines;

+ (CGSize)contentSizeWithAttribureStr:(NSAttributedString *)attribureStr
                       constraintSize:(CGSize)constraintSize
                        numberOfLines:(NSInteger)numberOfLines;

/*
 @param attribureStr The string with attributes.
 @param constraintWidth The restrict width.
 @param numberOfLines numberOfLines.
 @param detectType The detect type
 @param emojiDic The emoji dictionary which key is string and value is iamge name.
 @param emojiSize emoji size. If it is CGSizeZero ,use the emoji image size

 return The content size.
 */
+ (CGSize)contentHeightWithAttribureStr:(NSAttributedString *)attribureStr
                         constraintWidth:(CGFloat)constraintWidth
                           numberOfLines:(NSInteger)numberOfLines
                              detectType:(YPAutoDetectCheckType)detectType
                                emojiDic:(NSDictionary *)emojiDic
                               emojiSize:(CGSize)emojiSize;

+ (CGSize)contentSizeWithAttribureStr:(NSAttributedString *)attribureStr
                        constraintSize:(CGSize)constraintSize
                         numberOfLines:(NSInteger)numberOfLines
                            detectType:(YPAutoDetectCheckType)detectType
                              emojiDic:(NSDictionary *)emojiDic
                             emojiSize:(CGSize)emojiSize;
  • 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;
/**
注意:
只设置 段落 的一个属性时可以直接用点语法;
若要同时设置段落的多个属性时,需要通过NSParagraphStyle 对象进行设置:
NSParagraphStyle *style = [[NSParagraphStyle alloc] init];
style.lineSpacing = 4;
style.alignment = NSTextAlignmentCenter;
test.paragraphStyle = style;
*/
  • 选择

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

  • 总结

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

Licenses

All source code is licensed under the MIT License.

Latest podspec

{
    "name": "YPSeniorLabel",
    "version": "0.1.9",
    "summary": "Senior view.",
    "description": "Senior view with CoreText.",
    "homepage": "https://github.com/LYPDoit/YPSeniorLabel",
    "license": "MIT",
    "authors": {
        "liuyaping": "https://github.com/LYPDoit"
    },
    "platforms": {
        "ios": "7.0"
    },
    "source": {
        "git": "https://github.com/LYPDoit/YPSeniorLabel.git",
        "tag": "0.1.9"
    },
    "source_files": "YPSeniorLabel/YPSeniorLabel/**/*.{h,m}",
    "public_header_files": "YPSeniorLabel/YPSeniorLabel/**/*.h",
    "frameworks": [
        "Foundation",
        "CoreText",
        "UIKit"
    ]
}

Pin It on Pinterest

Share This