{"id":20635951,"url":"https://github.com/lele8446/cjlabel","last_synced_at":"2025-08-20T22:32:30.187Z","repository":{"id":56904716,"uuid":"48437222","full_name":"lele8446/CJLabel","owner":"lele8446","description":"A drop-in replacement for UILabel that supports NSAttributedString, rich text, display any view, links, select copy and more","archived":false,"fork":false,"pushed_at":"2022-09-13T07:45:19.000Z","size":7432,"stargazers_count":185,"open_issues_count":22,"forks_count":27,"subscribers_count":6,"default_branch":"master","last_synced_at":"2024-12-11T23:41:44.556Z","etag":null,"topics":["attributedtext","copy-paste","coretext","label","nsattributedstring","select","uilabel"],"latest_commit_sha":null,"homepage":"","language":"Objective-C","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/lele8446.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2015-12-22T14:44:54.000Z","updated_at":"2024-11-14T01:56:15.000Z","dependencies_parsed_at":"2022-08-21T02:50:26.066Z","dependency_job_id":null,"html_url":"https://github.com/lele8446/CJLabel","commit_stats":null,"previous_names":["lele8446/cjlabeltest"],"tags_count":19,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/lele8446%2FCJLabel","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/lele8446%2FCJLabel/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/lele8446%2FCJLabel/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/lele8446%2FCJLabel/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/lele8446","download_url":"https://codeload.github.com/lele8446/CJLabel/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":230462911,"owners_count":18229865,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["attributedtext","copy-paste","coretext","label","nsattributedstring","select","uilabel"],"created_at":"2024-11-16T15:08:18.578Z","updated_at":"2024-12-19T16:12:39.784Z","avatar_url":"https://github.com/lele8446.png","language":"Objective-C","readme":"# CJLabel\n\n`CJLabel`继承自`UILabel`，在支持`UILabel`所有属性的基础上，还提供富文本、图文混排、任意view插入展示、自定义点击链点设置、长按（双击）唤起`UIMenuController`选择复制文本等功能。\n\n## 特性简介\n   1. 禁止使用`-init`初始化！！\n   2. `enableCopy` 长按或双击可唤起`UIMenuController`进行选择、全选、复制文本操作   \n   3. `attributedText` 与 `text` 均可设置富文本\n   4. 不支持`NSAttachmentAttributeName`，`NSTextAttachment`！！\u003cbr/\u003e显示图片请调用:\u003cbr/\u003e\n      `+ initWithView:viewSize:lineAlignment:configure:`或者\u003cbr/\u003e\n      `+ insertViewAtAttrString:view:viewSize:atIndex:lineAlignment:configure:`方法初始化`NSAttributedString`后显示\n   5. `extendsLinkTouchArea`设置是否扩大链点点击识别范围 \n   6. `shadowRadius`设置文本阴影模糊半径 \n   7. `textInsets` 设置文本内边距\n   8. `verticalAlignment` 设置垂直方向的文本对齐方式。注意与显示图片时候的`imagelineAlignment`作区分，`self.verticalAlignment`对应的是整体文本在垂直方向的对齐方式，而`imagelineAlignment`只对图片所在行的垂直对齐方式有效\n   9. `delegate` 点击链点代理\n   10. `attributedTruncationToken`自定义截断字符，默认\"...\",只针对`self.lineBreakMode`的以下三种值有效，假如`attributedTruncationToken`=`***`，则： \u003cbr/\u003e\n `NSLineBreakByTruncatingHead,    // 头部截断: \"***wxyz\"`\u003cbr/\u003e\n `NSLineBreakByTruncatingTail,    // 中间截断: \"abcd***\"`\u003cbr/\u003e\n `NSLineBreakByTruncatingMiddle   // 尾部截断:  \"ab***yz\"`\n   11. `kCJBackgroundFillColorAttributeName` 背景填充颜色，属性优先级低于`NSBackgroundColorAttributeName`如果设置`NSBackgroundColorAttributeName`会忽略`kCJBackgroundFillColorAttributeName`的设置\n   12. `kCJBackgroundStrokeColorAttributeName ` 背景边框线颜色\n   13. `kCJBackgroundLineWidthAttributeName ` 背景边框线宽度\n   14. `kCJBackgroundLineCornerRadiusAttributeName ` 背景边框线圆角弧度\n   15. `kCJActiveBackgroundFillColorAttributeName ` 点击时候的背景填充颜色属性优先级同\n`kCJBackgroundFillColorAttributeName`\n   16. `kCJActiveBackgroundStrokeColorAttributeName ` 点击时候的背景边框线颜色\n   17. 支持添加自定义样式、可点击（长按）的文本点击链点\n   18. 支持 Interface Builder\n\n\n##### CJLabel 已知 Bug\n\n   `numberOfLines`大于0且小于实际`label.numberOfLines`，同时`verticalAlignment`不等于`CJContentVerticalAlignmentTop`时，文本显示位置有偏差。如下图所示:\u003cbr/\u003e\n   \u003ccenter\u003e\n \u003cimg src=\"http://oz3eqyeso.bkt.clouddn.com/CJLabelBug.jpg\" width=\"50%\"/\u003e\n \u003c/center\u003e\n\n## CJLabel引用\n##### 1. 直接导入\n下载demo，将CJLabel文件夹导入项目，引用头文件 `#import \"CJLabel.h\"`\n##### 2. CocoaPods安装\n```ruby\npod 'CJLabel'\n\n```\n\n## 用法\n* 根据NSAttributedString计算CJLabel的size大小\n\n```objective-c\nCGSize size = [CJLabel sizeWithAttributedString:str withConstraints:CGSizeMake(320, CGFLOAT_MAX) limitedToNumberOfLines:0];\n```\n* 指定内边距以及限定行数计算CJLabel的size大小\n```objective-c\nCGSize size = [CJLabel sizeWithAttributedString:str withConstraints:CGSizeMake(320, CGFLOAT_MAX) limitedToNumberOfLines:0 textInsets:3];\n```\n\n* 设置富文本展示\n\u003ccenter\u003e\n \u003cimg src=\"http://oz3eqyeso.bkt.clouddn.com/example0_1.png\" width=\"50%\"/\u003e\n \u003c/center\u003e\n\n```objective-c\n//初始化配置\nCJLabelConfigure *configure = [CJLabel configureAttributes:nil isLink:NO activeLinkAttributes:nil parameter:nil clickLinkBlock:nil longPressBlock:nil];\n//设置配置属性\nconfigure.attributes = @{NSFontAttributeName:[UIFont boldSystemFontOfSize:18]};\n//设置指定字符属性\nattStr = [CJLabel configureAttrString:attStr withString:@\"不同字体\" sameStringEnable:NO configure:configure];\nNSRange imgRange = [attStr.string rangeOfString:@\"插入图片\"];\n//移除指定属性\n[configure removeAttributesForKey:kCJBackgroundStrokeColorAttributeName];\n//指定位置插入图片\nUIImageView *imgView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@\"CJLabel.png\"]];\nimgView.contentMode = UIViewContentModeScaleAspectFill;\nimgView.clipsToBounds = YES;\nattStr = [CJLabel insertViewAtAttrString:attStr view:imgView viewSize:CGSizeMake(55, 45) atIndex:(imgRange.location+imgRange.length) lineAlignment:CJVerticalAlignmentBottom configure:configure];\n//设置内边距\nself.label.textInsets = UIEdgeInsetsMake(10, 10, 10, 0);\nself.label.attributedText = attStr;\n```\n* 垂直对齐、选择复制\n \u003ccenter\u003e\n \u003cimg src=\"http://oz3eqyeso.bkt.clouddn.com/example1.gif\" width=\"35%\"/\u003e\n \u003c/center\u003e\n\n```objective-c\n//设置垂直对齐方式\nself.label.verticalAlignment = CJVerticalAlignmentCenter;\nself.label.text = self.attStr;\n//支持选择复制\nself.label.enableCopy = YES;\n```\n* 设置文字、图片点击链点\n \u003ccenter\u003e\n \u003cimg src=\"http://oz3eqyeso.bkt.clouddn.com/example4.gif\" width=\"25%\"/\u003e\n \u003c/center\u003e\n\n```objective-c\n//设置点击链点属性\nconfigure.attributes = @{NSForegroundColorAttributeName:[UIColor blueColor]};\n//设置点击高亮属性\nconfigure.activeLinkAttributes = @{NSForegroundColorAttributeName:[UIColor redColor]};\n//链点自定义参数\nconfigure.parameter = @\"参数为字符串\";\n//点击回调（也可通过设置self.label.delegate = self代理，返回点击回调事件）\nconfigure.clickLinkBlock = ^(CJLabelLinkModel *linkModel) {\n   //do something\n};\n//长按回调\nconfigure.longPressBlock = ^(CJLabelLinkModel *linkModel) {\n   //do something\n};\n//设置为可点击链点\nconfigure.isLink = YES;\n//设置点击链点\nattStr = [CJLabel configureAttrString:attStr\n                           withString:@\"CJLabel\"\n                     sameStringEnable:YES\n                            configure:configure];\n//设置图片点击链点属性\nNSRange imageRange = [attStr.string rangeOfString:@\"图片\"];\nCJLabelConfigure *imgConfigure =\n[CJLabel configureAttributes:@{kCJBackgroundStrokeColorAttributeName:[UIColor redColor]}\n                      isLink:YES\n         activeLinkAttributes:@{kCJActiveBackgroundStrokeColorAttributeName:[UIColor lightGrayColor]}\n                    parameter:@\"图片参数\"\n               clickLinkBlock:^(CJLabelLinkModel *linkModel){\n                   [self clickLink:linkModel isImage:YES];\n               }\n               longPressBlock:^(CJLabelLinkModel *linkModel){\n                   [self clicklongPressLink:linkModel isImage:YES];\n               }];\nattStr = [CJLabel insertViewAtAttrString:attStr view:@\"CJLabel.png\" viewSize:CGSizeMake(45, 35) atIndex:(imageRange.location+imageRange.length) lineAlignment:verticalAlignment configure:imgConfigure];\nself.label.attributedText = attStr;\n//支持选择复制\nself.label.enableCopy = YES;\n```\n\n* 自定义截断文本，并设置为可点击\n \u003ccenter\u003e\n \u003cimg src=\"http://oz3eqyeso.bkt.clouddn.com/example5.gif\" width=\"28%\"/\u003e\n \u003c/center\u003e\n\n```objective-c\n//配置链点属性\nconfigure.isLink = YES;\nconfigure.clickLinkBlock = ^(CJLabelLinkModel *linkModel) {\n    //点击 `……全文`\n    [self clickTruncationToken:linkModel];\n};\nconfigure.attributes = @{NSForegroundColorAttributeName:[UIColor blueColor],NSFontAttributeName:[UIFont systemFontOfSize:13]};\n//自定义截断字符为：\"……全文\"\nNSAttributedString *truncationToken = [CJLabel initWithAttributedString:[[NSAttributedString alloc]initWithString:@\"……全文\"] strIdentifier:@\"TruncationToken\" configure:configure];\n//设置行尾截断\nself.label.lineBreakMode = NSLineBreakByTruncatingTail;\nself.label.attributedTruncationToken = truncationToken;\n//设置点击链点\nattStr = [CJLabel configureAttrString:attStr withAttributedString:truncationToken strIdentifier:@\"TruncationToken\" sameStringEnable:NO configure:configure];            \nself.label.attributedText = attStr;\n//支持选择复制\nself.label.enableCopy = YES;\n```\n\n## 版本说明\n* ***V4.7.0***\u003cbr/\u003e\n   新增不可换行标签功能，优化图文混排展示\n\n* ***V4.6.0***\u003cbr/\u003e\n   支持显示任意view\n\n* ***V4.5.0 V4.5.1 V4.5.3***\u003cbr/\u003e\n   增加`attributedTruncationToken`属性，支持自定义截断字符；增加`kCJStrikethroughStyleAttributeName、kCJStrikethroughColorAttributeName`属性，可对指定文本添加删除线\n\n* ***V4.4.0***\u003cbr/\u003e\n   优化NSAttributedString链点属性设置\n\n* ***V4.0.0***\u003cbr/\u003e\n   新增`enableCopy`属性，支持选择、全选、复制功能，类似`UITextView`的选择复制效果。\n\n* ***V3.0.0***\u003cbr/\u003e\n   优化富文本配置方法，新增CJLabelConfigure类，简化方法调用，增加对NSAttributedString点击链点的判断（比如对于两个重名用户：@lele 和 @lele，可以分别设置不同的点击响应事件）\u003cbr/\u003e\n   ***注意***\n   ***`V3.0.0`*** 版本引入`CJLabelConfigure`类，优化了NSAttributedString的设置，旧的配置API不再支持。相关调用请参照以下相关方法\u003cbr/\u003e\n   `+ initWithImage:imageSize:imagelineAlignment:configure:`\u003cbr/\u003e\n   `+ initWithString:configure:`\u003cbr/\u003e\n   `+ initWithAttributedString:strIdentifier:configure:`\u003cbr/\u003e\n\n* ***V2.1.2***\u003cbr/\u003e\n   可修改图片所在行在垂直方向的对齐方式（只针对当前行），有居上、居中、居下选项，默认居下\n\n* ***V2.1.1***\u003cbr/\u003e\n   修复单行文字时候点击链点的判断，增加delegate\n\n* ***V2.0.0***\u003cbr/\u003e\n   优化点击链点响应判断，增加插入图片、插入图片链点、点击链点背景色填充、点击链点边框线描边等功能\n    v2.0.0之后版本与v1.x.x版本差别较大，基本上重写了增加以及移除点击链点的API\n\n* ***V1.0.2***\u003cbr/\u003e\n   点击链点增加扩展属性parameter\n\n* ***V1.0.1***\u003cbr/\u003e\n   增加文本中内容相同的链点能够响应点击属性sameLinkEnable，必须在设置self.attributedText前赋值，默认值为NO，只取文本中首次出现的链点。\n\n* ***V1.0.0***\u003cbr/\u003e\n  支持链点点击响应\n\n  \n## 许可证\nCJLabel 使用 MIT 许可证，详情见 LICENSE 文件。\n\n## 更多\n[深入理解 iOS 图文混排原理并自定义图文控件](https://www.infoq.cn/article/cy916KUJYK7GA3p2VjZH)\n[CJLabel富文本三 —— UILabel支持选择复制以及实现原理](https://www.jianshu.com/p/7de3e6d19e31)","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Flele8446%2Fcjlabel","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Flele8446%2Fcjlabel","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Flele8446%2Fcjlabel/lists"}