Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
8db516b
commit 2d18930
Showing
9 changed files
with
260 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
.DS_Store | ||
xcuserdata | ||
xcshareddata | ||
*.xccheckout |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters