Latest 4.6.1
Homepage https://github.com/lele8446/CJLabelTest
License Apache License, Version 2.0 Licensed under the Apache License, Version 2.0 (the “License”);
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an “AS IS” BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

Platforms ios 7.0
Authors

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

CJLabel引用

1. 直接导入

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

2. CocoaPods安装
pod 'CJLabel'

用法

  • 根据NSAttributedString计算CJLabel的size大小
     

    CGSize size = [CJLabel sizeWithAttributedString:str withConstraints:CGSizeMake(320, CGFLOAT_MAX) limitedToNumberOfLines:0];
  • 指定内边距以及限定行数计算CJLabel的size大小

    CGSize size = [CJLabel sizeWithAttributedString:str withConstraints:CGSizeMake(320, CGFLOAT_MAX) limitedToNumberOfLines:0 textInsets:3];
  • 设置富文本展示

    CJLabel
//初始化配置
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;
  • 垂直对齐、选择复制

    CJLabel
//设置垂直对齐方式
self.label.verticalAlignment = CJVerticalAlignmentCenter;
self.label.text = self.attStr;
//支持选择复制
self.label.enableCopy = YES;
  • 设置文字、图片点击链点

    CJLabel
//设置点击链点属性
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;
  • 自定义截断文本,并设置为可点击

    CJLabel
//配置链点属性
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;

版本说明

  • V4.6.0
    支持显示任意view
  • V4.5.0 V4.5.1 V4.5.3
    增加attributedTruncationToken属性,支持自定义截断字符;增加kCJStrikethroughStyleAttributeName、kCJStrikethroughColorAttributeName属性,可对指定文本添加删除线
  • V4.4.0
    优化NSAttributedString链点属性设置
  • V4.0.0
    新增enableCopy属性,支持选择、全选、复制功能,类似UITextView的选择复制效果。
  • V3.0.0
    优化富文本配置方法,新增CJLabelConfigure类,简化方法调用,增加对NSAttributedString点击链点的判断(比如对于两个重名用户:@lele 和 @lele,可以分别设置不同的点击响应事件)
    注意
    V3.0.0 版本引入CJLabelConfigure类,优化了NSAttributedString的设置,旧的配置API不再支持。相关调用请参照以下相关方法
    + initWithImage:imageSize:imagelineAlignment:configure:
    + initWithString:configure:
    + initWithAttributedString:strIdentifier:configure:
  • V2.1.2
    可修改图片所在行在垂直方向的对齐方式(只针对当前行),有居上、居中、居下选项,默认居下
  • V2.1.1
    修复单行文字时候点击链点的判断,增加delegate
  • V2.0.0
    优化点击链点响应判断,增加插入图片、插入图片链点、点击链点背景色填充、点击链点边框线描边等功能
    v2.0.0之后版本与v1.x.x版本差别较大,基本上重写了增加以及移除点击链点的API
  • V1.0.2
    点击链点增加扩展属性parameter
  • V1.0.1
    增加文本中内容相同的链点能够响应点击属性sameLinkEnable,必须在设置self.attributedText前赋值,默认值为NO,只取文本中首次出现的链点。
  • V1.0.0
    支持链点点击响应

许可证

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

更多

CJLabel图文混排二 —— UILabel插入图片以及精确链点点击
CJLabel图文混排一 ——UILabel富文本显示以及任意字符的点击响应
CJLabel富文本三 —— UILabel支持选择复制以及实现原理

Latest podspec

{
    "name": "CJLabel",
    "version": "4.6.1",
    "summary": "A drop-in replacement for UILabel that supports NSAttributedString, rich text, links, select copy and more.",
    "homepage": "https://github.com/lele8446/CJLabelTest",
    "license": {
        "type": "Apache License, Version 2.0",
        "text": "    Licensed under the Apache License, Version 2.0 (the "License");n    you may not use this file except in compliance with the License.n    You may obtain a copy of the License atnn    http://www.apache.org/licenses/LICENSE-2.0nn    Unless required by applicable law or agreed to in writing, softwaren    distributed under the License is distributed on an "AS IS" BASIS,n    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.n    See the License for the specific language governing permissions andn    limitations under the License.n"
    },
    "authors": {
        "ChiJinLian": "[email protected]"
    },
    "platforms": {
        "ios": "7.0"
    },
    "source": {
        "git": "https://github.com/lele8446/CJLabelTest.git",
        "tag": "4.6.1"
    },
    "source_files": "CJLabel/*"
}

Pin It on Pinterest

Share This