Code Monkey home page Code Monkey logo

tyattributedlabel's Introduction

TYAttributedLabel v2.6

TYAttributedLabel 简单,强大的属性文本的控件(无需了解CoreText),支持图文混排显示,支持添加链接,image和UIView控件,支持自定义排版显示

新的异步渲染TYText:text asynchronous rendering by TextKit

更新:
详细的使用请看LovePlayNews项目
使用autolayout 如果需要自动推断高度,必须设置preferredMaxLayoutWidth
添加到CocoaPods
微博demo(建议真机调试) 下载地址:链接: http://pan.baidu.com/s/1sjnBWRj 密码: t7qn
v2.6 新增宽度自适应isWidthToFit,空心字设置strokeWidth,段落间距paragraphSpacing
v2.5 把label的一些操作移动textContainer,label只负责draw了。
v2.4 修复imge放大bug,新增imageAlignment 和 autolayout支持,以及相应的demo,感谢xinzhengzhang,nonstriater
v2.3 新增 做题demo,代码优化
v2.2 新增 TYImagecache类,新增 image URL 下载缓存,功能优化,改进
v2.1 添加 tableViewCell demo
v2.0 重构优化代码,性能提升,稳定(已在项目中使用), 分离出TYTextContainer ,可以提前生成,也可以生成attributedString,显著提升cell滑动场景流畅度,可以和微博一样流畅
v1.2 添加设置行数,修复bug,增强稳定性
v1.1 添加链接高亮效果,链接便利方法,长按手势代理,优化代码

CocoaPods

pod 'TYAttributedLabel', '~> 2.6.2'

ScreenShot

image

新-做题demo

image

weibo demo 使用TYAttributedLabel 截图

image

Requirements

  • Xcode 5 or higher
  • Apple LLVM compiler
  • iOS 6.0 or higher
  • ARC

Features

  • 支持属性文本,图文混排显示,支持行间距 行间距 段落间距,设置行数,自适应高度,宽度
  • 支持添加自定义文本属性
  • 支持添加属性文本,自定义链接,新增高亮效果显示(文字和背景)
  • 支持添加UIImage和UIView控件

Demo

运行demo可以查看效果,而且在demo中,针对各种文本和图文的实现都有详细的用例,每个头文件中都有详细的用法注释,这里简单的介绍下用法

Usage

API Quickstart

  • Category And Protocol
Class Function
NSMutableAttributedString (TY) category提供便利color,font CharacterSpacing,UnderlineStyle,ParagraphStyle的属性添加,无需了解复杂的CoreText
TYTextStorageProtocol 自定义文本属性 遵守最基本的协议 即可 addTextStorage 添加进去
TYAppendTextStorageProtocol 自定义文本属性协议 遵守即可appendTextStorage 添加进去
TYLinkStorageProtocol 自定义文本链接属性 继承TYAppendTextStorageProtocol
TYDrawStorageProtocol 自定义显示内容协议 如 UIImage UIView

下层协议继承上层的协议,如果觉得复杂,其实我已经实现了常用的自定义属性,拿来就可以用,或者继承,添加你想要的

  • Label And Storage
Class Function
TYAttributedLabel 简单易用的属性文本,富文本的显示控件,
addTextStorage在已经设置文本的基础上添加属性,image或者view,
appendTextStorage(无需事先设置文本)直接添加属性,image或者view到最后
TYTextContainer 文本容器,可以提前生成,也可以生成attributedString,显著提升cell滚动流畅度
TYTextStorage 自定义文本属性,支持textColor,font,underLineStyle
TYLinkTextStorage 自定义链接属性,继承TYTextStorage,支持点击代理
TYDrawStorage 自定义显示内容属性,如UIImage,UIView,支持点击代理
TYImageStorage 自定义图片显示,继承TYDrawStorage
TYViewStorage 自定义UIView控件,继承TYDrawStorage
TYImageCache image缓存类,支持URL请求

如果需要更加详细的内容,请看各个头文件,有详细的注释

Delegate

// 点击代理
- (void)attributedLabel:(TYAttributedLabel *)attributedLabel textStorageClicked:(id<TYTextStorageProtocol>)textStorage atPoint:(CGPoint)point;

// 长按代理 有多个状态 begin, changes, end 都会调用,所以需要判断状态
- (void)attributedLabel:(TYAttributedLabel *)attributedLabel textStorageLongPressed:(id<TYTextStorageProtocol>)textStorage onState:(UIGestureRecognizerState)state atPoint:(CGPoint)point;

Examples

  • appendStorage demo
TYAttributedLabel *label = [[TYAttributedLabel alloc]init];
[self.view addSubview:label];

// 文字间隙
label.characterSpacing = 2;
// 文本行间隙
label.linesSpacing = 6;

NSString *text = @"\t总有一天你将破蛹而出,成长得比人们期待的还要美丽。\n";
[label appendText:text];

NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc]initWithString:text];
[attributedString addAttributeTextColor:[UIColor blueColor]];
[attributedString addAttributeFont:[UIFont systemFontOfSize:15]];
[label appendTextAttributedString:attributedString];

[label appendImageWithName:@"CYLoLi" size:CGSizeMake(CGRectGetWidth(label.frame), 180)];

UIImageView *imageView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"CYLoLi"]];
imageView.frame = CGRectMake(0, 0, CGRectGetWidth(label.frame), 180);
[label appendView:imageView];

[label setFrameWithOrign:CGPointMake(0,0Width:CGRectGetWidth(self.view.frame)];
  • addStorage demo
TYAttributedLabel *label = [[TYAttributedLabel alloc]initWithFrame:CGRectMake(0, 0, CGRectGetWidth(self.view.frame), 0)];
[self.view addSubview:label];

NSString *text = @"\t总有一天你将破蛹而出,成长得比人们期待的还要美丽。\n";
[label setText:text];

// 文字间隙
label.characterSpacing = 2;
// 文本行间隙
label.linesSpacing = 6;

textStorage = [[TYTextStorage alloc]init];
textStorage.range = [text rangeOfString:@"总有一天你将破蛹而出"]; 
textStorage.textColor = RGB(0, 155, 0, 1);
textStorage.font = [UIFont systemFontOfSize:18];
[label addTextStorage:textStorage];

[label addLinkWithLinkData:@"www.baidu.com" range:NSMakeRange(5, 8)];

[label addImageWithName:@"haha" range:NSMakeRange(2, 1)];

UIImageView *imageView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"CYLoLi"]];
imageView.frame = CGRectMake(0, 0, CGRectGetWidth(label.frame), 180);
[label addView:imageView range:NSMakeRange(16, 1)];

[label sizeToFit];
  • TextContainer demo
NSString *text = @"\t总有一天你将破蛹而出,成长得比人们期待的还要美丽。\n";
TYTextContainer *textContainer = [[TYTextContainer alloc]init];
    textContainer.text = text;
    // 文字间隙
textContainer.characterSpacing = 2;
// 文本行间隙
textContainer.linesSpacing = 5;

textStorage = [[TYTextStorage alloc]init];
textStorage.range = [text rangeOfString:@"总有一天你将破蛹而出"]; 
textStorage.textColor = RGB(0, 155, 0, 1);
textStorage.font = [UIFont systemFontOfSize:18];
[textContainer addTextStorage:textStorage];

[textContainer addLinkWithLinkData:@"www.baidu.com" range:NSMakeRange(5, 8)];

[textContainer addImageWithName:@"haha" range:NSMakeRange(2, 1)];

UIImageView *imageView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"CYLoLi"]];
imageView.frame = CGRectMake(0, 0, CGRectGetWidth(label.frame), 180);
[textContainer addView:imageView range:NSMakeRange(16, 1)];


// 生成 textContainer 文本容器
[textContainer createTextContainerWithTextWidth:CGRectGetWidth(self.view.frame)];

TYAttributedLabel *label = [[TYAttributedLabel alloc]init];
label.textContainer = textContainer;


// 也可以 生成NSAttributedString 属性文本
//NSAttributedString *attString = [textContainer createAttributedString];
//label.attributedText = attString;

[label setFrameWithOrign:CGPointZero Width:CGRectGetWidth(self.view.frame)];
[self.view addSubView:label];

Contact

如果你发现bug,please pull reqeust me
如果你有更好的改进,please pull reqeust me

tyattributedlabel's People

Contributors

12207480 avatar lianchengjiang avatar smelloftime avatar triplecc avatar xinzhengzhang avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

tyattributedlabel's Issues

小建议

设置文本颜色大小时 并没有给出range的入口 我看了你的源码 range都是[0,self.lengh]
出现一行文字大小不一样或者颜色不一样的时候我还要自己去更改
能不能直接添加进来

label1.lineBreakMode = kCTLineBreakByTruncatingTail导致多行设置失效.

TYAttributedLabel *label1 = [[TYAttributedLabel alloc]initWithFrame:CGRectMake(0, 64, 320, 110)];

TYImageStorage *imageUrlStorage = [[TYImageStorage alloc]init];
imageUrlStorage.imageURL = [NSURL URLWithString:@"https://ss0.bdstatic.com/5aV1bjqh_Q23odCf/static/superman/img/logo/bd_logo1_31bdc765.png"];
imageUrlStorage.size = CGSizeMake(15,15);
imageUrlStorage.placeholdImageName = @"loading";
[label1 appendTextStorage:imageUrlStorage];
[label1 appendTextAttributedString:[[NSMutableAttributedString alloc]initWithString:@" "]];
NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc]initWithString:@"sdflsdjgljsdlgjlsjglsdjlkfgjsdjglsdjgkjsgjsjglsjdgkgkjsgjgjlgjlgjdsfsdfdsgdsgjkdlsjgklsjdgkjsdljglsdjgkjdfslgjfkdgjljglksjdlgjs"];
[label1 appendTextAttributedString:attributedString];

// 文字间隙
label1.characterSpacing = 2;
// 文本行间隙
label1.linesSpacing = 2;

label1.lineBreakMode = kCTLineBreakByTruncatingTail;
label1.numberOfLines = 2;
// 文本字体
label1.font = [UIFont systemFontOfSize:17];

[self.view addSubview:label1];

label的多行设置失效.总是显示一行

NSLineBreakByTruncatingTail

  • (void)setLineBreakMode:(CTLineBreakMode)lineBreakMode
    {
    if (_lineBreakMode != lineBreakMode) {
    _lineBreakMode = lineBreakMode;
    if (_lineBreakMode == kCTLineBreakByTruncatingTail)
    {
    lineBreakMode = _numberOfLines == 1 ? kCTLineBreakByCharWrapping : kCTLineBreakByWordWrapping;
    }

    [_attString addAttributeAlignmentStyle:_textAlignment lineSpaceStyle:_linesSpacing paragraphSpaceStyle:_paragraphSpacing lineBreakStyle:lineBreakMode];
    [self resetFrameRef];
    

    }
    }

为什么要这么处理 linBreakMode 呢? 如果我想要 kCTLineBreakByTruncatingTail 这种效果, 可以注释掉 这个 if 判断么?

masksToBounds 问题

图文混排时,想实现 图片 比 文字 宽一些, 因为文字两侧想留部分间隔
不知道,这样 除了 masksToBounds 还有什么较好的方式

TYImageStorage 支持设置固定大小吗?

现在给TYImageStorage设置size,并不是把大小设置成固定的,而是根据实际图片的宽高比。
现在我有设置图片固定大小的需求,要怎么实现呢?

加了图片之后就不能正确的右对齐了

// 跟帖数
    TYAttributedLabel *numLabel = [[TYAttributedLabel alloc]initWithFrame:CGRectMake(10, frameY, kScreenWidth-10-10, 20)];
    numLabel.textAlignment = kCTTextAlignmentRight;
    numLabel.backgroundColor = [UIColor redColor];
    TYImageStorage *numStorage = [[TYImageStorage alloc]init];
    numStorage.cacheImageOnMemory = YES;
    numStorage.imageName = @"post_s";
    numStorage.size = CGSizeMake(18, 18);
    numStorage.imageAlignment = TYImageAlignmentRight;
    [numLabel appendTextStorage:numStorage];
    [numLabel appendText:@" "];

    NSMutableAttributedString *numString = [[NSMutableAttributedString alloc]initWithString:_activityDataModel.num];
    [numString setFont:[UIFont fruitdayLightGaryText]];
    [numString setTextColor:[UIColor fruitdayLightGaryText]];
    [numString addAttributeCharacterSpacing:0];
    [numString addAttributeAlignmentStyle:kCTTextAlignmentRight lineSpaceStyle:0 lineBreakStyle:kCTLineBreakByTruncatingTail];
    [numLabel appendTextAttributedString:numString];
    [numLabel sizeToFit];

    [_headView addSubview:numLabel];

如过我不加上面的图片 就可以正确的右对齐,加了之后就一直显示左对齐了

Demo中简单文本显示异常

在xcode7.1,iOS9模拟器上,简单文本显示的例子中,label1只显示到"面对着汹涌而来的现实,觉..."这里,后面的文本被截断了.

行间距问题

行间距设成0以后,还是觉的大了,设成负的也不管用,想问下,还有别的方法可以设置吗?谢谢

文本不能居中 一直靠左显示

@Property (weak, nonatomic) IBOutlet TYAttributedLabel *timeButtonLabel;
[self.timeButtonLabel setAttributedText:totalString];
self.timeButtonLabel.textAlignment = NSTextAlignmentCenter;//NSTextAlignmentCenter不起作用

Demo No support in iOS6.0 complete

console print these:
1:
2015-11-05 11:29:05.152 TYAttributedLabelDemo[24130:907]
-[ViewController setEdgesForExtendedLayout:]: unrecognized selector sent to instance 0x1d599fb0
2015-11-05 11:29:05.156 TYAttributedLabelDemo[24130:907]
*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[ViewController setEdgesForExtendedLayout:]: unrecognized selector sent to instance 0x1d599fb0'
2:
2015-11-05 11:36:58.926 TYAttributedLabelDemo[24237:907] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[TExamTextField setTintColor:]: unrecognized selector sent to instance 0x1ed3a860'
3:
2015-11-05 11:38:41.877 TYAttributedLabelDemo[24308:907] -[UITableView setEstimatedRowHeight:]: unrecognized selector sent to instance 0x2038a800
2015-11-05 11:38:41.880 TYAttributedLabelDemo[24308:907] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[UITableView setEstimatedRowHeight:]: unrecognized selector sent to instance 0x2038a800'

和系统的UILabel表现不同

设置相同的间距值,显示和系统的不相同.
字体间默认间距也不相同.
字体的颜色都不同.等等.
795b2a58-fe82-49ef-820b-29991accea1a

在tablecell上面 TYAttributedLabel使用手势代理截获tableView:didSelectRowAtIndexPath:问题

简单修改了singletap方法,不知有没有更好的方法

pragma mark - Gesture action

  • (void)singleTap:(UITapGestureRecognizer *)sender
    {
    CGPoint point = [sender locationInView:self];

    // CoreText context coordinates are the opposite to UIKit so we flip the bounds
    CGAffineTransform transform = CGAffineTransformScale(CGAffineTransformMakeTranslation(0, self.bounds.size.height), 1.f, -1.f);

    __typeof (self) __weak weakSelf = self;
    __block BOOL didHit = NO;
    // 遍历run位置字典
    [_runRectDictionary enumerateKeysAndObjectsUsingBlock:^(NSValue *keyRectValue, id obj, BOOL *stop) {

    CGRect imgRect = [keyRectValue CGRectValue];
    CGRect rect = CGRectApplyAffineTransform(imgRect, transform);
    
    // point 是否在rect里
    if(CGRectContainsPoint(rect, point)){
        //NSLog(@"点击了 textStorage ");
        // 调用代理
        if (_delegateFlags.textStorageClickedAtPoint) {
            didHit = YES;
            [_delegate attributedLabel:weakSelf textStorageClicked:obj atPoint:point];
            *stop = YES;
        }
    }
    

    }];
    if (!didHit) {
    if ([_delegate respondsToSelector:@selector(attributedLabelDidNotHit)]) {
    [_delegate attributedLabelDidNotHit];
    }
    }
    }

名字截断可以支持么

比如我里面一些评论开头是名字 名字长度要限制 超过的要截断 能通过一些方法来进行这项设置么

关于 CTTextAlignment textAlignment 和 CTLineBreakMode lineBreakMode 设置 注意 CT开头

CTTextAlignment textAlignment:
typedef CF_ENUM(uint8_t, CTTextAlignment) {
kCTTextAlignmentLeft CT_ENUM_AVAILABLE(10_8, 6_0) = 0,
kCTTextAlignmentRight CT_ENUM_AVAILABLE(10_8, 6_0) = 1,
kCTTextAlignmentCenter CT_ENUM_AVAILABLE(10_8, 6_0) = 2,
kCTTextAlignmentJustified CT_ENUM_AVAILABLE(10_8, 6_0) = 3,
kCTTextAlignmentNatural CT_ENUM_AVAILABLE(10_8, 6_0) = 4,
}

CTLineBreakMode lineBreakMode:
typedef CF_ENUM(uint8_t, CTLineBreakMode) {
kCTLineBreakByWordWrapping = 0,
kCTLineBreakByCharWrapping = 1,
kCTLineBreakByClipping = 2,
kCTLineBreakByTruncatingHead = 3,
kCTLineBreakByTruncatingTail = 4,
kCTLineBreakByTruncatingMiddle = 5
};

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.