Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

添加了 #153

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions TYAttributedLabelDemo.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -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 */; };
Expand Down Expand Up @@ -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 = "<group>"; };
3CB1450023D0554C0061EF5B /* TextViewController.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = TextViewController.m; sourceTree = "<group>"; };
3CB1450123D0554C0061EF5B /* TextViewController.xib */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = file.xib; path = TextViewController.xib; sourceTree = "<group>"; };
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>"; };
Expand Down Expand Up @@ -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 */,
Expand Down Expand Up @@ -322,6 +330,7 @@
developmentRegion = English;
hasScannedForEncodings = 0;
knownRegions = (
English,
en,
Base,
);
Expand All @@ -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 */,
Expand Down Expand Up @@ -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 */,
Expand Down
16 changes: 8 additions & 8 deletions TYAttributedLabelDemo/TYAttributedLabel/TYTextContainer.m
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down Expand Up @@ -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];
Expand Down
17 changes: 17 additions & 0 deletions TYAttributedLabelDemo/TextViewController.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
//
// TextViewController.h
// TYAttributedLabelDemo
//
// Created by Macrotellect-iOSDev on 2020/1/15.
// Copyright © 2020 tanyang. All rights reserved.
//

#import <UIKit/UIKit.h>

NS_ASSUME_NONNULL_BEGIN

@interface TextViewController : UIViewController

@end

NS_ASSUME_NONNULL_END
153 changes: 153 additions & 0 deletions TYAttributedLabelDemo/TextViewController.m
Original file line number Diff line number Diff line change
@@ -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 ()<TYAttributedLabelDelegate>
@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<TYTextStorageProtocol>)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<TYTextStorageProtocol>)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
51 changes: 51 additions & 0 deletions TYAttributedLabelDemo/TextViewController.xib
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<?xml version="1.0" encoding="UTF-8"?>
<document type="com.apple.InterfaceBuilder3.CocoaTouch.XIB" version="3.0" toolsVersion="15505" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" useTraitCollections="YES" colorMatched="YES">
<device id="retina6_1" orientation="portrait" appearance="light"/>
<dependencies>
<deployment identifier="iOS"/>
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="15510"/>
<capability name="documents saved in the Xcode 8 format" minToolsVersion="8.0"/>
</dependencies>
<objects>
<placeholder placeholderIdentifier="IBFilesOwner" id="-1" userLabel="File's Owner" customClass="TextViewController">
<connections>
<outlet property="label1" destination="eOk-Z0-VN8" id="Hxw-yu-Ec4"/>
<outlet property="view" destination="i5M-Pr-FkT" id="sfx-zR-JGt"/>
</connections>
</placeholder>
<placeholder placeholderIdentifier="IBFirstResponder" id="-2" customClass="UIResponder"/>
<view clearsContextBeforeDrawing="NO" contentMode="scaleToFill" id="i5M-Pr-FkT">
<rect key="frame" x="0.0" y="0.0" width="414" height="896"/>
<autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/>
<subviews>
<scrollView clipsSubviews="YES" multipleTouchEnabled="YES" contentMode="scaleToFill" translatesAutoresizingMaskIntoConstraints="NO" id="kuX-RC-HPl">
<rect key="frame" x="87" y="100" width="240" height="696"/>
<subviews>
<view contentMode="scaleToFill" translatesAutoresizingMaskIntoConstraints="NO" id="eOk-Z0-VN8" customClass="TYAttributedLabel">
<rect key="frame" x="0.0" y="0.0" width="240" height="1000"/>
<color key="backgroundColor" systemColor="systemBackgroundColor" cocoaTouchSystemColor="whiteColor"/>
<constraints>
<constraint firstAttribute="height" constant="1000" id="a32-qH-Y7z"/>
</constraints>
</view>
</subviews>
<constraints>
<constraint firstItem="eOk-Z0-VN8" firstAttribute="leading" secondItem="kuX-RC-HPl" secondAttribute="leading" id="gFM-mf-Esd"/>
<constraint firstItem="eOk-Z0-VN8" firstAttribute="width" secondItem="kuX-RC-HPl" secondAttribute="width" id="hp3-hi-48m"/>
<constraint firstAttribute="trailing" secondItem="eOk-Z0-VN8" secondAttribute="trailing" id="jbc-3N-njT"/>
<constraint firstAttribute="bottom" secondItem="eOk-Z0-VN8" secondAttribute="bottom" constant="20" symbolic="YES" id="jed-c1-cMM"/>
<constraint firstItem="eOk-Z0-VN8" firstAttribute="top" secondItem="kuX-RC-HPl" secondAttribute="top" id="lQn-fK-q0N"/>
</constraints>
</scrollView>
</subviews>
<color key="backgroundColor" red="1" green="1" blue="1" alpha="1" colorSpace="custom" customColorSpace="sRGB"/>
<constraints>
<constraint firstAttribute="trailing" secondItem="kuX-RC-HPl" secondAttribute="trailing" constant="87" id="niR-Af-mNx"/>
<constraint firstAttribute="bottom" secondItem="kuX-RC-HPl" secondAttribute="bottom" constant="100" id="vtW-Pw-X1T"/>
<constraint firstItem="kuX-RC-HPl" firstAttribute="top" secondItem="i5M-Pr-FkT" secondAttribute="top" constant="100" id="wJl-iM-lwD"/>
<constraint firstItem="kuX-RC-HPl" firstAttribute="leading" secondItem="i5M-Pr-FkT" secondAttribute="leading" constant="87" id="xwo-Mb-3GC"/>
</constraints>
<point key="canvasLocation" x="137.68115942028987" y="130.58035714285714"/>
</view>
</objects>
</document>
8 changes: 7 additions & 1 deletion TYAttributedLabelDemo/ViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
#import "TextTableViewController.h"
#import "AutoLayoutTableViewController.h"
#import "LabelXibViewController.h"

#import "TextViewController.h"
@interface tableViewItem : NSObject

@property (nonatomic, strong) NSString *title;
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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 ) {
Expand Down