├── RZColorful
├── Assets
│ └── .gitkeep
└── Classes
│ ├── UICore
│ ├── UITextField+SelectedRange.h
│ ├── RZTapActionHelper.h
│ ├── UIButton+RZColorful.m
│ ├── LabelFoldHelper.h
│ ├── UIButton+RZColorful.h
│ ├── MarkdownRZ.h
│ ├── UITextField+SelectedRange.m
│ ├── UITextField+RZColorful.h
│ ├── UIView+RZContinueFirstResponder.h
│ ├── UITextView+RZColorful.h
│ ├── UILabel+RZColorful.h
│ ├── UITextField+RZColorful.m
│ ├── RZTapActionHelper.m
│ ├── UITextView+RZColorful.m
│ ├── UILabel+RZColorful.m
│ ├── LabelFoldHelper.m
│ └── UIView+RZContinueFirstResponder.m
│ ├── AttributeCore
│ ├── NSString+RZCode.h
│ ├── RZShadow.h
│ ├── NSString+RZCode.m
│ ├── RZColorfulConferrer.h
│ ├── RZShadow.m
│ ├── RZImageAttachment.h
│ ├── RZParagraphStyle.h
│ ├── NSAttributedString+RZHtml.h
│ ├── NSAttributedString+RZColorful.h
│ ├── RZImageAttachment.m
│ ├── RZParagraphStyle.m
│ ├── RZColorfulAttribute.h
│ ├── RZColorfulConferrer.m
│ ├── RZColorfulAttribute.m
│ ├── NSAttributedString+RZColorful.m
│ └── NSAttributedString+RZHtml.m
│ └── RZColorful.h
├── _Pods.xcodeproj
├── .gitignore
├── .DS_Store
├── line.png
├── image_1.jpg
├── image_2.png
├── Example
├── Tests
│ ├── en.lproj
│ │ └── InfoPlist.strings
│ ├── Tests-Prefix.pch
│ ├── Tests.m
│ └── Tests-Info.plist
├── RZColorful
│ ├── en.lproj
│ │ └── InfoPlist.strings
│ ├── Images.xcassets
│ │ ├── Contents.json
│ │ ├── image.imageset
│ │ │ ├── image@3x.png
│ │ │ └── Contents.json
│ │ └── AppIcon.appiconset
│ │ │ └── Contents.json
│ ├── RZViewController.h
│ ├── RZAppDelegate.h
│ ├── Demo
│ │ ├── RZTextViewVC.h
│ │ ├── RZTestViewController.h
│ │ ├── MDToHtmlViewController.h
│ │ ├── AttributedStringUseDemoVC.h
│ │ ├── RZFoldLabelVC.h
│ │ ├── RZTestViewController.m
│ │ ├── RZTextViewVC.m
│ │ ├── MDToHtmlViewController.m
│ │ ├── AttributedStringUseDemoVC.m
│ │ └── RZFoldLabelVC.m
│ ├── main.m
│ ├── RZColorful-Prefix.pch
│ ├── RZColorful-Info.plist
│ ├── Base.lproj
│ │ ├── LaunchScreen.storyboard
│ │ └── Main.storyboard
│ ├── RZAppDelegate.m
│ └── RZViewController.m
├── RZColorful.xcworkspace
│ ├── xcuserdata
│ │ └── rztime.xcuserdatad
│ │ │ ├── UserInterfaceState.xcuserstate
│ │ │ └── xcdebugger
│ │ │ └── Breakpoints_v2.xcbkptlist
│ ├── contents.xcworkspacedata
│ └── xcshareddata
│ │ └── IDEWorkspaceChecks.plist
├── RZColorful.xcodeproj
│ ├── project.xcworkspace
│ │ └── contents.xcworkspacedata
│ └── xcshareddata
│ │ └── xcschemes
│ │ └── RZColorful-Example.xcscheme
└── Podfile
├── LICENSE
├── RZColorful.podspec
├── UpdateLog.md
└── README.md
/RZColorful/Assets/.gitkeep:
--------------------------------------------------------------------------------
1 |
--------------------------------------------------------------------------------
/_Pods.xcodeproj:
--------------------------------------------------------------------------------
1 | Example/Pods/Pods.xcodeproj
--------------------------------------------------------------------------------
/.gitignore:
--------------------------------------------------------------------------------
1 | Pods/
2 | Podfile.lock
3 | */xcuserdata/
4 |
--------------------------------------------------------------------------------
/.DS_Store:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/rztime/RZColorful/HEAD/.DS_Store
--------------------------------------------------------------------------------
/line.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/rztime/RZColorful/HEAD/line.png
--------------------------------------------------------------------------------
/image_1.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/rztime/RZColorful/HEAD/image_1.jpg
--------------------------------------------------------------------------------
/image_2.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/rztime/RZColorful/HEAD/image_2.png
--------------------------------------------------------------------------------
/Example/Tests/en.lproj/InfoPlist.strings:
--------------------------------------------------------------------------------
1 | /* Localized versions of Info.plist keys */
2 |
3 |
--------------------------------------------------------------------------------
/Example/RZColorful/en.lproj/InfoPlist.strings:
--------------------------------------------------------------------------------
1 | /* Localized versions of Info.plist keys */
2 |
3 |
--------------------------------------------------------------------------------
/Example/RZColorful/Images.xcassets/Contents.json:
--------------------------------------------------------------------------------
1 | {
2 | "info" : {
3 | "author" : "xcode",
4 | "version" : 1
5 | }
6 | }
7 |
--------------------------------------------------------------------------------
/Example/RZColorful/Images.xcassets/image.imageset/image@3x.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/rztime/RZColorful/HEAD/Example/RZColorful/Images.xcassets/image.imageset/image@3x.png
--------------------------------------------------------------------------------
/Example/Tests/Tests-Prefix.pch:
--------------------------------------------------------------------------------
1 | // The contents of this file are implicitly included at the beginning of every test case source file.
2 |
3 | #ifdef __OBJC__
4 |
5 | @import FBSnapshotTestCase;
6 |
7 | #endif
8 |
--------------------------------------------------------------------------------
/Example/RZColorful.xcworkspace/xcuserdata/rztime.xcuserdatad/UserInterfaceState.xcuserstate:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/rztime/RZColorful/HEAD/Example/RZColorful.xcworkspace/xcuserdata/rztime.xcuserdatad/UserInterfaceState.xcuserstate
--------------------------------------------------------------------------------
/Example/RZColorful.xcodeproj/project.xcworkspace/contents.xcworkspacedata:
--------------------------------------------------------------------------------
1 |
2 |
4 |
6 |
7 |
8 |
--------------------------------------------------------------------------------
/Example/RZColorful.xcworkspace/xcuserdata/rztime.xcuserdatad/xcdebugger/Breakpoints_v2.xcbkptlist:
--------------------------------------------------------------------------------
1 |
2 |
6 |
7 |
--------------------------------------------------------------------------------
/Example/Podfile:
--------------------------------------------------------------------------------
1 | use_frameworks!
2 |
3 | platform :ios, '11.0'
4 |
5 | target 'RZColorful_Example' do
6 | pod 'RZColorful', :path => '../'
7 | pod 'Masonry'
8 | target 'RZColorful_Tests' do
9 | inherit! :search_paths
10 |
11 | # pod 'FBSnapshotTestCase'
12 | end
13 | end
14 |
--------------------------------------------------------------------------------
/Example/RZColorful/RZViewController.h:
--------------------------------------------------------------------------------
1 | //
2 | // RZViewController.h
3 | // RZColorful
4 | //
5 | // Created by rztime on 01/14/2022.
6 | // Copyright (c) 2022 rztime. All rights reserved.
7 | //
8 |
9 | @import UIKit;
10 |
11 | @interface RZViewController : UIViewController
12 |
13 | @end
14 |
--------------------------------------------------------------------------------
/Example/RZColorful.xcworkspace/contents.xcworkspacedata:
--------------------------------------------------------------------------------
1 |
2 |
4 |
6 |
7 |
9 |
10 |
11 |
--------------------------------------------------------------------------------
/Example/RZColorful.xcworkspace/xcshareddata/IDEWorkspaceChecks.plist:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | IDEDidComputeMac32BitWarning
6 |
7 |
8 |
9 |
--------------------------------------------------------------------------------
/Example/RZColorful/RZAppDelegate.h:
--------------------------------------------------------------------------------
1 | //
2 | // RZAppDelegate.h
3 | // RZColorful
4 | //
5 | // Created by rztime on 01/14/2022.
6 | // Copyright (c) 2022 rztime. All rights reserved.
7 | //
8 |
9 | @import UIKit;
10 |
11 | @interface RZAppDelegate : UIResponder
12 |
13 | @property (strong, nonatomic) UIWindow *window;
14 |
15 | @end
16 |
--------------------------------------------------------------------------------
/Example/RZColorful/Demo/RZTextViewVC.h:
--------------------------------------------------------------------------------
1 | //
2 | // RZTextViewVC.h
3 | // RZColorful_Example
4 | //
5 | // Created by rztime on 2022/1/26.
6 | // Copyright © 2022 rztime. All rights reserved.
7 | //
8 |
9 | #import
10 |
11 | NS_ASSUME_NONNULL_BEGIN
12 |
13 | @interface RZTextViewVC : UIViewController
14 |
15 | @end
16 |
17 | NS_ASSUME_NONNULL_END
18 |
--------------------------------------------------------------------------------
/Example/RZColorful/Demo/RZTestViewController.h:
--------------------------------------------------------------------------------
1 | //
2 | // RZTestViewController.h
3 | // RZColorful_Example
4 | //
5 | // Created by rztime on 2025/2/12.
6 | // Copyright © 2025 rztime. All rights reserved.
7 | //
8 |
9 | #import
10 |
11 | NS_ASSUME_NONNULL_BEGIN
12 |
13 | @interface RZTestViewController : UIViewController
14 |
15 | @end
16 |
17 | NS_ASSUME_NONNULL_END
18 |
--------------------------------------------------------------------------------
/Example/RZColorful/Demo/MDToHtmlViewController.h:
--------------------------------------------------------------------------------
1 | //
2 | // MDToHtmlViewController.h
3 | // RZColorful_Example
4 | //
5 | // Created by rztime on 2025/3/14.
6 | // Copyright © 2025 rztime. All rights reserved.
7 | //
8 |
9 | #import
10 |
11 | NS_ASSUME_NONNULL_BEGIN
12 |
13 | @interface MDToHtmlViewController : UIViewController
14 |
15 | @end
16 |
17 | NS_ASSUME_NONNULL_END
18 |
--------------------------------------------------------------------------------
/Example/RZColorful/Demo/AttributedStringUseDemoVC.h:
--------------------------------------------------------------------------------
1 | //
2 | // AttributedStringUseDemoVC.h
3 | // RZColorful_Example
4 | //
5 | // Created by rztime on 2022/1/25.
6 | // Copyright © 2022 rztime. All rights reserved.
7 | //
8 |
9 | #import
10 |
11 | NS_ASSUME_NONNULL_BEGIN
12 |
13 | @interface AttributedStringUseDemoVC : UIViewController
14 |
15 | @end
16 |
17 | NS_ASSUME_NONNULL_END
18 |
--------------------------------------------------------------------------------
/Example/RZColorful/main.m:
--------------------------------------------------------------------------------
1 | //
2 | // main.m
3 | // RZColorful
4 | //
5 | // Created by rztime on 01/14/2022.
6 | // Copyright (c) 2022 rztime. All rights reserved.
7 | //
8 |
9 | @import UIKit;
10 | #import "RZAppDelegate.h"
11 |
12 | int main(int argc, char * argv[])
13 | {
14 | @autoreleasepool {
15 | return UIApplicationMain(argc, argv, nil, NSStringFromClass([RZAppDelegate class]));
16 | }
17 | }
18 |
--------------------------------------------------------------------------------
/Example/RZColorful/RZColorful-Prefix.pch:
--------------------------------------------------------------------------------
1 | //
2 | // Prefix header
3 | //
4 | // The contents of this file are implicitly included at the beginning of every source file.
5 | //
6 |
7 | #import
8 |
9 | #ifndef __IPHONE_5_0
10 | #warning "This project uses features only available in iOS SDK 5.0 and later."
11 | #endif
12 |
13 | #ifdef __OBJC__
14 | @import UIKit;
15 | @import Foundation;
16 | #endif
17 |
--------------------------------------------------------------------------------
/RZColorful/Classes/UICore/UITextField+SelectedRange.h:
--------------------------------------------------------------------------------
1 | //
2 | // UITextField+SelectedRange.h
3 | // RZColorfulExample
4 | //
5 | // Created by rztime on 2017/11/16.
6 | // Copyright © 2017年 rztime. All rights reserved.
7 | //
8 |
9 | #import
10 |
11 | @interface UITextField (SelectedRange)
12 |
13 | //获取焦点的位置
14 | -(NSRange)selectedRange;
15 | //设置焦点的位置
16 | -(void)setSelectedRange:(NSRange)range;
17 |
18 | @end
19 |
--------------------------------------------------------------------------------
/Example/RZColorful/Images.xcassets/image.imageset/Contents.json:
--------------------------------------------------------------------------------
1 | {
2 | "images" : [
3 | {
4 | "idiom" : "universal",
5 | "scale" : "1x"
6 | },
7 | {
8 | "idiom" : "universal",
9 | "scale" : "2x"
10 | },
11 | {
12 | "filename" : "image@3x.png",
13 | "idiom" : "universal",
14 | "scale" : "3x"
15 | }
16 | ],
17 | "info" : {
18 | "author" : "xcode",
19 | "version" : 1
20 | }
21 | }
22 |
--------------------------------------------------------------------------------
/RZColorful/Classes/UICore/RZTapActionHelper.h:
--------------------------------------------------------------------------------
1 | //
2 | // RZTapActionHelper.h
3 | // RZColorfulExample
4 | //
5 | // Created by xk_mac_mini on 2019/9/29.
6 | // Copyright © 2019 rztime. All rights reserved.
7 | //
8 |
9 | #import
10 | #import
11 |
12 | NS_ASSUME_NONNULL_BEGIN
13 |
14 | @interface RZTapActionHelper : NSObject
15 |
16 | @property (nonatomic, weak) UITextView *textView;
17 |
18 | @end
19 |
20 | NS_ASSUME_NONNULL_END
21 |
--------------------------------------------------------------------------------
/RZColorful/Classes/AttributeCore/NSString+RZCode.h:
--------------------------------------------------------------------------------
1 | //
2 | // NSString+RZCode.h
3 | // RZColorfulExample
4 | //
5 | // Created by xk_mac_mini on 2019/11/20.
6 | // Copyright © 2019 rztime. All rights reserved.
7 | //
8 | #import
9 |
10 | NS_ASSUME_NONNULL_BEGIN
11 |
12 | @interface NSString (RZCode)
13 | // 对string进行URL encode转码
14 | - (NSString *)rz_encodedString;
15 | // 对string进行URL decode解码
16 | - (NSString *)rz_decodedString;
17 | @end
18 |
19 | NS_ASSUME_NONNULL_END
20 |
--------------------------------------------------------------------------------
/RZColorful/Classes/UICore/UIButton+RZColorful.m:
--------------------------------------------------------------------------------
1 | //
2 | // UIButton+RZColorful.m
3 | // RZColorful
4 | //
5 | // Created by rztime on 2022/1/25.
6 | //
7 |
8 | #import "UIButton+RZColorful.h"
9 |
10 | @implementation UIButton (RZColorful)
11 |
12 | - (void)rz_colorfulConfer:(void (^)(RZColorfulConferrer * _Nonnull))attribute state:(UIControlState)state {
13 | NSAttributedString *string = [NSAttributedString rz_colorfulConfer:attribute];
14 | [self setAttributedTitle:string forState:state];
15 | }
16 |
17 | @end
18 |
--------------------------------------------------------------------------------
/RZColorful/Classes/UICore/LabelFoldHelper.h:
--------------------------------------------------------------------------------
1 | //
2 | // LabelFoldHelper.h
3 | // RZColorful
4 | //
5 | // Created by rztime on 2022/1/25.
6 | //
7 |
8 | #import
9 | #import "UILabel+RZColorful.h"
10 |
11 | NS_ASSUME_NONNULL_BEGIN
12 |
13 | @interface UILabel(DrawText)
14 |
15 | @property (nonatomic, assign) CGRect rz_drawTextRect;
16 | + (void)rz_swizzedSelected;
17 | @end
18 |
19 | @interface LabelFoldHelper : UIView
20 |
21 | - (instancetype)initWithtarget:(UILabel *)target tapAction:(RZLabelTapAction)tapAction;
22 |
23 | @end
24 |
25 | NS_ASSUME_NONNULL_END
26 |
--------------------------------------------------------------------------------
/RZColorful/Classes/UICore/UIButton+RZColorful.h:
--------------------------------------------------------------------------------
1 | //
2 | // UIButton+RZColorful.h
3 | // RZColorful
4 | //
5 | // Created by rztime on 2022/1/25.
6 | //
7 |
8 | #import
9 | #import "RZColorfulConferrer.h"
10 | #import "NSAttributedString+RZColorful.h"
11 | NS_ASSUME_NONNULL_BEGIN
12 |
13 | @interface UIButton (RZColorful)
14 |
15 | /**
16 | 仅UILabel、UITextField、UITextView 使用有效 设置富文本,原内容将清除
17 |
18 | @param attribute block description
19 | */
20 | - (void)rz_colorfulConfer:(void(^)( RZColorfulConferrer * _Nonnull confer))attribute state:(UIControlState)state;
21 |
22 | @end
23 |
24 | NS_ASSUME_NONNULL_END
25 |
--------------------------------------------------------------------------------
/RZColorful/Classes/UICore/MarkdownRZ.h:
--------------------------------------------------------------------------------
1 | //
2 | // MarkdownRZ.h
3 | // RZColorful
4 | //
5 | // Created by rztime on 2025/3/14.
6 | //
7 |
8 | #import
9 |
10 | NS_ASSUME_NONNULL_BEGIN
11 |
12 | @interface MarkdownRZ : NSObject
13 |
14 | /// 请提前调用此方法, 加载markedjs,用于解析markdown
15 | /// - Parameter retry: 如果加载js出错,重试次数(如果加载出错,会有一个默认版本的js来处理)
16 | + (void)asyncInit:(NSInteger)retry;
17 | /// 解析md文档
18 | /// 有一些md文档正常解析中文会乱码,所以这里先将文档base64之后解析来解决中文乱码
19 | /// - Parameter md: markdown文档
20 | /// - Returns: 返回对应的html(解析md后的html,没有任何的样式,需要自行组装head里style:颜色、字号、列表、代码等等)
21 | + (NSString *)parse: (NSString *)md;
22 | @end
23 |
24 | NS_ASSUME_NONNULL_END
25 |
--------------------------------------------------------------------------------
/RZColorful/Classes/RZColorful.h:
--------------------------------------------------------------------------------
1 | //
2 | // RZColorful.h
3 | //
4 | // Created by rztime
5 | // Copyright © 2017年 rztime. All rights reserved.
6 | // 使用方法请参照 RZColorfulConferrer 中的属性
7 |
8 | #import "UITextView+RZColorful.h"
9 | #import "UITextField+RZColorful.h"
10 | #import "UILabel+RZColorful.h"
11 | #import "NSAttributedString+RZColorful.h"
12 | #import "UITextField+SelectedRange.h"
13 | #import "UIView+RZContinueFirstResponder.h"
14 | #import "NSAttributedString+RZHtml.h"
15 | #import "UIButton+RZColorful.h"
16 | #import "NSString+RZCode.h"
17 | #import "RZColorfulAttribute.h"
18 | #import "RZColorfulConferrer.h"
19 | #import "RZImageAttachment.h"
20 | #import "RZParagraphStyle.h"
21 | #import "RZShadow.h"
22 | #import "MarkdownRZ.h"
23 |
--------------------------------------------------------------------------------
/Example/RZColorful/Demo/RZFoldLabelVC.h:
--------------------------------------------------------------------------------
1 | //
2 | // RZFoldLabelVC.h
3 | // RZColorful_Example
4 | //
5 | // Created by rztime on 2022/1/26.
6 | // Copyright © 2022 rztime. All rights reserved.
7 | //
8 |
9 | #import
10 |
11 | NS_ASSUME_NONNULL_BEGIN
12 |
13 | @interface FoldModel : NSObject
14 | @property (nonatomic, strong) NSAttributedString *text;
15 | @property (nonatomic, strong) NSAttributedString *showAll;
16 | @property (nonatomic, strong) NSAttributedString *showFold;
17 | @property (nonatomic, assign) BOOL isFold;
18 | @end
19 |
20 | @interface RZFoldLabelVC : UIViewController
21 |
22 | @end
23 |
24 | @interface RZFoldTestCell : UITableViewCell
25 |
26 | @property (nonatomic, strong) UILabel *titleLabel;
27 |
28 | @end
29 |
30 | NS_ASSUME_NONNULL_END
31 |
--------------------------------------------------------------------------------
/Example/Tests/Tests.m:
--------------------------------------------------------------------------------
1 | //
2 | // RZColorfulTests.m
3 | // RZColorfulTests
4 | //
5 | // Created by rztime on 01/14/2022.
6 | // Copyright (c) 2022 rztime. All rights reserved.
7 | //
8 |
9 | @import XCTest;
10 |
11 | @interface Tests : XCTestCase
12 |
13 | @end
14 |
15 | @implementation Tests
16 |
17 | - (void)setUp
18 | {
19 | [super setUp];
20 | // Put setup code here. This method is called before the invocation of each test method in the class.
21 | }
22 |
23 | - (void)tearDown
24 | {
25 | // Put teardown code here. This method is called after the invocation of each test method in the class.
26 | [super tearDown];
27 | }
28 |
29 | - (void)testExample
30 | {
31 | XCTFail(@"No implementation for \"%s\"", __PRETTY_FUNCTION__);
32 | }
33 |
34 | @end
35 |
36 |
--------------------------------------------------------------------------------
/Example/Tests/Tests-Info.plist:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | CFBundleDevelopmentRegion
6 | en
7 | CFBundleExecutable
8 | ${EXECUTABLE_NAME}
9 | CFBundleIdentifier
10 | $(PRODUCT_BUNDLE_IDENTIFIER)
11 | CFBundleInfoDictionaryVersion
12 | 6.0
13 | CFBundlePackageType
14 | BNDL
15 | CFBundleShortVersionString
16 | 1.0
17 | CFBundleSignature
18 | ????
19 | CFBundleVersion
20 | 1
21 |
22 |
23 |
--------------------------------------------------------------------------------
/LICENSE:
--------------------------------------------------------------------------------
1 | Copyright (c) 2022 rztime
2 |
3 | Permission is hereby granted, free of charge, to any person obtaining a copy
4 | of this software and associated documentation files (the "Software"), to deal
5 | in the Software without restriction, including without limitation the rights
6 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7 | copies of the Software, and to permit persons to whom the Software is
8 | furnished to do so, subject to the following conditions:
9 |
10 | The above copyright notice and this permission notice shall be included in
11 | all copies or substantial portions of the Software.
12 |
13 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
19 | THE SOFTWARE.
20 |
--------------------------------------------------------------------------------
/RZColorful/Classes/AttributeCore/RZShadow.h:
--------------------------------------------------------------------------------
1 | //
2 | // RZShadow.h
3 | // RZColorfulExample
4 | //
5 | // Created by 乄若醉灬 on 2017/7/28.
6 | // Copyright © 2017年 rztime. All rights reserved.
7 | //
8 |
9 | // 阴影的设置集合
10 |
11 | #import
12 | #import
13 | @class RZColorfulAttribute;
14 | @class RZImageAttachment;
15 |
16 | @interface RZShadow : NSObject
17 |
18 | - (instancetype)initWithAttr:(RZColorfulAttribute *)attr;
19 | - (instancetype)initWithAttach:(RZImageAttachment *)attach;
20 |
21 | - (NSShadow *)code;
22 |
23 | /** 连接词 text、htmlText可使用*/
24 | - (RZColorfulAttribute *)and;
25 | /** 连接词 text、htmlText可使用 */
26 | - (RZColorfulAttribute *)with;
27 | /** 连接词 text、htmlText可使用 */
28 | - (RZColorfulAttribute *)end;
29 |
30 | /** 连接词 image、imageByUrl可使用*/
31 | - (RZImageAttachment *)andAttach;
32 | /** 连接词 image、imageByUrl可使用*/
33 | - (RZImageAttachment *)withAttach;
34 | /** 连接词 image、imageByUrl可使用*/
35 | - (RZImageAttachment *)endAttach;
36 |
37 | /** 阴影偏移量 */
38 | - (RZShadow *(^)(CGSize offset))offset;
39 | /** // blur radius of the shadow in default user space units // 值越大,越模糊 */
40 | - (RZShadow *(^)(CGFloat radius))radius;
41 | /** 阴影颜色 */
42 | - (RZShadow *(^)(UIColor *color))color;
43 |
44 | @end
45 |
--------------------------------------------------------------------------------
/RZColorful/Classes/UICore/UITextField+SelectedRange.m:
--------------------------------------------------------------------------------
1 | //
2 | // UITextField+SelectedRange.m
3 | // RZColorfulExample
4 | //
5 | // Created by rztime on 2017/11/16.
6 | // Copyright © 2017年 rztime. All rights reserved.
7 | //
8 |
9 | #import "UITextField+SelectedRange.h"
10 |
11 | @implementation UITextField (SelectedRange)
12 |
13 | -(NSRange)selectedRange {
14 | UITextPosition *beginning = self.beginningOfDocument;
15 | UITextRange *selectedRange = self.selectedTextRange;
16 | UITextPosition *selectionStart = selectedRange.start;
17 | UITextPosition *selectionEnd = selectedRange.end;
18 |
19 | const NSInteger location = [self offsetFromPosition:beginning toPosition:selectionStart];
20 | const NSInteger length = [self offsetFromPosition:selectionStart toPosition:selectionEnd];
21 | return NSMakeRange(location, length);
22 | }
23 |
24 | -(void)setSelectedRange:(NSRange)range {
25 | UITextPosition *beginning = self.beginningOfDocument;
26 | UITextPosition *startPosition = [self positionFromPosition:beginning offset:range.location];
27 | UITextPosition *endPosition = [self positionFromPosition:beginning offset:range.location + range.length];
28 | UITextRange *selectionRange = [self textRangeFromPosition:startPosition toPosition:endPosition];
29 | [self setSelectedTextRange:selectionRange];
30 | }
31 |
32 | @end
33 |
--------------------------------------------------------------------------------
/RZColorful/Classes/UICore/UITextField+RZColorful.h:
--------------------------------------------------------------------------------
1 | //
2 | // UITextField+RZColorful.h
3 | // RZColorfulExample
4 | //
5 | // Created by 若醉 on 2018/7/6.
6 | // Copyright © 2018年 rztime. All rights reserved.
7 | //
8 |
9 | #import
10 | #import "RZColorfulConferrer.h"
11 |
12 | NS_ASSUME_NONNULL_BEGIN
13 |
14 | @interface UITextField (RZColorful)
15 |
16 | /**
17 | 仅UILabel、UITextField、UITextView 使用有效 设置富文本,原内容将清除
18 |
19 | @param attribute block description
20 | */
21 | - (void )rz_colorfulConfer:(void(^)( RZColorfulConferrer * _Nonnull confer))attribute;
22 |
23 | /**
24 | 仅UILabel、UITextField、UITextView 使用有效 追加新的富文本,原内容仍在
25 |
26 | @param attribute block description
27 | */
28 | - (void )rz_colorfulConferAppend:(void (^)(RZColorfulConferrer * _Nonnull confer))attribute RZWARNING("请使用rz_colorfulConferInsetTo: append:方法");
29 |
30 | /**
31 | 仅UILabel、UITextField、UITextView 使用有效 插入文本到头、尾、光标位置
32 |
33 | @param position 插入的位置
34 | @param attribute 新的内容
35 | */
36 | - (void )rz_colorfulConferInsetTo:(rzConferInsertPosition)position append:(void (^)(RZColorfulConferrer * _Nonnull confer))attribute;
37 |
38 |
39 | /**
40 | 仅UILabel、UITextField、UITextView 使用有效 添加到指定位置
41 |
42 | @param location 位置
43 | @param attribute attribute
44 | */
45 | - (void )rz_colorfulConferInsetToLocation:(NSUInteger)location append:(void (^)(RZColorfulConferrer * _Nonnull confer))attribute;
46 |
47 | @end
48 | NS_ASSUME_NONNULL_END
49 |
--------------------------------------------------------------------------------
/RZColorful/Classes/AttributeCore/NSString+RZCode.m:
--------------------------------------------------------------------------------
1 | //
2 | // NSString+RZCode.m
3 | // RZColorfulExample
4 | //
5 | // Created by xk_mac_mini on 2019/11/20.
6 | // Copyright © 2019 rztime. All rights reserved.
7 | //
8 |
9 | #import "NSString+RZCode.h"
10 |
11 | @implementation NSString (RZCode)
12 |
13 | - (NSString *)rz_encodedString {
14 | NSString *encodedString = (NSString *)
15 | CFBridgingRelease(CFURLCreateStringByAddingPercentEscapes(kCFAllocatorDefault,
16 | (CFStringRef)self,
17 | (CFStringRef)@"!$&'()*+,-./:;=?@_~%#[]",
18 | NULL,
19 | kCFStringEncodingUTF8));
20 | return encodedString;
21 | }
22 | -(NSString *)rz_decodedString {
23 | NSString *encodedString = self;
24 | NSString *decodedString = (__bridge_transfer NSString *)CFURLCreateStringByReplacingPercentEscapesUsingEncoding(NULL,
25 | (__bridge CFStringRef)encodedString,
26 | CFSTR(""), CFStringConvertNSStringEncodingToEncoding(NSUTF8StringEncoding));
27 | return decodedString;
28 | }
29 |
30 | @end
31 |
--------------------------------------------------------------------------------
/RZColorful/Classes/UICore/UIView+RZContinueFirstResponder.h:
--------------------------------------------------------------------------------
1 | //
2 | // UIView+RZContinueFirstResponder.h
3 | // TextInputViewFocusDemo
4 | //
5 | // Created by rztime on 2017/11/10.
6 | // Copyright © 2017年 rztime. All rights reserved.
7 | //
8 |
9 |
10 | /******************
11 | 在如tableview中加入了大量的输入框时,可以在[rzContinueFirstResponderAndExecuteCode]方法中写入刷新代码,那么刷新之后输入框焦点不会丢失,且设置UIViewResponderHelper时,可以指定刷新后焦点指定到特定的输入框
12 | *******************/
13 |
14 | #import
15 |
16 | @interface UIViewResponderHelper : NSObject
17 |
18 | /**
19 | 指定下一个响应的tagIdentity
20 | */
21 | @property (nonatomic, copy) NSString *nextFirstResponderTagIdentity;
22 |
23 |
24 | /**
25 | 指定下一个响应的index 如果设置了nextFirstResponderTagIdentity 则优先设置到nextFirstResponderTagIdentity
26 | */
27 | @property (nonatomic, assign) NSInteger nextFirstResponderIndex;
28 |
29 | @end
30 |
31 | #pragma mark - 让view中的第一响应者在刷新界面过后依然保持第一响应
32 | @interface UIView (RZContinueFirstResponder)
33 |
34 | /**
35 | 扩展的身份标志,唯一
36 | 如果需要精准的在刷新界面之后,还原到之前的第一响应,或者指定下一个响应,则需要设置此id
37 | 如果未设置,则默认按照刷新前在view中的排位index,刷新之后依然按照此index还原第一响应
38 | 如果新增了或者删除了,要精确还原到原第一响应,则需要设置此id,除非是加在当前第一响应者的下边
39 | */
40 | @property (nonatomic, copy) NSString *tagIdentity;
41 |
42 |
43 | /**
44 | 刷新界面的扩展方法 将刷新界面的code写在这个code的block中,
45 |
46 | 放心使用,没有循环引用的问题
47 |
48 | 如果确定是scrollview 如tableivew或者collectionview,则最好直接使用[tableview rzContinueFirstResponderAndExecuteCode]来刷新,这样获取界面的可响应者时,效率更高
49 | @param code nextResponder指定下一个需要响应的方法
50 | */
51 | - (void)rzContinueFirstResponderAndExecuteCode:(void (^)(UIViewResponderHelper *nextResponder))code;
52 |
53 | @end
54 |
--------------------------------------------------------------------------------
/RZColorful/Classes/AttributeCore/RZColorfulConferrer.h:
--------------------------------------------------------------------------------
1 | //
2 | // RZAttributedMaker.h
3 | // RZAttributedStringText
4 | //
5 | // Created by 乄若醉灬 on 2017/7/21.
6 | // Copyright © 2017年 rztime. All rights reserved.
7 | //
8 | // 富文本的控制集合
9 |
10 | #import
11 | #import "RZImageAttachment.h"
12 | #import "RZColorfulAttribute.h"
13 |
14 | typedef NS_ENUM(NSInteger, rzConferInsertPosition) {
15 | rzConferInsertPositionDefault = 0, // 默认位置 默认加到光标处
16 | rzConferInsertPositionHeader = 1, // 加到头部
17 | rzConferInsertPositionEnd = 2, // 加到尾部
18 |
19 | rzConferInsertPositionCursor = 4, // 加到光标处
20 | };
21 |
22 | #define RZWARNING(instead) NS_DEPRECATED(2_0, 2_0, 2_0, 2_0, instead)
23 |
24 | NS_ASSUME_NONNULL_BEGIN
25 |
26 | @interface RZColorfulConferrer : NSObject
27 |
28 | - (NSAttributedString *)confer RZWARNING("请勿使用及修改此方法, 具体设置请参照RZColorfulConferrer的其他属性");
29 |
30 | /**
31 | 普通文本内容 要设置其属性,使用"."语法直接连接属性,属性参考RZColorfulAttribute.h
32 | 如: confer.text(@"内容").textColor([UIColor redColor]).font(FONT(16)); 即设置 “内容”显示为红色,16号字体
33 | text.之后的属性,仅对当前text()内容有效
34 | */
35 | - (RZColorfulAttribute *(^)(NSString * _Nullable))text;
36 | /** 添加html格式的内容(网页源码),会通过将源码转换成普通文本内容 */
37 | - (RZColorfulAttribute *(^)(NSString * _Nullable))htmlText;
38 | /** 添加图片,为使图片与前后排文字对齐,可在后设置size */
39 | - (RZImageAttachment *(^)(UIImage * _Nullable))image;
40 | /** 通过url添加图片 */
41 | - (RZImageAttachment *(^)(NSString * _Nullable))imageByUrl;
42 | /** 设置当前控件对象统一段落样式, 这里设置段落之后,请勿使用and等连接词 ,在text中设置了段落样式,则text的段落样式优先级高于统一段落样式 */
43 | - (RZParagraphStyle * _Nullable)paragraphStyle;
44 | /** 设置统一阴影对象 , 请勿使用and等连接词,在text中设置了阴影样式,则text的阴影样式优先级高于统一阴影样式 */
45 | - (RZShadow * _Nullable)shadow;
46 | @end
47 | NS_ASSUME_NONNULL_END
48 |
--------------------------------------------------------------------------------
/Example/RZColorful/RZColorful-Info.plist:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | CFBundleDevelopmentRegion
6 | en
7 | CFBundleDisplayName
8 | ${PRODUCT_NAME}
9 | CFBundleExecutable
10 | ${EXECUTABLE_NAME}
11 | CFBundleIdentifier
12 | $(PRODUCT_BUNDLE_IDENTIFIER)
13 | CFBundleInfoDictionaryVersion
14 | 6.0
15 | CFBundleName
16 | ${PRODUCT_NAME}
17 | CFBundlePackageType
18 | APPL
19 | CFBundleShortVersionString
20 | 1.0
21 | CFBundleSignature
22 | ????
23 | CFBundleVersion
24 | 1.0
25 | LSRequiresIPhoneOS
26 |
27 | UILaunchStoryboardName
28 | LaunchScreen
29 | UIMainStoryboardFile
30 | Main
31 | UIRequiredDeviceCapabilities
32 |
33 | armv7
34 |
35 | UISupportedInterfaceOrientations
36 |
37 | UIInterfaceOrientationPortrait
38 | UIInterfaceOrientationLandscapeLeft
39 | UIInterfaceOrientationLandscapeRight
40 |
41 | UISupportedInterfaceOrientations~ipad
42 |
43 | UIInterfaceOrientationPortrait
44 | UIInterfaceOrientationPortraitUpsideDown
45 | UIInterfaceOrientationLandscapeLeft
46 | UIInterfaceOrientationLandscapeRight
47 |
48 |
49 |
50 |
--------------------------------------------------------------------------------
/RZColorful.podspec:
--------------------------------------------------------------------------------
1 | #
2 | # Be sure to run `pod lib lint RZColorful.podspec' to ensure this is a
3 | # valid spec before submitting.
4 | #
5 | # Any lines starting with a # are optional, but their use is encouraged
6 | # To learn more about a Podspec see https://guides.cocoapods.org/syntax/podspec.html
7 | #
8 |
9 | Pod::Spec.new do |s|
10 | s.name = 'RZColorful'
11 | s.version = '1.7.0'
12 | s.summary = 'NSAttributedString富文本的快捷设置方法集合,以及UITextView、UITextField、UILabel富文本简单优雅的使用, UILabel支持富文本点击,超行折叠、展开功能'
13 |
14 | # This description is used to generate tags and improve search results.
15 | # * Think: What does it do? Why did you write it? What is the focus?
16 | # * Try to keep it short, snappy and to the point.
17 | # * Write the description between the DESC delimiters below.
18 | # * Finally, don't worry about the indent, CocoaPods strips it!
19 |
20 | s.description = <<-DESC
21 | NSAttributedString 富文本方法 以及与html互相转换, UILabel的富文本点击事件
22 | DESC
23 |
24 | s.homepage = 'https://github.com/rztime/RZColorful'
25 | # s.screenshots = 'www.example.com/screenshots_1', 'www.example.com/screenshots_2'
26 | s.license = { :type => 'MIT', :file => 'LICENSE' }
27 | s.author = { 'rztime' => 'rztime@vip.qq.com' }
28 | s.source = { :git => 'https://github.com/rztime/RZColorful.git', :tag => s.version.to_s }
29 | # s.social_media_url = 'https://twitter.com/'
30 |
31 | s.ios.deployment_target = '11.0'
32 |
33 | s.source_files = 'RZColorful/Classes/**/*'
34 |
35 | # s.resource_bundles = {
36 | # 'RZColorful' => ['RZColorful/Assets/*.png']
37 | # }
38 |
39 | # s.public_header_files = 'Pod/Classes/**/*.h'
40 | # s.frameworks = 'UIKit', 'MapKit'
41 | # s.dependency 'AFNetworking', '~> 2.3'
42 | end
43 |
--------------------------------------------------------------------------------
/RZColorful/Classes/UICore/UITextView+RZColorful.h:
--------------------------------------------------------------------------------
1 | //
2 | // UITextView+RZColorful.h
3 | // RZColorfulExample
4 | //
5 | // Created by 若醉 on 2018/7/6.
6 | // Copyright © 2018年 rztime. All rights reserved.
7 | //
8 |
9 | #import
10 | #import "RZColorfulConferrer.h"
11 | #import "NSString+RZCode.h"
12 |
13 | NS_ASSUME_NONNULL_BEGIN
14 |
15 | @interface UITextView (RZColorful)
16 |
17 | /**
18 | 仅UILabel、UITextField、UITextView 使用有效 设置富文本,原内容将清除
19 |
20 | @param attribute block description
21 | */
22 | - (void )rz_colorfulConfer:(void(^)( RZColorfulConferrer * _Nonnull confer))attribute;
23 |
24 | /**
25 | 仅UILabel、UITextField、UITextView 使用有效 追加新的富文本,原内容仍在
26 |
27 | @param attribute block description
28 | */
29 | - (void )rz_colorfulConferAppend:(void (^)(RZColorfulConferrer * _Nonnull confer))attribute RZWARNING("请使用rz_colorfulConferInsetTo: append:方法");
30 |
31 | /**
32 | 仅UILabel、UITextField、UITextView 使用有效 插入文本到头、尾、光标位置
33 |
34 | @param position 插入的位置
35 | @param attribute 新的内容
36 | */
37 | - (void )rz_colorfulConferInsetTo:(rzConferInsertPosition)position append:(void (^)(RZColorfulConferrer * _Nonnull confer))attribute;
38 |
39 |
40 | /**
41 | 仅UILabel、UITextField、UITextView 使用有效 添加到指定位置
42 |
43 | @param location 位置
44 | @param attribute attribute
45 | */
46 | - (void )rz_colorfulConferInsetToLocation:(NSUInteger)location append:(void (^)(RZColorfulConferrer * _Nonnull confer))attribute;
47 |
48 | /**
49 | 点击textView里的可以点击的字符串,
50 | tapObj:为tapId或者NSURL
51 | return: NO: 不跳转到浏览器, YES:跳转到浏览器
52 | 如果tapAction()或url()中包含了有中文,URL将会进行编码,所以请将tapObj,rz_decodedString解码
53 | */
54 | @property (nonatomic, copy) BOOL(^rzDidTapTextView)(id __nullable tapObj);
55 |
56 | /// 获取range所在文本的位置
57 | - (CGRect)rz_rectFor:(NSRange)range;
58 |
59 | /// 获取range所在文本的位置,可能涉及多行 NSValue: CGRect
60 | - (NSArray *)rz_rectFors:(NSRange)range;
61 |
62 | @end
63 | NS_ASSUME_NONNULL_END
64 |
--------------------------------------------------------------------------------
/Example/RZColorful/Base.lproj/LaunchScreen.storyboard:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
22 |
23 |
24 |
25 |
26 |
27 |
28 |
29 |
30 |
31 |
32 |
--------------------------------------------------------------------------------
/Example/RZColorful/Images.xcassets/AppIcon.appiconset/Contents.json:
--------------------------------------------------------------------------------
1 | {
2 | "images" : [
3 | {
4 | "idiom" : "iphone",
5 | "size" : "20x20",
6 | "scale" : "2x"
7 | },
8 | {
9 | "idiom" : "iphone",
10 | "size" : "20x20",
11 | "scale" : "3x"
12 | },
13 | {
14 | "idiom" : "iphone",
15 | "size" : "29x29",
16 | "scale" : "2x"
17 | },
18 | {
19 | "idiom" : "iphone",
20 | "size" : "29x29",
21 | "scale" : "3x"
22 | },
23 | {
24 | "idiom" : "iphone",
25 | "size" : "40x40",
26 | "scale" : "2x"
27 | },
28 | {
29 | "idiom" : "iphone",
30 | "size" : "40x40",
31 | "scale" : "3x"
32 | },
33 | {
34 | "idiom" : "iphone",
35 | "size" : "60x60",
36 | "scale" : "2x"
37 | },
38 | {
39 | "idiom" : "iphone",
40 | "size" : "60x60",
41 | "scale" : "3x"
42 | },
43 | {
44 | "idiom" : "ipad",
45 | "size" : "20x20",
46 | "scale" : "1x"
47 | },
48 | {
49 | "idiom" : "ipad",
50 | "size" : "20x20",
51 | "scale" : "2x"
52 | },
53 | {
54 | "idiom" : "ipad",
55 | "size" : "29x29",
56 | "scale" : "1x"
57 | },
58 | {
59 | "idiom" : "ipad",
60 | "size" : "29x29",
61 | "scale" : "2x"
62 | },
63 | {
64 | "idiom" : "ipad",
65 | "size" : "40x40",
66 | "scale" : "1x"
67 | },
68 | {
69 | "idiom" : "ipad",
70 | "size" : "40x40",
71 | "scale" : "2x"
72 | },
73 | {
74 | "idiom" : "ipad",
75 | "size" : "76x76",
76 | "scale" : "1x"
77 | },
78 | {
79 | "idiom" : "ipad",
80 | "size" : "76x76",
81 | "scale" : "2x"
82 | },
83 | {
84 | "idiom" : "ipad",
85 | "size" : "83.5x83.5",
86 | "scale" : "2x"
87 | },
88 | {
89 | "idiom" : "ios-marketing",
90 | "size" : "1024x1024",
91 | "scale" : "1x"
92 | }
93 | ],
94 | "info" : {
95 | "version" : 1,
96 | "author" : "xcode"
97 | }
98 | }
99 |
--------------------------------------------------------------------------------
/RZColorful/Classes/AttributeCore/RZShadow.m:
--------------------------------------------------------------------------------
1 | //
2 | // RZShadow.m
3 | // RZColorfulExample
4 | //
5 | // Created by 乄若醉灬 on 2017/7/28.
6 | // Copyright © 2017年 rztime. All rights reserved.
7 | //
8 |
9 | #import "RZShadow.h"
10 | #import "RZColorfulAttribute.h"
11 |
12 | @interface RZShadow ()
13 |
14 | @property (nonatomic, strong) NSShadow *shadow;
15 | @property (nonatomic, weak) RZColorfulAttribute *colorfulsAttr;
16 | @property (nonatomic, weak) RZImageAttachment *imageAttach;
17 |
18 | @end
19 |
20 | @implementation RZShadow
21 |
22 | - (instancetype)initWithAttr:(RZColorfulAttribute *)attr {
23 | if (self = [super init]) {
24 | self.colorfulsAttr = attr;
25 | }
26 | return self;
27 | }
28 |
29 | - (instancetype)initWithAttach:(RZImageAttachment *)attach {
30 | if (self = [super init]) {
31 | self.imageAttach = attach;
32 | }
33 | return self;
34 | }
35 |
36 | - (RZColorfulAttribute *)and {
37 | return _colorfulsAttr;
38 | }
39 | - (RZColorfulAttribute *)with {
40 | return _colorfulsAttr;
41 | }
42 | - (RZColorfulAttribute *)end {
43 | return _colorfulsAttr;
44 | }
45 |
46 | - (RZImageAttachment *)andAttach {
47 | return _imageAttach;
48 | }
49 | - (RZImageAttachment *)withAttach {
50 | return _imageAttach;
51 | }
52 | - (RZImageAttachment *)endAttach {
53 | return _imageAttach;
54 | }
55 |
56 | - (NSShadow *)code {
57 | return _shadow;
58 | }
59 |
60 | - (NSShadow *)shadow {
61 | if (!_shadow) {
62 | _shadow = [[NSShadow alloc] init];
63 | }
64 | return _shadow;
65 | }
66 |
67 | - (RZShadow *(^)(CGSize))offset {
68 | return ^id (CGSize offset) {
69 | self.shadow.shadowOffset = offset;
70 | return self;
71 | };
72 | }
73 |
74 | - (RZShadow *(^)(CGFloat))radius {
75 | return ^id (CGFloat radius) {
76 | self.shadow.shadowBlurRadius = radius;
77 | return self;
78 | };
79 | }
80 |
81 | - (RZShadow *(^)(UIColor *))color {
82 | return ^id (UIColor *color) {
83 | self.shadow.shadowColor = color;
84 | return self;
85 | };
86 | }
87 | @end
88 |
--------------------------------------------------------------------------------
/Example/RZColorful/Demo/RZTestViewController.m:
--------------------------------------------------------------------------------
1 | //
2 | // RZTestViewController.m
3 | // RZColorful_Example
4 | //
5 | // Created by rztime on 2025/2/12.
6 | // Copyright © 2025 rztime. All rights reserved.
7 | //
8 |
9 | #import "RZTestViewController.h"
10 | #import "Masonry.h"
11 | #import "RZColorful.h"
12 | @interface RZTestViewController ()
13 | @property(nonatomic, strong) UILabel * label;
14 | @end
15 |
16 | @implementation RZTestViewController
17 |
18 | - (void)viewDidLoad {
19 | [super viewDidLoad];
20 | self.view.backgroundColor = [UIColor whiteColor];
21 | self.label = [[UILabel alloc] init];
22 | self.label.numberOfLines = 0;
23 | [self.view addSubview:self.label];
24 | [self.label mas_makeConstraints:^(MASConstraintMaker *make) {
25 | make.left.right.equalTo(self.view).inset(20);
26 | make.top.equalTo(self.view).inset(100);
27 | }];
28 | NSString * text = @"壹贰叁\n肆伍陆柒捌玖拾壹拾壹壹拾贰壹拾叁壹拾肆壹拾伍壹拾陆壹拾柒壹拾捌壹拾玖1\n贰拾贰拾壹贰拾贰贰拾叁贰拾肆贰拾伍贰拾陆贰拾柒贰拾捌贰拾玖\n叁拾叁拾壹叁拾贰叁拾叁叁拾肆叁拾伍叁拾陆叁拾柒叁拾捌叁拾玖\n肆拾肆拾壹肆拾贰肆拾叁肆拾肆肆拾伍肆拾陆肆拾柒肆拾捌肆拾玖伍拾。";
29 | NSAttributedString * place = [NSAttributedString rz_colorfulConfer:^(RZColorfulConferrer * _Nullable confer) {
30 | confer.text(@"...").font([UIFont systemFontOfSize:30]).textColor([UIColor redColor]).tapActionByLable(@"1");
31 | }];
32 | NSAttributedString * test = [NSAttributedString rz_colorfulConfer:^(RZColorfulConferrer * _Nullable confer) {
33 | confer.text(text).font([UIFont systemFontOfSize:30]).textColor([UIColor blackColor]).tapActionByLable(@"1");
34 | }];
35 | _label.attributedText = [test rz_attributedStringBy:2 maxWidth:self.view.frame.size.width - 40 lineBreakMode:NSLineBreakByClipping placeHolder:place];
36 | }
37 |
38 | /*
39 | #pragma mark - Navigation
40 |
41 | // In a storyboard-based application, you will often want to do a little preparation before navigation
42 | - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
43 | // Get the new view controller using [segue destinationViewController].
44 | // Pass the selected object to the new view controller.
45 | }
46 | */
47 |
48 | @end
49 |
--------------------------------------------------------------------------------
/Example/RZColorful/RZAppDelegate.m:
--------------------------------------------------------------------------------
1 | //
2 | // RZAppDelegate.m
3 | // RZColorful
4 | //
5 | // Created by rztime on 01/14/2022.
6 | // Copyright (c) 2022 rztime. All rights reserved.
7 | //
8 |
9 | #import "RZAppDelegate.h"
10 | #import "RZColorful.h"
11 | @implementation RZAppDelegate
12 |
13 | - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
14 | {
15 | // Override point for customization after application launch.
16 | [MarkdownRZ asyncInit:3];
17 | return YES;
18 | }
19 |
20 | - (void)applicationWillResignActive:(UIApplication *)application
21 | {
22 | // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
23 | // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
24 | }
25 |
26 | - (void)applicationDidEnterBackground:(UIApplication *)application
27 | {
28 | // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
29 | // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
30 | }
31 |
32 | - (void)applicationWillEnterForeground:(UIApplication *)application
33 | {
34 | // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
35 | }
36 |
37 | - (void)applicationDidBecomeActive:(UIApplication *)application
38 | {
39 | // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
40 | }
41 |
42 | - (void)applicationWillTerminate:(UIApplication *)application
43 | {
44 | // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
45 | }
46 |
47 | @end
48 |
--------------------------------------------------------------------------------
/Example/RZColorful/Demo/RZTextViewVC.m:
--------------------------------------------------------------------------------
1 | //
2 | // RZTextViewVC.m
3 | // RZColorful_Example
4 | //
5 | // Created by rztime on 2022/1/26.
6 | // Copyright © 2022 rztime. All rights reserved.
7 | //
8 |
9 | #import "RZTextViewVC.h"
10 | #import
11 | #import
12 |
13 | @interface RZTextViewVC ()
14 |
15 | @property (nonatomic, strong) UITextView *textView;
16 |
17 | @end
18 |
19 | @implementation RZTextViewVC
20 |
21 | - (void)viewDidLoad {
22 | [super viewDidLoad];
23 | self.view.backgroundColor = UIColor.whiteColor;
24 |
25 | _textView = [[UITextView alloc] init];
26 | [self.view addSubview:_textView];
27 | [_textView mas_makeConstraints:^(MASConstraintMaker *make) {
28 | make.top.equalTo(self.view).offset(100);
29 | make.left.equalTo(self.view).offset(20);
30 | make.right.equalTo(self.view).offset((-20));
31 | make.height.equalTo(@300);
32 | }];
33 | _textView.backgroundColor = [UIColor colorWithWhite:0 alpha:0.1];
34 | _textView.linkTextAttributes = @{};
35 |
36 | [_textView rz_colorfulConfer:^(RZColorfulConferrer * _Nonnull confer) {
37 | confer.text(@"").font([UIFont systemFontOfSize:16]);
38 |
39 | confer.text(@"百年来,我们党团结带领人民进行的一切奋斗、一切牺牲、一切创造").font([UIFont systemFontOfSize:16]).textColor([UIColor blackColor]);
40 | confer.text(@"都是在践行为中国人民谋幸福、为中华民族谋复兴的初心使命\n中国共产党的成就和贡献,不仅是历史性的,也是世界性的").font([UIFont systemFontOfSize:16]).textColor([UIColor redColor]);
41 | confer.text(@"\n\n\n");
42 | confer.text(@"添加链接").font([UIFont systemFontOfSize:18]).textColor([UIColor blueColor]).tapAction(@"点击事件");
43 | }];
44 |
45 | _textView.rzDidTapTextView = ^BOOL(id _Nullable tapObj) {
46 | NSString *obj = [((NSURL *)tapObj).absoluteString rz_decodedString];
47 | NSLog(@"%@", obj); // http://www.baidu.com
48 | return false;
49 | };
50 | }
51 |
52 | /*
53 | #pragma mark - Navigation
54 |
55 | // In a storyboard-based application, you will often want to do a little preparation before navigation
56 | - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
57 | // Get the new view controller using [segue destinationViewController].
58 | // Pass the selected object to the new view controller.
59 | }
60 | */
61 |
62 | @end
63 |
--------------------------------------------------------------------------------
/RZColorful/Classes/AttributeCore/RZImageAttachment.h:
--------------------------------------------------------------------------------
1 | //
2 | // RZImageAttachment.h
3 | // RZColorfulExample
4 | //
5 | // Created by 乄若醉灬 on 2017/7/28.
6 | // Copyright © 2017年 rztime. All rights reserved.
7 | //
8 | // 仅添加图片的附件
9 | #import
10 | #import
11 | #import "RZParagraphStyle.h"
12 | #import "RZShadow.h"
13 |
14 | #define RZWARNING(instead) NS_DEPRECATED(2_0, 2_0, 2_0, 2_0, instead)
15 |
16 | typedef NS_ENUM(NSInteger, RZImageAttachmentHorizontalAlign) {
17 | RZHorizontalAlignBottom = 0,
18 | RZHorizontalAlignCenter = 1,
19 | RZHorizontalAlignTop = 2,
20 | };
21 |
22 | @interface RZImageAttachment : NSObject
23 |
24 | @property (nonatomic, assign) CGRect imageBounds RZWARNING("该属性不可使用,请使用bounds(CGRectMake(...))方法");
25 |
26 | @property (nonatomic, assign, readonly) BOOL hadParagraphStyle;
27 | @property (nonatomic, assign, readonly) BOOL hadShadow;
28 |
29 | - (NSDictionary *)code;
30 |
31 | /**
32 | 将bounds数据转换成html格式的语句
33 | */
34 | - (NSString *)toHTMLStringWithImageUrl:(NSString *)imageUrl;
35 |
36 | #pragma mark - 可以设置的属性
37 | /** 段落样式 */
38 | - (RZParagraphStyle *)paragraphStyle;
39 | /** 阴影 */
40 | - (RZShadow *)shadow;
41 | /// 需要添加更多的属性,用这个,如underline等
42 | -(RZColorfulAttribute *)more;
43 | /**
44 | 设置图片bounds时,origin.x 设置无效 size.width height 可以设置与前后排文字字体大小一样,且适当调整origin.y为负值,可以让图片文本对齐
45 | 如果是通过url设置bounds时,宽设置为0:则宽按照高度自动等比显示,高设置为0亦如此
46 | */
47 | - (RZImageAttachment *(^)(CGRect bounds))bounds;
48 | /**
49 | 水平对齐方式
50 | align 上,中,下
51 | refer 对齐的参考系 (前后的字体)
52 | */
53 | - (RZImageAttachment *(^)(CGSize size, RZImageAttachmentHorizontalAlign align, UIFont *font))size;
54 |
55 | /**
56 | y轴偏移量,在某些情况下,在对齐之后需要做上下偏移时,用此方法,请在设置size之后或者bounds之后使用
57 | > 0 时,向下移动位置
58 | < 0 时,向上移动位置
59 | */
60 | - (RZImageAttachment *(^)(CGFloat yOffset))yOffset;
61 |
62 | #pragma mark - 下边附加属性也可以通过.more去调用,
63 | /** 自定义属性和值 */
64 | - (RZColorfulAttribute * (^)(NSAttributedStringKey key, id value))custom;
65 | /** 给图片添加链接,要实现点击,需实现UITextView的delegate的url点击事件 */
66 | - (RZImageAttachment *(^)(NSURL *url))url;
67 | /* 给属性文本添加点击事件 只有UITextView可以用,且UITextView需要实现block didTapTextView */
68 | - (RZImageAttachment * (^)(NSString *tapId))tapAction;
69 | /* 给属性文本添加点击事件 只有UILabel可以用,且UILabel需要实现方法 rz_tapAction */
70 | - (RZImageAttachment * (^)(NSString *tapId))tapActionByLable;
71 |
72 | @end
73 |
--------------------------------------------------------------------------------
/RZColorful/Classes/UICore/UILabel+RZColorful.h:
--------------------------------------------------------------------------------
1 | //
2 | // UILabel+RZColorful.h
3 | // RZColorfulExample
4 | //
5 | // Created by 若醉 on 2018/7/6.
6 | // Copyright © 2018年 rztime. All rights reserved.
7 | //
8 |
9 | #import
10 | #import "RZColorfulConferrer.h"
11 |
12 | NS_ASSUME_NONNULL_BEGIN
13 |
14 | @interface UILabel (RZColorful)
15 |
16 | /**
17 | 仅UILabel、UITextField、UITextView 使用有效 设置富文本,原内容将清除
18 |
19 | @param attribute block description
20 | */
21 | - (void )rz_colorfulConfer:(void(^)( RZColorfulConferrer * _Nonnull confer))attribute;
22 |
23 | /**
24 | 仅UILabel、UITextField、UITextView 使用有效 追加新的富文本,原内容仍在
25 |
26 | @param attribute block description
27 | */
28 | - (void )rz_colorfulConferAppend:(void (^)(RZColorfulConferrer * _Nonnull confer))attribute RZWARNING("请使用rz_colorfulConferInsetTo: append:方法");
29 |
30 | /**
31 | 仅UILabel、UITextField、UITextView 使用有效 插入文本到头、尾、光标位置
32 |
33 | @param position 插入的位置
34 | @param attribute 新的内容
35 | */
36 | - (void )rz_colorfulConferInsetTo:(rzConferInsertPosition)position append:(void (^)(RZColorfulConferrer * _Nonnull confer))attribute;
37 |
38 | /**
39 | 仅UILabel、UITextField、UITextView 使用有效 添加到指定位置
40 |
41 | @param location 位置
42 | @param attribute attribute
43 | */
44 | - (void )rz_colorfulConferInsetToLocation:(NSUInteger)location append:(void (^)(RZColorfulConferrer * _Nonnull confer))attribute;
45 |
46 | /// 设置富文本(超过行数后,自动追加“展开” “收起”)
47 | /// @param attr 原文
48 | /// @param line 最大显示行数
49 | /// @param width 最大显示宽度,这个宽度用于计算文本行
50 | /// @param fold 当前是否折叠
51 | /// @param allText 超过了行数之后,折叠状态显示的文本 如”展开“ 需要给文本设置NSTapActionByLabel属性 (tapActionByLable)
52 | /// @param foldText 超过行数之后,全部展开状态显示的文本 如”收起“ 需要给文本设置NSTapActionByLabel属性 (tapActionByLable)
53 | - (void)rz_setAttributedString:(NSAttributedString * _Nullable)attr maxLine:(NSInteger)line maxWidth:(CGFloat)width isFold:(BOOL)fold showAllText:(NSAttributedString *_Nullable)allText showFoldText:(NSAttributedString *_Nullable)foldText;
54 |
55 | typedef void(^RZLabelTapAction)(UILabel *_Nonnull label, NSString * _Nonnull tapActionId, NSRange range);
56 | /// 给label的富文本添加文本点击事件
57 | /// 注意:NSAttributedString里的每一个字,都需要设置字体,否则无法准确点击位置(内部使用UITextView计算位置,无字体时,默认给的11号字体,会导致位置计算错误)
58 | /// @param tapAction ,tapActionId 即tapActionByLable里的内容
59 | - (void)rz_tapAction:(RZLabelTapAction _Nullable)tapAction;
60 | @end
61 | NS_ASSUME_NONNULL_END
62 |
--------------------------------------------------------------------------------
/UpdateLog.md:
--------------------------------------------------------------------------------
1 | ##### 1.7.0
2 |
3 | 新增了markdown文档转换为html的方法
4 | ```
5 | // 1.初始化(可以提前初始化,用于下载最新的js解析方法)
6 | [MarkdownRZ asyncInit:3];
7 | // 2.解析 解析md后的html,没有任何的样式,需要自行组装head里style:颜色、字号、列表、代码等等)
8 | NSString *html = [MarkdownRZ parse:md];
9 | ```
10 |
11 | ##### 1.6.0
12 |
13 | 新增完善iOS14-18的富文本属性
14 | 优化富文本截断方法的内部实现
15 | 图片使用.more,可以添加文本一样的属性
16 |
17 | ##### 1.5.0
18 |
19 | 新增对富文本进行阶段
20 | ```
21 | NSAttributedString *newAttr = [attr rz_attributedStringBy:3 maxWidth:300 lineBreakMode:NSLineBreakByTruncatingTail placeHolder:[[NSAttributedString alloc] initWithString:@"..."]];
22 | ```
23 |
24 | 新增给paragraphStyle里添加行数限制 如:
25 | ```
26 | /// 只对2中的文本进行行数计算,如果前后有1、3,将不适用
27 | confer.text("1").
28 | confer.text("2")?.paragraphStyle?.numberOfLines(3, maxWidth: width).lineBreakMode(.byTruncatingMiddle)
29 | confer.text("3").
30 | ```
31 |
32 | 新增NSAttributedString里关键字标记
33 |
34 | ##### 1.4.0
35 | * 新增iOS15的方法
36 | * 给UILabel添加超行显示 “折叠” “收起” 功能
37 | * 给UILabel添加富文本可点击事件
38 |
39 | ##### 1.3.1
40 | * 将 RZLineStyle 替换为原系统 NSUnderlineStyle ,设置时,可设置单线双线虚线破折号线等等
41 | * NSAttributedString系统转换为html的方法,有部分标签会无效,所以新增方法,将无效的标签,通过代码注入style,将无效的标签通过css实现了。 看[- (NSString *)rz_codingToCompleteHtmlByWeb](https://github.com/rztime/RZColorful/blob/master/RZColorfulExample/RZColorful/AttributeCore/NSAttributedString+RZHtml.h)
42 |
43 |
44 | ##### 1.3.0
45 | * 新增富文本在drawRect之后,获取每一行的文本和Range
46 | * 一些其他优化
47 |
48 |
49 | ##### 1.2.5更新日志
50 | * 1.修复在html转换成富文本时,当NSLinkAttributeName对应的URL未包含http(https)时,iOS系统自动为其添加的“applewebdata://BF307C6C-5A2C-4F76-B3A0-6FD67E66CF82/”,导致所对应的url或tapId错误的bug
51 | * 2.在UITextView的rzDidTapTextView回调中新增返回值,以控制其是否跳转浏览器
52 |
53 |
54 |
55 | ##### 1.2.4更新日志
56 |
57 | * 1.新增图片的size设置方式 并可参照前后文本字体进行对齐,并在某些情况下做y轴偏移 (对齐中文效果较好,英文会有偏差)
58 | ```
59 | confer.appendImage([UIImage imageNamed:@"image"]).size(CGSizeMake(32, 32), RZHorizontalAlignBottom, rzFont(16)).yOffset(10);
60 | ```
61 |
62 |
63 | * 2.新增图片、文本、html的点击事件 .tapAction(@"1") 只对UITextView有效
64 | ```
65 | confer.appendImage([UIImage imageNamed:@"image"]).size(CGSizeMake(32, 32), RZHorizontalAlignBottom, rzFont(16)).tapAction(@"22222222");
66 |
67 | confer.text(@"点击事件1").font(rzFont(30)).tapAction(@"1").textColor(UIColor.orangeColor);
68 |
69 | confer.htmlText(htmlstring); // 此自动实现tapAction方法
70 | ```
71 | * 需要实现UITextView的rzDidTapTextView方法
72 | ```
73 | textView.rzDidTapTextView = ^(id _Nullable tapObj) {
74 | //tapObj :可以是tapaction里的 tapId, 也可能是htmlstring里的NSURL
75 | NSLog(@"点击了:%@", tapObj);
76 | };
77 | ```
78 |
79 | ```
80 | // 点击事件默认文本会有颜色,去掉可用:
81 | textView.linkTextAttributes = @{};
82 | ```
83 |
--------------------------------------------------------------------------------
/Example/RZColorful/RZViewController.m:
--------------------------------------------------------------------------------
1 | //
2 | // RZViewController.m
3 | // RZColorful
4 | //
5 | // Created by rztime on 01/14/2022.
6 | // Copyright (c) 2022 rztime. All rights reserved.
7 | //
8 |
9 | #import "RZViewController.h"
10 | #import
11 | #import
12 |
13 | @interface RZViewController ()
14 | @property (nonatomic) UITableView *tableView;
15 | @property (nonatomic, strong) NSArray *items;
16 | @end
17 |
18 | @implementation RZViewController
19 |
20 | - (void)viewDidLoad
21 | {
22 | [super viewDidLoad];
23 | // Do any additional setup after loading the view, typically from a nib.
24 | _tableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain];
25 | _tableView.delegate = self;
26 | _tableView.dataSource = self;
27 | [self.view addSubview:_tableView];
28 | [_tableView mas_makeConstraints:^(MASConstraintMaker *make) {
29 | make.edges.equalTo(self.view);
30 | }];
31 | _items = @[@{@"富文本常规使用": @"AttributedStringUseDemoVC"},
32 | @{@"显示折叠展开的label": @"RZFoldLabelVC"},
33 | @{@"UITextView常规使用": @"RZTextViewVC"},
34 | @{@"测试": @"RZTestViewController"},
35 | @{@"markdow to html": @"MDToHtmlViewController"}];
36 | }
37 |
38 | - (void)didReceiveMemoryWarning
39 | {
40 | [super didReceiveMemoryWarning];
41 | // Dispose of any resources that can be recreated.
42 | }
43 |
44 | - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
45 | return self.items.count;
46 | }
47 | - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
48 | NSDictionary *item = self.items[indexPath.row];
49 | UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
50 | if (cell == nil) {
51 | cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"cell"];
52 | }
53 | cell.textLabel.text = item.allKeys.firstObject;
54 | cell.detailTextLabel.text = item.allValues.firstObject;
55 | return cell;
56 | }
57 | - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
58 | return 70;
59 | }
60 | - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
61 | [tableView deselectRowAtIndexPath:indexPath animated:false];
62 | NSDictionary *item = self.items[indexPath.row];
63 | UIViewController *vc = (UIViewController *)[[NSClassFromString(item.allValues.firstObject) alloc] init];
64 | [self.navigationController pushViewController:vc animated:true];
65 | }
66 |
67 | @end
68 |
--------------------------------------------------------------------------------
/RZColorful/Classes/UICore/UITextField+RZColorful.m:
--------------------------------------------------------------------------------
1 | //
2 | // UITextField+RZColorful.m
3 | // RZColorfulExample
4 | //
5 | // Created by 若醉 on 2018/7/6.
6 | // Copyright © 2018年 rztime. All rights reserved.
7 | //
8 |
9 | #import "UITextField+RZColorful.h"
10 | #import "NSAttributedString+RZColorful.h"
11 | #import "UITextField+SelectedRange.h"
12 |
13 | @implementation UITextField (RZColorful)
14 |
15 | /**
16 | 设置富文本,原内容将清除 仅UILabel、UITextField、UITextView 有效
17 |
18 | @param attribute <#block description#>
19 | */
20 | - (void )rz_colorfulConfer:(void(^)(RZColorfulConferrer *confer))attribute {
21 | [self rzSetAttributedText:nil];
22 | [self rz_colorfulConferInsetToLocation:0 append:attribute];
23 | }
24 |
25 | - (void )rz_colorfulConferAppend:(void (^)(RZColorfulConferrer *confer))attribute {
26 | [self rz_colorfulConferInsetTo:rzConferInsertPositionEnd append:attribute];
27 | }
28 |
29 | /**
30 | 插入文本
31 |
32 | @param position 插入的位置
33 | @param attribute 新的内容
34 | */
35 | - (void )rz_colorfulConferInsetTo:(rzConferInsertPosition)position append:(void (^)(RZColorfulConferrer * _Nonnull confer))attribute {
36 | NSUInteger location;
37 | switch (position) {
38 | case rzConferInsertPositionDefault:
39 | case rzConferInsertPositionCursor: { // 默认位置 光标处
40 | location = [self getCursorLocation];
41 | break;
42 | }
43 | case rzConferInsertPositionHeader: { // 头
44 | location = 0;
45 | break;
46 | }
47 | case rzConferInsertPositionEnd: { // 尾
48 | location = [self getEndLocation];
49 | break;
50 | }
51 | default:
52 | location = [self getEndLocation];
53 | break;
54 | }
55 | [self rz_colorfulConferInsetToLocation:location append:attribute];
56 | }
57 |
58 | /**
59 | 添加到指定位置
60 |
61 | @param location <#location description#>
62 | @param attribute <#attribute description#>
63 | */
64 | - (void )rz_colorfulConferInsetToLocation:(NSUInteger)location append:(void (^)(RZColorfulConferrer * _Nonnull))attribute {
65 | if(!attribute) {
66 | return ;
67 | }
68 | NSUInteger loc = location;
69 | NSAttributedString *conferrerColorful = [NSAttributedString rz_colorfulConfer:attribute];
70 | if (conferrerColorful.string.length == 0) {
71 | return ;
72 | }
73 | if (loc > [self getEndLocation]) {
74 | loc = [self getEndLocation];
75 | }
76 | NSMutableAttributedString *attr = [[NSMutableAttributedString alloc] initWithAttributedString:[self rzAttributedString]];
77 | [attr insertAttributedString:conferrerColorful atIndex:loc];
78 | [self rzSetAttributedText:attr];
79 | }
80 | // 文本框的内容
81 | - (NSAttributedString *)rzAttributedString {
82 | return self.attributedText;
83 | }
84 | // 设置文本框的内容
85 | - (void)rzSetAttributedText:(NSAttributedString *)attributedString {
86 | self.attributedText = attributedString;
87 | }
88 | // 尾部的位置
89 | - (NSUInteger)getEndLocation {
90 | return self.attributedText.string.length;
91 | }
92 | // 光标的位置
93 | - (NSUInteger)getCursorLocation {
94 | return self.selectedRange.location;
95 | }
96 |
97 | @end
98 |
--------------------------------------------------------------------------------
/Example/RZColorful/Base.lproj/Main.storyboard:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
22 |
23 |
24 |
25 |
26 |
27 |
28 |
29 |
30 |
31 |
32 |
33 |
34 |
35 |
36 |
37 |
38 |
39 |
40 |
41 |
42 |
43 |
44 |
45 |
46 |
47 |
48 |
49 |
--------------------------------------------------------------------------------
/RZColorful/Classes/AttributeCore/RZParagraphStyle.h:
--------------------------------------------------------------------------------
1 | //
2 | // RZParagraphStyle.h
3 | // RZColorfulExample
4 | //
5 | // Created by 乄若醉灬 on 2017/7/31.
6 | // Copyright © 2017年 rztime. All rights reserved.
7 | //
8 |
9 | // 段落属性的设置集合
10 |
11 |
12 | #import
13 | #import
14 | @class RZColorfulAttribute;
15 | @class RZImageAttachment;
16 |
17 | #define RZWARNING(instead) NS_DEPRECATED(2_0, 2_0, 2_0, 2_0, instead)
18 |
19 | @interface RZMutableParagraphStyle : NSMutableParagraphStyle
20 | /// 行数
21 | @property (nonatomic, assign) NSUInteger numberOfLines;
22 | /// 计算时,绘制的文本的最大宽度
23 | @property (nonatomic, assign) CGFloat textDrawMaxWidth;
24 | /// 截断时,根据LineBreakMode方式,添加的文本
25 | @property (nonatomic, strong) NSAttributedString * _Nullable truncateText;
26 |
27 | + (RZMutableParagraphStyle *_Nonnull)copyWith:(RZMutableParagraphStyle *_Nonnull)para;
28 |
29 | @end
30 |
31 | @interface RZParagraphStyle : NSObject
32 |
33 | - (instancetype _Nonnull)initWithAttr:(RZColorfulAttribute *_Nonnull)attr;
34 | - (instancetype _Nonnull)initWithAttach:(RZImageAttachment *_Nonnull)attach;
35 |
36 | - (RZMutableParagraphStyle *_Nonnull)code;
37 |
38 | /** 连接词 text、htmlText可使用*/
39 | - (RZColorfulAttribute *_Nonnull)and;
40 | /** 连接词 text、htmlText可使用 */
41 | - (RZColorfulAttribute *_Nonnull)with;
42 | /** 连接词 text、htmlText可使用 */
43 | - (RZColorfulAttribute *_Nonnull)end;
44 |
45 | /** 连接词 image、imageByUrl可使用*/
46 | - (RZImageAttachment *_Nonnull)andAttach;
47 | /** 连接词 image、imageByUrl可使用*/
48 | - (RZImageAttachment *_Nonnull)withAttach;
49 | /** 连接词 image、imageByUrl可使用*/
50 | - (RZImageAttachment *_Nonnull)endAttach;
51 | /** 段落行距 */
52 | - (RZParagraphStyle *_Nonnull(^_Nonnull)(CGFloat))lineSpacing;
53 | /** 段落后面的间距 */
54 | - (RZParagraphStyle *_Nonnull(^_Nonnull)(CGFloat))paragraphSpacing;
55 | /** 文本对齐方式 */
56 | - (RZParagraphStyle *_Nonnull(^_Nonnull)(NSTextAlignment))alignment;
57 | /** 首行文本缩进 */
58 | - (RZParagraphStyle *_Nonnull(^_Nonnull)(CGFloat))firstLineHeadIndent;
59 | /** 非首行文本缩进 */
60 | - (RZParagraphStyle *_Nonnull(^_Nonnull)(CGFloat))headIndent;
61 | /** 文本缩进 */
62 | - (RZParagraphStyle *_Nonnull(^_Nonnull)(CGFloat))tailIndent;
63 | /** 文本折行方式 */
64 | - (RZParagraphStyle *_Nonnull(^_Nonnull)(NSLineBreakMode))lineBreakMode;
65 | /** 文本最小行距 */
66 | - (RZParagraphStyle *_Nonnull(^_Nonnull)(CGFloat))minimumLineHeight;
67 | /** 文本最大行距 */
68 | - (RZParagraphStyle *_Nonnull(^_Nonnull)(CGFloat))maximumLineHeight;
69 | /** 文本写入方式,即显示方式,从左至右,或从右到左 */
70 | - (RZParagraphStyle *_Nonnull(^_Nonnull)(NSWritingDirection))baseWritingDirection;
71 | /** 设置文本行间距是默认间距的倍数 */
72 | - (RZParagraphStyle *_Nonnull(^_Nonnull)(CGFloat))lineHeightMultiple;
73 | /** 段与段之间的间距 */
74 | - (RZParagraphStyle *_Nonnull(^_Nonnull)(CGFloat))paragraphSpacingBefore;
75 | /** 设置每行的最后单词是否截断,在0.0-1.0之间,默认为0.0,越接近1.0单词被截断的可能性越大, */
76 | - (RZParagraphStyle *_Nonnull(^_Nonnull)(float))hyphenationFactor;
77 | /** 未知 */
78 | - (RZParagraphStyle *_Nonnull(^_Nonnull)(CGFloat))defaultTabInterval NS_AVAILABLE(10_0, 7_0);
79 | /** 未知 */
80 | - (RZParagraphStyle *_Nonnull(^_Nonnull)(BOOL))allowsDefaultTighteningForTruncation NS_AVAILABLE(10_11, 9_0);
81 | /** 给段落添加行数限制,搭配NSLineBreakModel,将在段落里添加...占位 */
82 | - (RZParagraphStyle *_Nonnull(^_Nonnull)(NSInteger numberOfLines, CGFloat maxWidth, NSAttributedString *_Nullable truncateText))numberOfLines;
83 | @end
84 |
--------------------------------------------------------------------------------
/RZColorful/Classes/AttributeCore/NSAttributedString+RZHtml.h:
--------------------------------------------------------------------------------
1 | //
2 | // NSAttributedString+RZHtml.h
3 | // RZColorfulExample
4 | //
5 | // Created by rztime on 2020/8/12.
6 | // Copyright © 2020 rztime. All rights reserved.
7 | //
8 |
9 | #import
10 | #import "NSAttributedString+RZColorful.h"
11 |
12 | NS_ASSUME_NONNULL_BEGIN
13 |
14 | @interface NSAttributedString (RZHtml)
15 | // 将html转换成 NSAttributedString
16 | + (NSAttributedString *)htmlString:(NSString *)html;
17 | /**
18 | 将富文本完整的code成html标签,(此方法如果富文本中有图片,则图片将被丢失) 有图片时,请用[rz_codingToHtmlWithImagesURLSIfHad]方法 (备注 使用系统方法,某些标签将不能转换)
19 | @return HTML标签语言
20 | */
21 | - (NSString *)rz_codingToCompleteHtml;
22 |
23 | /// 将富文本转换为web可用的html
24 | /// 备注 :1将系统不能转换的标签,通过外部方法写入style,转换成web可用的html
25 | /// 2 通过htmlString还原成NSAttributedString,部分属性也有可能丢失 如下划线删除线的颜色(转换时,系统取得是\
89 | \
90 | \
91 | %@\
92 | \
93 |