Skip to content

Commit 0b21f43

Browse files
committed
增加xib适配
1 parent f00c2b7 commit 0b21f43

File tree

8 files changed

+181
-6
lines changed

8 files changed

+181
-6
lines changed

MYCoreText_Example.xcodeproj/project.pbxproj

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,9 @@
2424
B8894BE91E816D3100CC06DA /* LinksViewController.m in Sources */ = {isa = PBXBuildFile; fileRef = B8894BE11E816D3100CC06DA /* LinksViewController.m */; };
2525
B8894BEA1E816D3100CC06DA /* showCell.m in Sources */ = {isa = PBXBuildFile; fileRef = B8894BE31E816D3100CC06DA /* showCell.m */; };
2626
B8894BEB1E816D3100CC06DA /* tableViewModel.m in Sources */ = {isa = PBXBuildFile; fileRef = B8894BE51E816D3100CC06DA /* tableViewModel.m */; };
27+
B9B3BAE822014B3C005C76D2 /* XibViewController.m in Sources */ = {isa = PBXBuildFile; fileRef = B9B3BAE722014B3C005C76D2 /* XibViewController.m */; };
28+
B9B3BAF722015981005C76D2 /* XibTestView.m in Sources */ = {isa = PBXBuildFile; fileRef = B9B3BAF622015981005C76D2 /* XibTestView.m */; };
29+
B9B3BAF92201598B005C76D2 /* XibTestView.xib in Resources */ = {isa = PBXBuildFile; fileRef = B9B3BAF82201598B005C76D2 /* XibTestView.xib */; };
2730
/* End PBXBuildFile section */
2831

2932
/* Begin PBXContainerItemProxy section */
@@ -78,6 +81,11 @@
7881
B8894BE31E816D3100CC06DA /* showCell.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = showCell.m; sourceTree = "<group>"; };
7982
B8894BE41E816D3100CC06DA /* tableViewModel.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = tableViewModel.h; sourceTree = "<group>"; };
8083
B8894BE51E816D3100CC06DA /* tableViewModel.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = tableViewModel.m; sourceTree = "<group>"; };
84+
B9B3BAE622014B3C005C76D2 /* XibViewController.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = XibViewController.h; sourceTree = "<group>"; };
85+
B9B3BAE722014B3C005C76D2 /* XibViewController.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = XibViewController.m; sourceTree = "<group>"; };
86+
B9B3BAF522015981005C76D2 /* XibTestView.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = XibTestView.h; sourceTree = "<group>"; };
87+
B9B3BAF622015981005C76D2 /* XibTestView.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = XibTestView.m; sourceTree = "<group>"; };
88+
B9B3BAF82201598B005C76D2 /* XibTestView.xib */ = {isa = PBXFileReference; lastKnownFileType = file.xib; path = XibTestView.xib; sourceTree = "<group>"; };
8189
/* End PBXFileReference section */
8290

8391
/* Begin PBXFrameworksBuildPhase section */
@@ -193,6 +201,11 @@
193201
B8894BDF1E816D3100CC06DA /* KeywordViewController.m */,
194202
B8894BE01E816D3100CC06DA /* LinksViewController.h */,
195203
B8894BE11E816D3100CC06DA /* LinksViewController.m */,
204+
B9B3BAE622014B3C005C76D2 /* XibViewController.h */,
205+
B9B3BAE722014B3C005C76D2 /* XibViewController.m */,
206+
B9B3BAF522015981005C76D2 /* XibTestView.h */,
207+
B9B3BAF622015981005C76D2 /* XibTestView.m */,
208+
B9B3BAF82201598B005C76D2 /* XibTestView.xib */,
196209
B8894BE21E816D3100CC06DA /* showCell.h */,
197210
B8894BE31E816D3100CC06DA /* showCell.m */,
198211
B8894BE41E816D3100CC06DA /* tableViewModel.h */,
@@ -309,6 +322,7 @@
309322
files = (
310323
B801264C1E56BFAF006BAF70 /* LaunchScreen.storyboard in Resources */,
311324
B80126491E56BFAF006BAF70 /* Assets.xcassets in Resources */,
325+
B9B3BAF92201598B005C76D2 /* XibTestView.xib in Resources */,
312326
B80126471E56BFAF006BAF70 /* Main.storyboard in Resources */,
313327
);
314328
runOnlyForDeploymentPostprocessing = 0;
@@ -336,13 +350,15 @@
336350
files = (
337351
B8894BE81E816D3100CC06DA /* KeywordViewController.m in Sources */,
338352
B8894BEA1E816D3100CC06DA /* showCell.m in Sources */,
353+
B9B3BAE822014B3C005C76D2 /* XibViewController.m in Sources */,
339354
B80126991E56C016006BAF70 /* MYCoreTextLabel.m in Sources */,
340355
B80126441E56BFAF006BAF70 /* ViewController.m in Sources */,
341356
B8894BE61E816D3100CC06DA /* DetailViewController.m in Sources */,
342357
B80126411E56BFAF006BAF70 /* AppDelegate.m in Sources */,
343358
B801269B1E56C016006BAF70 /* MYSubCoretextResult.m in Sources */,
344359
B8894BE71E816D3100CC06DA /* ImageViewController.m in Sources */,
345360
B801263E1E56BFAF006BAF70 /* main.m in Sources */,
361+
B9B3BAF722015981005C76D2 /* XibTestView.m in Sources */,
346362
B8894BE91E816D3100CC06DA /* LinksViewController.m in Sources */,
347363
B801269A1E56C016006BAF70 /* MYCoretextResultTool.m in Sources */,
348364
B8894BEB1E816D3100CC06DA /* tableViewModel.m in Sources */,
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
//
2+
// XibTestView.h
3+
// MYCoreText_Example
4+
//
5+
// Created by 孟遥 on 2019/1/30.
6+
// Copyright © 2019年 mengyao. All rights reserved.
7+
//
8+
9+
#import <UIKit/UIKit.h>
10+
11+
@interface XibTestView : UIView
12+
+(instancetype)loadXibView;
13+
@property (nonatomic, copy) NSString *content;
14+
@end
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
//
2+
// XibTestView.m
3+
// MYCoreText_Example
4+
//
5+
// Created by 孟遥 on 2019/1/30.
6+
// Copyright © 2019年 mengyao. All rights reserved.
7+
//
8+
9+
#import "XibTestView.h"
10+
#import "MYCoreTextLabel.h"
11+
12+
13+
@interface XibTestView ()
14+
15+
@property (weak, nonatomic) IBOutlet MYCoreTextLabel *coreLabel;
16+
17+
@end
18+
19+
@implementation XibTestView
20+
21+
+(instancetype)loadXibView
22+
{
23+
return [[NSBundle mainBundle]loadNibNamed:@"XibTestView" owner:nil options:nil].lastObject;
24+
}
25+
- (void)setContent:(NSString *)content
26+
{
27+
_content = content;
28+
self.coreLabel.text = content;
29+
self.coreLabel.keywords = @[@"油伞"];
30+
self.coreLabel.customLinks = @[@"一杯相思",@"恍若昨"];
31+
[self.coreLabel renderForMaxWith:0 maxHeight:0];
32+
}
33+
34+
@end
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<document type="com.apple.InterfaceBuilder3.CocoaTouch.XIB" version="3.0" toolsVersion="14113" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" useTraitCollections="YES" useSafeAreas="YES" colorMatched="YES">
3+
<device id="retina4_7" orientation="portrait">
4+
<adaptation id="fullscreen"/>
5+
</device>
6+
<dependencies>
7+
<deployment identifier="iOS"/>
8+
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="14088"/>
9+
<capability name="Safe area layout guides" minToolsVersion="9.0"/>
10+
<capability name="documents saved in the Xcode 8 format" minToolsVersion="8.0"/>
11+
</dependencies>
12+
<objects>
13+
<placeholder placeholderIdentifier="IBFilesOwner" id="-1" userLabel="File's Owner"/>
14+
<placeholder placeholderIdentifier="IBFirstResponder" id="-2" customClass="UIResponder"/>
15+
<view contentMode="scaleToFill" id="iN0-l3-epB" customClass="XibTestView">
16+
<rect key="frame" x="0.0" y="0.0" width="443" height="264"/>
17+
<autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/>
18+
<subviews>
19+
<view contentMode="scaleToFill" translatesAutoresizingMaskIntoConstraints="NO" id="FoQ-7R-tiF" customClass="MYCoreTextLabel">
20+
<rect key="frame" x="10" y="10" width="423" height="244"/>
21+
<color key="backgroundColor" white="1" alpha="1" colorSpace="custom" customColorSpace="genericGamma22GrayColorSpace"/>
22+
</view>
23+
</subviews>
24+
<color key="backgroundColor" red="0.035829077012509147" green="1" blue="0.90305534852078762" alpha="1" colorSpace="custom" customColorSpace="sRGB"/>
25+
<constraints>
26+
<constraint firstItem="vUN-kp-3ea" firstAttribute="trailing" secondItem="FoQ-7R-tiF" secondAttribute="trailing" constant="10" id="KkR-9z-w9n"/>
27+
<constraint firstItem="FoQ-7R-tiF" firstAttribute="top" secondItem="vUN-kp-3ea" secondAttribute="top" constant="10" id="afG-Oz-EHx"/>
28+
<constraint firstItem="vUN-kp-3ea" firstAttribute="bottom" secondItem="FoQ-7R-tiF" secondAttribute="bottom" constant="10" id="fUb-Jo-PaN"/>
29+
<constraint firstItem="FoQ-7R-tiF" firstAttribute="leading" secondItem="vUN-kp-3ea" secondAttribute="leading" constant="10" id="mfS-vw-RVN"/>
30+
</constraints>
31+
<nil key="simulatedTopBarMetrics"/>
32+
<freeformSimulatedSizeMetrics key="simulatedDestinationMetrics"/>
33+
<viewLayoutGuide key="safeArea" id="vUN-kp-3ea"/>
34+
<connections>
35+
<outlet property="coreLabel" destination="FoQ-7R-tiF" id="Pzt-Qf-PgC"/>
36+
</connections>
37+
<point key="canvasLocation" x="48" y="99"/>
38+
</view>
39+
</objects>
40+
</document>
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
//
2+
// XibViewController.h
3+
// MYCoreText_Example
4+
//
5+
// Created by 孟遥 on 2019/1/30.
6+
// Copyright © 2019年 mengyao. All rights reserved.
7+
//
8+
9+
#import <UIKit/UIKit.h>
10+
11+
@interface XibViewController : UIViewController
12+
13+
@end
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
//
2+
// XibViewController.m
3+
// MYCoreText_Example
4+
//
5+
// Created by 孟遥 on 2019/1/30.
6+
// Copyright © 2019年 mengyao. All rights reserved.
7+
//
8+
9+
#import "XibViewController.h"
10+
#import "XibTestView.h"
11+
12+
13+
@interface XibViewController ()
14+
15+
@property (nonatomic, strong) XibTestView *xView;
16+
17+
@end
18+
19+
@implementation XibViewController
20+
21+
- (void)viewDidLoad {
22+
[super viewDidLoad];
23+
24+
self.title = @"xib使用示例";
25+
self.view.backgroundColor = [UIColor whiteColor];
26+
[self.view addSubview:self.xView];
27+
self.xView.frame = CGRectMake(10,100,[UIScreen mainScreen].bounds.size.width-20,250);
28+
self.xView.content = @"这人间袅袅炊烟,和风花雪月浪漫。痴情人多半贪恋,爱恨情仇的好看。又让你痛不欲生,又让你趁醉装疯。终有天脱胎换骨,直到哭着笑才懂。欲问青天这人生有几何,怕这去日苦多。往事讨一杯相思喝,倘若这回还像曾经执着,心执念你一个,那我可能是多情了。浊酒一杯余生不悲不喜,何惧爱恨别离,一路纵马去斟酌,一曲相思入江水与山河,在油伞下走过,悠然入梦却恍若昨";
29+
}
30+
31+
- (XibTestView *)xView
32+
{
33+
if (!_xView) {
34+
_xView = [XibTestView loadXibView];
35+
}
36+
return _xView;
37+
}
38+
39+
40+
@end

MYCoreText_Example/MYCoreTextLabel/MYCoreTextLabel.m

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,18 @@ - (UITextView *)contentTextView
132132
return _links;
133133
}
134134

135+
- (void)awakeFromNib
136+
{
137+
[super awakeFromNib];
138+
}
139+
140+
- (instancetype)initWithCoder:(NSCoder *)aDecoder
141+
{
142+
if (self = [super initWithCoder:aDecoder]) {
143+
[self addSubview:self.contentTextView];
144+
}
145+
return self;
146+
}
135147

136148
- (instancetype)initWithFrame:(CGRect)frame
137149
{
@@ -146,6 +158,11 @@ - (void)layoutSubviews
146158
{
147159
[super layoutSubviews];
148160
self.contentTextView.frame = self.bounds;
161+
//关键字
162+
if (!_keywordConfig) {
163+
[self keyWord:[[NSMutableAttributedString alloc]initWithAttributedString:self.contentTextView.attributedText]];
164+
_keywordConfig = YES;
165+
}
149166
}
150167

151168

@@ -164,11 +181,6 @@ - (CGSize)renderForMaxWith:(CGFloat)maxWidth maxHeight:(CGFloat)maxHeight
164181
CGSize viewSize = [self.contentTextView sizeThatFits:CGSizeMake(maxWidth,maxHeight)];
165182
self.contentTextView.frame = CGRectMake(0,0,viewSize.width,viewSize.height);
166183

167-
//关键字
168-
if (!_keywordConfig) {
169-
[self keyWord:[[NSMutableAttributedString alloc]initWithAttributedString:self.contentTextView.attributedText]];
170-
_keywordConfig = YES;
171-
}
172184
return viewSize;
173185
}
174186

MYCoreText_Example/ViewController.m

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
#import "LinksViewController.h"
1212
#import "KeywordViewController.h"
1313
#import "DetailViewController.h"
14+
#import "XibViewController.h"
1415

1516
@interface ViewController ()<UITableViewDelegate,UITableViewDataSource>
1617

@@ -24,7 +25,7 @@ @implementation ViewController
2425
- (NSArray *)munesArray
2526
{
2627
if (!_munesArray) {
27-
_munesArray = @[@"只展示图片/表情,其他链接不展示",@"只展示链接,其他不展示",@"只展示关键字,其他不展示",@"最复杂的情况,所有特性都展示"];
28+
_munesArray = @[@"只展示图片/表情,其他链接不展示",@"只展示链接,其他不展示",@"只展示关键字,其他不展示",@"最复杂的情况,所有特性都展示",@"xib情况"];
2829
}
2930
return _munesArray;
3031
}
@@ -93,6 +94,11 @@ - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath
9394
[self.navigationController pushViewController:imageVc animated:YES];
9495
}
9596
break;
97+
case 4:
98+
{
99+
XibViewController *xib = [XibViewController new];
100+
[self.navigationController pushViewController:xib animated:YES];
101+
}
96102
default:
97103
break;
98104
}

0 commit comments

Comments
 (0)