diff --git a/TYAttributedLabelDemo.xcodeproj/project.pbxproj b/TYAttributedLabelDemo.xcodeproj/project.pbxproj index c35f241..6028286 100755 --- a/TYAttributedLabelDemo.xcodeproj/project.pbxproj +++ b/TYAttributedLabelDemo.xcodeproj/project.pbxproj @@ -7,6 +7,8 @@ objects = { /* Begin PBXBuildFile section */ + 3CB1450223D0554C0061EF5B /* TextViewController.m in Sources */ = {isa = PBXBuildFile; fileRef = 3CB1450023D0554C0061EF5B /* TextViewController.m */; }; + 3CB1450323D0554C0061EF5B /* TextViewController.xib in Resources */ = {isa = PBXBuildFile; fileRef = 3CB1450123D0554C0061EF5B /* TextViewController.xib */; }; 5454E5721BE49FEC00722E25 /* AutoLayoutTableViewController.m in Sources */ = {isa = PBXBuildFile; fileRef = 5454E5711BE49FEC00722E25 /* AutoLayoutTableViewController.m */; }; 5454E5751BE4A07800722E25 /* AutoLayoutAttributedLabelCell.m in Sources */ = {isa = PBXBuildFile; fileRef = 5454E5741BE4A07800722E25 /* AutoLayoutAttributedLabelCell.m */; }; 5454E5791BE503C400722E25 /* AutoLayoutLinkImageTextViewController.m in Sources */ = {isa = PBXBuildFile; fileRef = 5454E5781BE503C400722E25 /* AutoLayoutLinkImageTextViewController.m */; }; @@ -50,6 +52,9 @@ /* End PBXBuildFile section */ /* Begin PBXFileReference section */ + 3CB144FF23D0554C0061EF5B /* TextViewController.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = TextViewController.h; sourceTree = ""; }; + 3CB1450023D0554C0061EF5B /* TextViewController.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = TextViewController.m; sourceTree = ""; }; + 3CB1450123D0554C0061EF5B /* TextViewController.xib */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = file.xib; path = TextViewController.xib; sourceTree = ""; }; 5454E5701BE49FEC00722E25 /* AutoLayoutTableViewController.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = AutoLayoutTableViewController.h; sourceTree = ""; }; 5454E5711BE49FEC00722E25 /* AutoLayoutTableViewController.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = AutoLayoutTableViewController.m; sourceTree = ""; }; 5454E5731BE4A07800722E25 /* AutoLayoutAttributedLabelCell.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = AutoLayoutAttributedLabelCell.h; sourceTree = ""; }; @@ -226,6 +231,9 @@ D35F47D31AE0DBE30048B58A /* ParseTextViewController.m */, D3553BD01AE778DB005D8F31 /* TYTextStorageParser.h */, D3553BD11AE778DB005D8F31 /* TYTextStorageParser.m */, + 3CB144FF23D0554C0061EF5B /* TextViewController.h */, + 3CB1450023D0554C0061EF5B /* TextViewController.m */, + 3CB1450123D0554C0061EF5B /* TextViewController.xib */, D3709F171AD3A5C000145EC6 /* Main.storyboard */, D3709F1A1AD3A5C000145EC6 /* Images.xcassets */, D3709F1C1AD3A5C000145EC6 /* LaunchScreen.xib */, @@ -322,6 +330,7 @@ developmentRegion = English; hasScannedForEncodings = 0; knownRegions = ( + English, en, Base, ); @@ -340,6 +349,7 @@ isa = PBXResourcesBuildPhase; buildActionMask = 2147483647; files = ( + 3CB1450323D0554C0061EF5B /* TextViewController.xib in Resources */, D3553BCF1AE7746A005D8F31 /* content.json in Resources */, D3709F191AD3A5C000145EC6 /* Main.storyboard in Resources */, B0165EF81AE157FA0003D580 /* CYLoLi.png in Resources */, @@ -377,6 +387,7 @@ B034A2A61BE115AC0015130E /* UIScrollView+TPKeyboardAvoidingAdditions.m in Sources */, B034A2A31BE115930015130E /* TPKeyboardAvoidingScrollView.m in Sources */, D3553BD21AE778DB005D8F31 /* TYTextStorageParser.m in Sources */, + 3CB1450223D0554C0061EF5B /* TextViewController.m in Sources */, B0E7CF1E1BCE9510000341F4 /* TYImageCache.m in Sources */, D35F47D11AE0DB500048B58A /* ImageTextViewController.m in Sources */, D35F47D41AE0DBE30048B58A /* ParseTextViewController.m in Sources */, diff --git a/TYAttributedLabelDemo/TYAttributedLabel/TYTextContainer.m b/TYAttributedLabelDemo/TYAttributedLabel/TYTextContainer.m index 24912a4..8337166 100755 --- a/TYAttributedLabelDemo/TYAttributedLabel/TYTextContainer.m +++ b/TYAttributedLabelDemo/TYAttributedLabel/TYTextContainer.m @@ -249,11 +249,11 @@ - (NSMutableAttributedString *)createTextAttibuteStringWithText:(NSString *)text // 创建属性文本 NSMutableAttributedString *attString = [[NSMutableAttributedString alloc]initWithString:text]; - // 添加文本颜色 字体属性 - [self addTextColorAndFontWithAtrributedString:attString]; - - // 添加文本段落样式 - [self addTextParaphStyleWithAtrributedString:attString]; +// // 添加文本颜色 字体属性 +// [self addTextColorAndFontWithAtrributedString:attString]; +// +// // 添加文本段落样式 +// [self addTextParaphStyleWithAtrributedString:attString]; return attString; } @@ -634,9 +634,9 @@ - (void)appendTextAttributedString:(NSAttributedString *)attributedText _attString = [[NSMutableAttributedString alloc]init]; } - if ([attributedText isKindOfClass:[NSMutableAttributedString class]]) { - [self addTextParaphStyleWithAtrributedString:(NSMutableAttributedString *)attributedText]; - } +// if ([attributedText isKindOfClass:[NSMutableAttributedString class]]) { +// [self addTextParaphStyleWithAtrributedString:(NSMutableAttributedString *)attributedText]; +// } [_attString appendAttributedString:attributedText]; [self resetFrameRef]; diff --git a/TYAttributedLabelDemo/TextViewController.h b/TYAttributedLabelDemo/TextViewController.h new file mode 100644 index 0000000..48b0a25 --- /dev/null +++ b/TYAttributedLabelDemo/TextViewController.h @@ -0,0 +1,17 @@ +// +// TextViewController.h +// TYAttributedLabelDemo +// +// Created by Macrotellect-iOSDev on 2020/1/15. +// Copyright © 2020 tanyang. All rights reserved. +// + +#import + +NS_ASSUME_NONNULL_BEGIN + +@interface TextViewController : UIViewController + +@end + +NS_ASSUME_NONNULL_END diff --git a/TYAttributedLabelDemo/TextViewController.m b/TYAttributedLabelDemo/TextViewController.m new file mode 100644 index 0000000..5e5d612 --- /dev/null +++ b/TYAttributedLabelDemo/TextViewController.m @@ -0,0 +1,153 @@ +// +// TextViewController.m +// TYAttributedLabelDemo +// +// Created by Macrotellect-iOSDev on 2020/1/15. +// Copyright © 2020 tanyang. All rights reserved. +// +#define RGB(r,g,b,a) [UIColor colorWithRed:r/255.0 green:g/255.0 blue:b/255.0 alpha:a] +#import "TextViewController.h" +#import "TYAttributedLabel.h" +@interface TextViewController () +@property (weak, nonatomic) IBOutlet TYAttributedLabel *label1; + +@end + +@implementation TextViewController + +- (void)viewDidLoad { + [super viewDidLoad]; + // Do any additional setup after loading the view from its nib. + + [self addTextAttributedLabel1]; + +} + +- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration +{ + NSLog(@"%s", __func__); + if (UIInterfaceOrientationIsPortrait(toInterfaceOrientation)) { + [self addTextAttributedLabel1]; + }else{ + [self addTextAttributedLabel1]; + } +} + + + +- (void)addTextAttributedLabel1 +{ + _label1.attributedText = [[NSAttributedString alloc] initWithString:@""]; + _label1.delegate = self; + _label1.highlightedLinkColor = [UIColor orangeColor]; + + NSString *text = @"总有一天你将破蛹而出,成长得比人们期待的还要美丽。\n\t但这个过程会很痛,会很辛苦,有时候还会觉得灰心但这个过程会很痛,会很辛苦,有时候还会觉得灰心但这个过程会很痛,会很辛苦,有时候还会觉得灰心但这个过程会很痛,会很辛苦,有时候还会觉得灰心但这个过程会很痛,会很辛苦,有时候还会觉得灰心但这个过程会很痛,会很辛苦,有时候还会觉得灰心但这个过程会很痛,会很辛苦,有时候还会觉得灰心但这个过程会很痛,会很辛苦,有时候还会觉得灰心但这个过程会很痛,会很辛苦,有时候还会觉得灰心\n\t面对着汹涌而来的现实,觉得自己渺小无力。\n\t但这,也是生命的一部分,做好现在你能做的,然后,一切都会好的。\n\t我们都将孤独地长大,不要害怕。\n\t总有一天你将破蛹而出,成长得比人们期待的还要美丽。\n\t但这个过程会很痛,会很辛苦,有时候还会觉得灰心。\n\t面对着汹涌而来的现实,觉得自己渺小无力。\n\t但这,也是生命的一部分,做好现在你能做的,然后,一切都会好的。\n\t我们都将孤独地长大,不要害怕。"; + + NSArray *textArray = [text componentsSeparatedByString:@"\n\t"]; + NSArray *colorArray = @[RGB(213, 0, 0, 1),RGB(0, 155, 0, 1),RGB(103, 0, 207, 1),RGB(209, 162, 74, 1),RGB(206, 39, 206, 1)]; + NSInteger index = 0; + + for (NSString *text in textArray) { + + if (index == 2) { + // 追加链接信息 + [_label1 appendLinkWithText:text linkFont:[UIFont systemFontOfSize:15+arc4random()%4] linkData:@"http://www.baidu.com"]; + }else { + + NSLog(@"=====index: %ld",(long)index); + // 追加文本属性 + NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc]initWithString:text]; + //局部 + [attributedString addAttributeCharacterSpacing:10]; + [attributedString addAttributeAlignmentStyle:kCTTextAlignmentLeft lineSpaceStyle:10.0 paragraphSpaceStyle:20.0 lineBreakStyle:kCTLineBreakByWordWrapping]; + [attributedString addAttributeTextColor:colorArray[index%5]]; + [attributedString addAttributeFont:[UIFont systemFontOfSize:15+arc4random()%4]]; + + [_label1 appendTextAttributedString:attributedString]; + } + [_label1 appendText:@"\n\t 11111 \n\t"]; + index++; + } + //全局 +// _label1.characterSpacing =20; +// _label1.linesSpacing = 20.0; +// _label1.paragraphSpacing =20.0; + [_label1 appendLinkWithText:@"百度一下" linkFont:[UIFont systemFontOfSize:15+arc4random()%4] linkData:@"http://www.baidu.com"]; + + +} + +- (void)addTextAttributedLabel2 +{ + NSString *text = @"\t任何值得去的地方,都没有捷径;\n\t任何值得等待的人,都会迟来一些;\n\t任何值得追逐的梦想,都必须在一路艰辛中备受嘲笑。\n\t所以,不要怕,不要担心你所追逐的有可能是错的。\n\t因为,不被嘲笑的梦想不是梦想。\n"; + + NSArray *textArray = [text componentsSeparatedByString:@"\n\t"]; + NSArray *colorArray = @[RGB(213, 0, 0, 1),RGB(0, 155, 0, 1),RGB(103, 0, 207, 1),RGB(209, 162, 74, 1),RGB(206, 39, 206, 1)]; + NSInteger index = 0; + NSMutableArray *textRunArray = [NSMutableArray array]; + + for (NSString *subText in textArray) { + if (index == 2) { + TYLinkTextStorage *linkTextStorage = [[TYLinkTextStorage alloc]init]; + linkTextStorage.range = [text rangeOfString:subText]; + linkTextStorage.font = [UIFont systemFontOfSize:15+arc4random()%4]; + linkTextStorage.textColor = colorArray[index%5]; + linkTextStorage.linkData = @"我被点中了哦O(∩_∩)O~"; + [textRunArray addObject:linkTextStorage]; + } else { + TYTextStorage *textStorage = [[TYTextStorage alloc]init]; + textStorage.font = [UIFont systemFontOfSize:15+arc4random()%4]; + textStorage.textColor = colorArray[index%5]; + textStorage.range = [text rangeOfString:subText]; + [textRunArray addObject:textStorage]; + } + index++; + } + + TYAttributedLabel *label2 = [[TYAttributedLabel alloc]init]; + label2.delegate = self; + label2.highlightedLinkColor = [UIColor redColor]; + label2.text = text; + [label2 addTextStorageArray:textRunArray]; + + label2.linesSpacing = 8; + label2.characterSpacing = 2; + [label2 setFrameWithOrign:CGPointMake(0, CGRectGetMaxY(_label1.frame)) Width:CGRectGetWidth(self.view.frame)]; + [self.view addSubview:label2]; + +} + +#pragma mark - TYAttributedLabelDelegate + +- (void)attributedLabel:(TYAttributedLabel *)attributedLabel textStorageClicked:(id)TextRun atPoint:(CGPoint)point +{ + NSLog(@"textStorageClickedAtPoint"); + if ([TextRun isKindOfClass:[TYLinkTextStorage class]]) { + + NSString *linkStr = ((TYLinkTextStorage*)TextRun).linkData; + + if ([linkStr hasPrefix:@"http:"]) { + [ [ UIApplication sharedApplication] openURL:[ NSURL URLWithString:linkStr]]; + }else { + UIAlertView *alertView = [[UIAlertView alloc]initWithTitle:@"点击提示" message:linkStr delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil]; + [alertView show]; + } + } +} + +- (void)attributedLabel:(TYAttributedLabel *)attributedLabel textStorageLongPressed:(id)textStorage onState:(UIGestureRecognizerState)state atPoint:(CGPoint)point +{ + NSLog(@"textStorageLongPressed"); +} + +/* +#pragma mark - Navigation + +// In a storyboard-based application, you will often want to do a little preparation before navigation +- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender { + // Get the new view controller using [segue destinationViewController]. + // Pass the selected object to the new view controller. +} +*/ + +@end diff --git a/TYAttributedLabelDemo/TextViewController.xib b/TYAttributedLabelDemo/TextViewController.xib new file mode 100644 index 0000000..734ae0d --- /dev/null +++ b/TYAttributedLabelDemo/TextViewController.xib @@ -0,0 +1,51 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/TYAttributedLabelDemo/ViewController.m b/TYAttributedLabelDemo/ViewController.m index 1b23971..37ffcaf 100644 --- a/TYAttributedLabelDemo/ViewController.m +++ b/TYAttributedLabelDemo/ViewController.m @@ -18,7 +18,7 @@ #import "TextTableViewController.h" #import "AutoLayoutTableViewController.h" #import "LabelXibViewController.h" - +#import "TextViewController.h" @interface tableViewItem : NSObject @property (nonatomic, strong) NSString *title; @@ -103,6 +103,8 @@ - (void)addTableItems [self addTableItemWithTitle:@"AttributedTextCell" detailText:@"tableViewCell显示图文混排" destVcClass:[TextTableViewController class]]; [self addTableItemWithTitle:@"AutoLayoutAttributedTextCell" detailText:@"Autolayout tableViewCell显示图文混排" destVcClass:[AutoLayoutTableViewController class]]; + [self addTableItemWithTitle:@"TextViewController" detailText:@"TextViewController_My" destVcClass:[TextViewController class]]; + } - (void)addTableItemWithTitle:(NSString *)title detailText:(NSString *)detailText destVcClass:(Class)destVcClass @@ -139,6 +141,10 @@ - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(N - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { + if (indexPath.row == self.itemArray.count -1) { + [self.navigationController pushViewController:[[TextViewController alloc] initWithNibName:@"TextViewController" bundle:nil] animated:YES]; + return; + } tableViewItem *item = self.itemArray[indexPath.row]; if (item.destVcClass ) {