Skip to content

Commit

Permalink
add autolayout support\ add demo
Browse files Browse the repository at this point in the history
  • Loading branch information
xinzhengzhang committed Oct 31, 2015
1 parent 8db516b commit 2d18930
Show file tree
Hide file tree
Showing 9 changed files with 260 additions and 0 deletions.
4 changes: 4 additions & 0 deletions .gitignore
@@ -0,0 +1,4 @@
.DS_Store
xcuserdata
xcshareddata
*.xccheckout
12 changes: 12 additions & 0 deletions TYAttributedLabelDemo.xcodeproj/project.pbxproj
Expand Up @@ -7,6 +7,8 @@
objects = {

/* Begin PBXBuildFile section */
5454E5721BE49FEC00722E25 /* AutoLayoutTableViewController.m in Sources */ = {isa = PBXBuildFile; fileRef = 5454E5711BE49FEC00722E25 /* AutoLayoutTableViewController.m */; settings = {ASSET_TAGS = (); }; };
5454E5751BE4A07800722E25 /* AutoLayoutAttributedLabelCell.m in Sources */ = {isa = PBXBuildFile; fileRef = 5454E5741BE4A07800722E25 /* AutoLayoutAttributedLabelCell.m */; settings = {ASSET_TAGS = (); }; };
B00F2C6C1B24666500EECDCE /* TYTextContainer+Extended.m in Sources */ = {isa = PBXBuildFile; fileRef = B00F2C6B1B24666500EECDCE /* TYTextContainer+Extended.m */; };
B0165EF81AE157FA0003D580 /* CYLoLi.png in Resources */ = {isa = PBXBuildFile; fileRef = B0165EF71AE157FA0003D580 /* CYLoLi.png */; };
B03135751BA05FCB00D17041 /* AttributedLabelCell.m in Sources */ = {isa = PBXBuildFile; fileRef = B03135741BA05FCB00D17041 /* AttributedLabelCell.m */; };
Expand Down Expand Up @@ -56,6 +58,10 @@
/* End PBXContainerItemProxy section */

/* Begin PBXFileReference section */
5454E5701BE49FEC00722E25 /* AutoLayoutTableViewController.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = AutoLayoutTableViewController.h; sourceTree = "<group>"; };
5454E5711BE49FEC00722E25 /* AutoLayoutTableViewController.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = AutoLayoutTableViewController.m; sourceTree = "<group>"; };
5454E5731BE4A07800722E25 /* AutoLayoutAttributedLabelCell.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = AutoLayoutAttributedLabelCell.h; sourceTree = "<group>"; };
5454E5741BE4A07800722E25 /* AutoLayoutAttributedLabelCell.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = AutoLayoutAttributedLabelCell.m; sourceTree = "<group>"; };
B00F2C6B1B24666500EECDCE /* TYTextContainer+Extended.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = "TYTextContainer+Extended.m"; sourceTree = "<group>"; };
B0165EF71AE157FA0003D580 /* CYLoLi.png */ = {isa = PBXFileReference; lastKnownFileType = image.png; path = CYLoLi.png; sourceTree = "<group>"; };
B03135731BA05FCB00D17041 /* AttributedLabelCell.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = AttributedLabelCell.h; sourceTree = "<group>"; };
Expand Down Expand Up @@ -154,6 +160,8 @@
B034A29F1BE1060B0015130E /* TExamTextField.m */,
B03135731BA05FCB00D17041 /* AttributedLabelCell.h */,
B03135741BA05FCB00D17041 /* AttributedLabelCell.m */,
5454E5731BE4A07800722E25 /* AutoLayoutAttributedLabelCell.h */,
5454E5741BE4A07800722E25 /* AutoLayoutAttributedLabelCell.m */,
);
name = views;
sourceTree = "<group>";
Expand Down Expand Up @@ -202,6 +210,8 @@
B03407401B27049300DCEACB /* TextContainerViewController.m */,
B0E11A6B1BA05A3A0044523D /* TextTableViewController.h */,
B0E11A6C1BA05A3A0044523D /* TextTableViewController.m */,
5454E5701BE49FEC00722E25 /* AutoLayoutTableViewController.h */,
5454E5711BE49FEC00722E25 /* AutoLayoutTableViewController.m */,
D35F47D21AE0DBE30048B58A /* ParseTextViewController.h */,
D35F47D31AE0DBE30048B58A /* ParseTextViewController.m */,
D3553BD01AE778DB005D8F31 /* TYTextStorageParser.h */,
Expand Down Expand Up @@ -380,6 +390,7 @@
B03407411B27049300DCEACB /* TextContainerViewController.m in Sources */,
D3553BD51AE79106005D8F31 /* AddViewTextViewController.m in Sources */,
D35F47CE1AE0DB370048B58A /* AttributedTextViewController.m in Sources */,
5454E5751BE4A07800722E25 /* AutoLayoutAttributedLabelCell.m in Sources */,
D3C623FE1B20348400C15D94 /* TYTextContainer.m in Sources */,
D34A49951AD50EB70036B7EF /* TYImageStorage.m in Sources */,
D3709F131AD3A5C000145EC6 /* AppDelegate.m in Sources */,
Expand All @@ -399,6 +410,7 @@
D34A498E1AD4FA760036B7EF /* TYDrawStorage.m in Sources */,
D34A49911AD4FF370036B7EF /* TYLinkTextStorage.m in Sources */,
D3709F361AD3A60800145EC6 /* TYAttributedLabel.m in Sources */,
5454E5721BE49FEC00722E25 /* AutoLayoutTableViewController.m in Sources */,
D35F47CB1AE0DAF20048B58A /* SimpleTextViewController.m in Sources */,
B0E11A6D1BA05A3A0044523D /* TextTableViewController.m in Sources */,
D3553BCC1AE732C8005D8F31 /* LinkTextViewController.m in Sources */,
Expand Down
16 changes: 16 additions & 0 deletions TYAttributedLabelDemo/AutoLayoutAttributedLabelCell.h
@@ -0,0 +1,16 @@
//
// AutoLayoutAttributedLabelCell.h
// TYAttributedLabelDemo
//
// Created by zhangxinzheng on 10/31/15.
// Copyright © 2015 tanyang. All rights reserved.
//

#import <UIKit/UIKit.h>
#import "TYAttributedLabel.h"

@interface AutoLayoutAttributedLabelCell : UITableViewCell

@property (nonatomic, weak, readonly) TYAttributedLabel *label;

@end
51 changes: 51 additions & 0 deletions TYAttributedLabelDemo/AutoLayoutAttributedLabelCell.m
@@ -0,0 +1,51 @@
//
// AutoLayoutAttributedLabelCell.m
// TYAttributedLabelDemo
//
// Created by zhangxinzheng on 10/31/15.
// Copyright © 2015 tanyang. All rights reserved.
//

#import "AutoLayoutAttributedLabelCell.h"

@interface AutoLayoutAttributedLabelCell ()
@property (nonatomic, weak) TYAttributedLabel *label;
@end

@implementation AutoLayoutAttributedLabelCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
self.selectionStyle = UITableViewCellSelectionStyleNone;
[self addAtrribuedLabel];
}
return self;
}

- (id)initWithCoder:(NSCoder *)aDecoder
{
if (self = [super initWithCoder:aDecoder]) {
[self addAtrribuedLabel];
}
return self;
}

- (void)addAtrribuedLabel
{
TYAttributedLabel *label = [[TYAttributedLabel alloc]init];
label.translatesAutoresizingMaskIntoConstraints = NO;
[self.contentView addSubview:label];
_label = label;
[NSLayoutConstraint activateConstraints: [NSLayoutConstraint constraintsWithVisualFormat:@"V:|-15-[label]-15-|" options:0 metrics:nil views:@{@"label":_label}]];
[NSLayoutConstraint activateConstraints: [NSLayoutConstraint constraintsWithVisualFormat:@"H:|-5-[label]-5-|" options:0 metrics:nil views:@{@"label":_label}]];

}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
[super setSelected:selected animated:animated];

// Configure the view for the selected state
}

@end
13 changes: 13 additions & 0 deletions TYAttributedLabelDemo/AutoLayoutTableViewController.h
@@ -0,0 +1,13 @@
//
// AutoLayoutTableViewController.h
// TYAttributedLabelDemo
//
// Created by zhangxinzheng on 10/31/15.
// Copyright © 2015 tanyang. All rights reserved.
//

#import <UIKit/UIKit.h>

@interface AutoLayoutTableViewController : UITableViewController

@end
144 changes: 144 additions & 0 deletions TYAttributedLabelDemo/AutoLayoutTableViewController.m
@@ -0,0 +1,144 @@
//
// AutoLayoutTableViewController.m
// TYAttributedLabelDemo
//
// Created by zhangxinzheng on 10/31/15.
// Copyright © 2015 tanyang. All rights reserved.
//

#import "AutoLayoutTableViewController.h"
#import "AutoLayoutAttributedLabelCell.h"
#import "RegexKitLite.h"

@interface AutoLayoutTableViewController ()<TYAttributedLabelDelegate>
@property (nonatomic, strong) NSArray *textContainers;
@end

static NSString *cellId = @"AutoLayoutAttributedLabelCell";
#define RGB(r,g,b,a) [UIColor colorWithRed:r/255.0 green:g/255.0 blue:b/255.0 alpha:a]

@implementation AutoLayoutTableViewController

- (void)viewDidLoad {
[super viewDidLoad];

[self.tableView registerClass:[AutoLayoutAttributedLabelCell class] forCellReuseIdentifier:cellId];

[self addTableViewItems];
self.tableView.estimatedRowHeight = 40;
self.tableView.rowHeight = UITableViewAutomaticDimension;
}

- (void)addTableViewItems
{
NSMutableArray *tmp = [NSMutableArray array];

for (NSInteger i = 0; i < 16; ++i) {
[tmp addObject:[self creatTextContainer]];
}
_textContainers = [tmp copy];
}

- (TYTextContainer *)creatTextContainer
{
NSString *text = @"@青春励志: [haha,15,15]其实所有漂泊的人,[haha,15,15]不过是为了有一天能够不再漂泊,[haha,15,15][haha,15,15]能用自己的力量撑起身后的家人和自己爱的人。 [haha,15,15]#青春励志#[button]";

// 属性文本生成器
TYTextContainer *textContainer = [[TYTextContainer alloc]init];
textContainer.text = text;
NSMutableArray *tmpArray = [NSMutableArray array];

// 正则匹配图片信息
[text enumerateStringsMatchedByRegex:@"\\[(\\w+?),(\\d+?),(\\d+?)\\]" usingBlock:^(NSInteger captureCount, NSString *const __unsafe_unretained *capturedStrings, const NSRange *capturedRanges, volatile BOOL *const stop) {

if (captureCount > 3) {
// 图片信息储存
TYImageStorage *imageStorage = [[TYImageStorage alloc]init];
imageStorage.cacheImageOnMemory = YES;
imageStorage.imageName = capturedStrings[1];
imageStorage.range = capturedRanges[0];
imageStorage.size = CGSizeMake([capturedStrings[2]intValue], [capturedStrings[3]intValue]);

[tmpArray addObject:imageStorage];
}
}];

// 添加图片信息数组到label
[textContainer addTextStorageArray:tmpArray];

[textContainer addLinkWithLinkData:@"点击了@青春励志" linkColor:nil underLineStyle:kCTUnderlineStyleNone range:[text rangeOfString:@"@青春励志"]];

[textContainer addLinkWithLinkData:@"点击了#青春励志#" linkColor:nil underLineStyle:kCTUnderlineStyleNone range:[text rangeOfString:@"#青春励志#"]];

TYTextStorage *textStorage = [[TYTextStorage alloc]init];
textStorage.range = [text rangeOfString:@"[CYLoLi,320,180]其实所有漂泊的人,"];
textStorage.textColor = RGB(213, 0, 0, 1);
textStorage.font = [UIFont systemFontOfSize:16];
[textContainer addTextStorage:textStorage];

textStorage = [[TYTextStorage alloc]init];
textStorage.range = [text rangeOfString:@"不过是为了有一天能够不再漂泊,"];
textStorage.textColor = RGB(0, 155, 0, 1);
textStorage.font = [UIFont systemFontOfSize:18];
[textContainer addTextStorage:textStorage];

UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
button.layer.cornerRadius = 2;
[button setBackgroundColor:[UIColor redColor]];
button.titleLabel.font = [UIFont systemFontOfSize:12];
[button setTitle:@"UIButton" forState:UIControlStateNormal];
[button addTarget:self action:@selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];
button.frame = CGRectMake(0, 0, 60, 15);
[textContainer addView:button range:[text rangeOfString:@"[button]"]];
textContainer.linesSpacing = 2;
textContainer = [textContainer createTextContainerWithTextWidth:CGRectGetWidth(self.view.frame)];
return textContainer;
}

#pragma mark - action

- (void)buttonClicked:(UIButton *)button
{
UIAlertView *alertView = [[UIAlertView alloc]initWithTitle:@"点击提示" message:@"我是UIButton哦" delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil];
[alertView show];
}

#pragma mark - Table view data source

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
// Return the number of rows in the section.
return _textContainers.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
AutoLayoutAttributedLabelCell *cell = [tableView dequeueReusableCellWithIdentifier:cellId forIndexPath:indexPath];

// Configure the cell...
cell.label.delegate = self;
cell.label.textContainer = _textContainers[indexPath.row];
cell.label.preferredMaxLayoutWidth = CGRectGetWidth(tableView.frame);

return cell;
}

#pragma mark - TYAttributedLabelDelegate

- (void)attributedLabel:(TYAttributedLabel *)attributedLabel textStorageClicked:(id<TYTextStorageProtocol>)TextRun atPoint:(CGPoint)point
{
NSLog(@"textStorageClickedAtPoint");
if ([TextRun isKindOfClass:[TYLinkTextStorage class]]) {

id linkStr = ((TYLinkTextStorage*)TextRun).linkData;
if ([linkStr isKindOfClass:[NSString class]]) {
UIAlertView *alertView = [[UIAlertView alloc]initWithTitle:@"点击提示" message:linkStr delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil];
[alertView show];
}
}else if ([TextRun isKindOfClass:[TYImageStorage class]]) {
TYImageStorage *imageStorage = (TYImageStorage *)TextRun;
UIAlertView *alertView = [[UIAlertView alloc]initWithTitle:@"点击提示" message:[NSString stringWithFormat:@"你点击了%@图片",imageStorage.imageName] delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil];
[alertView show];
}
}

@end

5 changes: 5 additions & 0 deletions TYAttributedLabelDemo/TYAttributedLabel/TYAttributedLabel.h
Expand Up @@ -44,6 +44,11 @@

@property (nonatomic, strong) TYTextContainer *textContainer;

/**
* Autolayout
*/
@property(nonatomic) CGFloat preferredMaxLayoutWidth NS_AVAILABLE_IOS(6_0);

/**
* 获取普通文本内容
*/
Expand Down
13 changes: 13 additions & 0 deletions TYAttributedLabelDemo/TYAttributedLabel/TYAttributedLabel.m
Expand Up @@ -641,6 +641,19 @@ - (CGSize)sizeThatFits:(CGSize)size
return CGSizeMake(width, height);
}

- (void)setPreferredMaxLayoutWidth:(CGFloat)preferredMaxLayoutWidth
{
if (_preferredMaxLayoutWidth != preferredMaxLayoutWidth) {
_preferredMaxLayoutWidth = preferredMaxLayoutWidth;
[self invalidateIntrinsicContentSize];
}
}

- (CGSize)intrinsicContentSize
{
return CGSizeMake(self.preferredMaxLayoutWidth, [self getHeightWithWidth:self.preferredMaxLayoutWidth]);
}

#pragma mark - set right frame
- (void)setFrameWithOrign:(CGPoint)orign Width:(CGFloat)width
{
Expand Down
2 changes: 2 additions & 0 deletions TYAttributedLabelDemo/ViewController.m
Expand Up @@ -15,6 +15,7 @@
#import "AddViewTextViewController.h"
#import "TextContainerViewController.h"
#import "TextTableViewController.h"
#import "AutoLayoutTableViewController.h"

@interface tableViewItem : NSObject

Expand Down Expand Up @@ -87,6 +88,7 @@ - (void)addTableItems
[self addTableItemWithTitle:@"ParseText" detailText:@"自定义排版解析图文混排显示" destVcClass:[ParseTextViewController class]];

[self addTableItemWithTitle:@"AttributedTextCell" detailText:@"tableViewCell显示图文混排" destVcClass:[TextTableViewController class]];
[self addTableItemWithTitle:@"AutoLayoutAttributedTextCell" detailText:@"Autolayout tableViewCell显示图文混排" destVcClass:[AutoLayoutTableViewController class]];

}

Expand Down

0 comments on commit 2d18930

Please sign in to comment.