lele8446 / CJLabel

A drop-in replacement for UILabel that supports NSAttributedString, rich text, display any view, links, select copy and more
MIT License
185 stars 27 forks source link
attributedtext copy-paste coretext label nsattributedstring select uilabel

CJLabel

CJLabel继承自UILabel,在支持UILabel所有属性的基础上,还提供富文本、图文混排、任意view插入展示、自定义点击链点设置、长按(双击)唤起UIMenuController选择复制文本等功能。

特性简介

  1. 禁止使用-init初始化!!
  2. enableCopy 长按或双击可唤起UIMenuController进行选择、全选、复制文本操作
  3. attributedTexttext 均可设置富文本
  4. 不支持NSAttachmentAttributeNameNSTextAttachment!!
    显示图片请调用:
    + initWithView:viewSize:lineAlignment:configure:或者
    + insertViewAtAttrString:view:viewSize:atIndex:lineAlignment:configure:方法初始化NSAttributedString后显示
  5. extendsLinkTouchArea设置是否扩大链点点击识别范围
  6. shadowRadius设置文本阴影模糊半径
  7. textInsets 设置文本内边距
  8. verticalAlignment 设置垂直方向的文本对齐方式。注意与显示图片时候的imagelineAlignment作区分,self.verticalAlignment对应的是整体文本在垂直方向的对齐方式,而imagelineAlignment只对图片所在行的垂直对齐方式有效
  9. delegate 点击链点代理
  10. attributedTruncationToken自定义截断字符,默认"...",只针对self.lineBreakMode的以下三种值有效,假如attributedTruncationToken=***,则:
    NSLineBreakByTruncatingHead, // 头部截断: "***wxyz"
    NSLineBreakByTruncatingTail, // 中间截断: "abcd***"
    NSLineBreakByTruncatingMiddle // 尾部截断: "ab***yz"
  11. kCJBackgroundFillColorAttributeName 背景填充颜色,属性优先级低于NSBackgroundColorAttributeName如果设置NSBackgroundColorAttributeName会忽略kCJBackgroundFillColorAttributeName的设置
  12. kCJBackgroundStrokeColorAttributeName 背景边框线颜色
  13. kCJBackgroundLineWidthAttributeName 背景边框线宽度
  14. kCJBackgroundLineCornerRadiusAttributeName 背景边框线圆角弧度
  15. kCJActiveBackgroundFillColorAttributeName 点击时候的背景填充颜色属性优先级同 kCJBackgroundFillColorAttributeName
  16. kCJActiveBackgroundStrokeColorAttributeName 点击时候的背景边框线颜色
  17. 支持添加自定义样式、可点击(长按)的文本点击链点
  18. 支持 Interface Builder
CJLabel 已知 Bug

numberOfLines大于0且小于实际label.numberOfLines,同时verticalAlignment不等于CJContentVerticalAlignmentTop时,文本显示位置有偏差。如下图所示:

CJLabel引用

1. 直接导入

下载demo,将CJLabel文件夹导入项目,引用头文件 #import "CJLabel.h"

2. CocoaPods安装
pod 'CJLabel'

用法

CGSize size = [CJLabel sizeWithAttributedString:str withConstraints:CGSizeMake(320, CGFLOAT_MAX) limitedToNumberOfLines:0];
//初始化配置
CJLabelConfigure *configure = [CJLabel configureAttributes:nil isLink:NO activeLinkAttributes:nil parameter:nil clickLinkBlock:nil longPressBlock:nil];
//设置配置属性
configure.attributes = @{NSFontAttributeName:[UIFont boldSystemFontOfSize:18]};
//设置指定字符属性
attStr = [CJLabel configureAttrString:attStr withString:@"不同字体" sameStringEnable:NO configure:configure];
NSRange imgRange = [attStr.string rangeOfString:@"插入图片"];
//移除指定属性
[configure removeAttributesForKey:kCJBackgroundStrokeColorAttributeName];
//指定位置插入图片
UIImageView *imgView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"CJLabel.png"]];
imgView.contentMode = UIViewContentModeScaleAspectFill;
imgView.clipsToBounds = YES;
attStr = [CJLabel insertViewAtAttrString:attStr view:imgView viewSize:CGSizeMake(55, 45) atIndex:(imgRange.location+imgRange.length) lineAlignment:CJVerticalAlignmentBottom configure:configure];
//设置内边距
self.label.textInsets = UIEdgeInsetsMake(10, 10, 10, 0);
self.label.attributedText = attStr;
//设置垂直对齐方式
self.label.verticalAlignment = CJVerticalAlignmentCenter;
self.label.text = self.attStr;
//支持选择复制
self.label.enableCopy = YES;
//设置点击链点属性
configure.attributes = @{NSForegroundColorAttributeName:[UIColor blueColor]};
//设置点击高亮属性
configure.activeLinkAttributes = @{NSForegroundColorAttributeName:[UIColor redColor]};
//链点自定义参数
configure.parameter = @"参数为字符串";
//点击回调(也可通过设置self.label.delegate = self代理,返回点击回调事件)
configure.clickLinkBlock = ^(CJLabelLinkModel *linkModel) {
   //do something
};
//长按回调
configure.longPressBlock = ^(CJLabelLinkModel *linkModel) {
   //do something
};
//设置为可点击链点
configure.isLink = YES;
//设置点击链点
attStr = [CJLabel configureAttrString:attStr
                           withString:@"CJLabel"
                     sameStringEnable:YES
                            configure:configure];
//设置图片点击链点属性
NSRange imageRange = [attStr.string rangeOfString:@"图片"];
CJLabelConfigure *imgConfigure =
[CJLabel configureAttributes:@{kCJBackgroundStrokeColorAttributeName:[UIColor redColor]}
                      isLink:YES
         activeLinkAttributes:@{kCJActiveBackgroundStrokeColorAttributeName:[UIColor lightGrayColor]}
                    parameter:@"图片参数"
               clickLinkBlock:^(CJLabelLinkModel *linkModel){
                   [self clickLink:linkModel isImage:YES];
               }
               longPressBlock:^(CJLabelLinkModel *linkModel){
                   [self clicklongPressLink:linkModel isImage:YES];
               }];
attStr = [CJLabel insertViewAtAttrString:attStr view:@"CJLabel.png" viewSize:CGSizeMake(45, 35) atIndex:(imageRange.location+imageRange.length) lineAlignment:verticalAlignment configure:imgConfigure];
self.label.attributedText = attStr;
//支持选择复制
self.label.enableCopy = YES;
//配置链点属性
configure.isLink = YES;
configure.clickLinkBlock = ^(CJLabelLinkModel *linkModel) {
    //点击 `……全文`
    [self clickTruncationToken:linkModel];
};
configure.attributes = @{NSForegroundColorAttributeName:[UIColor blueColor],NSFontAttributeName:[UIFont systemFontOfSize:13]};
//自定义截断字符为:"……全文"
NSAttributedString *truncationToken = [CJLabel initWithAttributedString:[[NSAttributedString alloc]initWithString:@"……全文"] strIdentifier:@"TruncationToken" configure:configure];
//设置行尾截断
self.label.lineBreakMode = NSLineBreakByTruncatingTail;
self.label.attributedTruncationToken = truncationToken;
//设置点击链点
attStr = [CJLabel configureAttrString:attStr withAttributedString:truncationToken strIdentifier:@"TruncationToken" sameStringEnable:NO configure:configure];            
self.label.attributedText = attStr;
//支持选择复制
self.label.enableCopy = YES;

版本说明

许可证

CJLabel 使用 MIT 许可证,详情见 LICENSE 文件。

更多

深入理解 iOS 图文混排原理并自定义图文控件 CJLabel富文本三 —— UILabel支持选择复制以及实现原理