Skip to content

Commit

Permalink
Add lineBreakMode
Browse files Browse the repository at this point in the history
  • Loading branch information
changsanjiang committed Jan 28, 2018
1 parent 39cc62f commit a30ad48
Show file tree
Hide file tree
Showing 8 changed files with 62 additions and 14 deletions.
4 changes: 4 additions & 0 deletions Demo/SJAttributesFactory.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
/* Begin PBXBuildFile section */
66AFD0AF4CC3266F99FFEC7C /* libPods-SJAttributesFactory.a in Frameworks */ = {isa = PBXBuildFile; fileRef = BC64684C1803C091BA7E813A /* libPods-SJAttributesFactory.a */; };
779A59E51FB33AB100F88DEC /* sample2.png in Resources */ = {isa = PBXBuildFile; fileRef = 779A59E41FB33AB100F88DEC /* sample2.png */; };
77A974BC201DD89800C32711 /* SJAttributesStringMaker.podspec in Resources */ = {isa = PBXBuildFile; fileRef = 77A974BB201DD89800C32711 /* SJAttributesStringMaker.podspec */; };
77CD179F1FEA197000B2B54F /* SJAttributeWorker.m in Sources */ = {isa = PBXBuildFile; fileRef = 77CD179B1FEA196F00B2B54F /* SJAttributeWorker.m */; };
77D19C22201C7C6A00BE5412 /* SJAttributesRecorder.m in Sources */ = {isa = PBXBuildFile; fileRef = 77D19C21201C7C6A00BE5412 /* SJAttributesRecorder.m */; };
77D19C39201D74FB00BE5412 /* regular.jpeg in Resources */ = {isa = PBXBuildFile; fileRef = 77D19C38201D74FB00BE5412 /* regular.jpeg */; };
Expand All @@ -33,6 +34,7 @@

/* Begin PBXFileReference section */
779A59E41FB33AB100F88DEC /* sample2.png */ = {isa = PBXFileReference; lastKnownFileType = image.png; path = sample2.png; sourceTree = "<group>"; };
77A974BB201DD89800C32711 /* SJAttributesStringMaker.podspec */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text; name = SJAttributesStringMaker.podspec; path = ../SJAttributesStringMaker.podspec; sourceTree = "<group>"; };
77CD179B1FEA196F00B2B54F /* SJAttributeWorker.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = SJAttributeWorker.m; sourceTree = "<group>"; };
77CD179D1FEA196F00B2B54F /* SJAttributeWorker.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = SJAttributeWorker.h; sourceTree = "<group>"; };
77D19C20201C7C6A00BE5412 /* SJAttributesRecorder.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = SJAttributesRecorder.h; sourceTree = "<group>"; };
Expand Down Expand Up @@ -162,6 +164,7 @@
children = (
77E7E62E1FE794860061439B /* README.md */,
77E7E64C1FE795670061439B /* SJAttributesFactory.podspec */,
77A974BB201DD89800C32711 /* SJAttributesStringMaker.podspec */,
A69E5BF21FB09BDC00EB65B5 /* SJAttributesFactory */,
77D19C44201DA62900BE5412 /* SwiftVersion */,
A69E5BF11FB09BDC00EB65B5 /* Products */,
Expand Down Expand Up @@ -291,6 +294,7 @@
77E7E64D1FE795680061439B /* SJAttributesFactory.podspec in Resources */,
77D19C39201D74FB00BE5412 /* regular.jpeg in Resources */,
A69E5BFD1FB09BDC00EB65B5 /* Assets.xcassets in Resources */,
77A974BC201DD89800C32711 /* SJAttributesStringMaker.podspec in Resources */,
77D19C3B201D7E0800BE5412 /* common.jpeg in Resources */,
77E7E6521FE7A3F60061439B /* action.gif in Resources */,
77E7E62F1FE794860061439B /* README.md in Resources */,
Expand Down
Binary file not shown.
2 changes: 1 addition & 1 deletion SJAttributesFactory.podspec
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ Pod::Spec.new do |s|
#

s.name = "SJAttributesFactory"
s.version = "2.0.1"
s.version = "2.0.2"
s.summary = "富文本编辑工厂."

# This description is used to generate tags and improve search results.
Expand Down
14 changes: 11 additions & 3 deletions SJAttributesFactory/SJAttributeWorker.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,14 @@
// Copyright © 2017年 畅三江. All rights reserved.
//
// Project Address: https://github.com/changsanjiang/SJAttributesFactory
// Email: changsanjiang@gmail.com
// Email: changsanjiange@gmail.com
//

#import <UIKit/UIKit.h>
#import "SJAttributesRecorder.h"

typedef NSString * NSAttributedStringKey NS_EXTENSIBLE_STRING_ENUM;

@class SJAttributeWorker;

NS_ASSUME_NONNULL_BEGIN
Expand All @@ -20,16 +22,18 @@ NS_ASSUME_NONNULL_BEGIN
* make attributed string:
* NSAttributedString *attrStr = sj_makeAttributesString(^(SJAttributeWorker * _Nonnull make) {
*
* // set font , text color.
* make.font([UIFont boldSystemFontOfSize:14]).textColor([UIColor blackColor]);
* // inset text
* make.insert(@"@迷你世界联机 :@江叔 用小淘气耍赖野人#迷你世界#", 0);
*
* make.regexp(@"[@][^@]+\\s", ^(SJAttributesRangeOperator * _Nonnull matched) {
* matched.textColor([UIColor purpleColor]);
* // some code
* });
* make.regexp(@"[#][^#]+#", ^(SJAttributesRangeOperator * _Nonnull matched) {
* matched.textColor([UIColor orangeColor]);
* // some code
Expand Down Expand Up @@ -84,6 +88,8 @@ extern NSMutableAttributedString *sj_makeAttributesString(void(^block)(SJAttribu
**/
@property (nonatomic, copy, readonly) NSAttributedString *(^subAttrStr)(NSRange subRange);

@property (nonatomic, assign, readonly) NSInteger length;

@end


Expand Down Expand Up @@ -229,6 +235,8 @@ extern NSMutableAttributedString *sj_makeAttributesString(void(^block)(SJAttribu
@property (nonatomic, copy, readonly) SJAttributesRangeOperator *(^tailIndent)(double tailIndent);
/// 对齐方式
@property (nonatomic, copy, readonly) SJAttributesRangeOperator *(^alignment)(NSTextAlignment alignment);
/// 截断模式
@property (nonatomic, copy, readonly) SJAttributesRangeOperator *(^lineBreakMode)(NSLineBreakMode lineBreakMode);

@end

Expand Down
12 changes: 11 additions & 1 deletion SJAttributesFactory/SJAttributeWorker.m
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,10 @@ - (NSRange)range {
return NSMakeRange(0, self.attrStr.length);
}

- (NSInteger)length {
return self.attrStr.length;
}

- (void)pauseTask {
[self endTask];
}
Expand Down Expand Up @@ -525,7 +529,13 @@ @implementation SJAttributesRangeOperator(Property)
return self;
};
}

/// 截断模式
- (SJAttributesRangeOperator * _Nonnull (^)(NSLineBreakMode))lineBreakMode {
return ^ SJAttributesRangeOperator *(NSLineBreakMode lineBreakMode) {
self.recorder.lineBreakMode = lineBreakMode;
return self;
};
}
@end

NS_ASSUME_NONNULL_END
1 change: 1 addition & 0 deletions SJAttributesFactory/SJAttributesRecorder.h
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ NS_ASSUME_NONNULL_BEGIN
@property (nonatomic, assign) double headIndent;
@property (nonatomic, assign) double tailIndent;
@property (nonatomic, assign) double alignment;
@property (nonatomic, assign) NSLineBreakMode lineBreakMode;
- (void)addAttributes:(NSMutableAttributedString *)attrStr;
@end

Expand Down
3 changes: 3 additions & 0 deletions SJAttributesFactory/SJAttributesRecorder.m
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,9 @@ - (double)tailIndent {return self.paragraphStyleM.tailIndent;}
- (void)setAlignment:(double)alignment {self.paragraphStyleM.alignment = alignment;}
- (double)alignment {return self.paragraphStyleM.alignment;}

- (void)setLineBreakMode:(NSLineBreakMode)lineBreakMode {self.paragraphStyleM.lineBreakMode = lineBreakMode;}
- (NSLineBreakMode)lineBreakMode {return self.paragraphStyleM.lineBreakMode;}

- (void)addAttributes:(NSMutableAttributedString *)attrStr {
NSRange range = self.range;
if ( range.location == 0 && range.length == 0 ) {
Expand Down
40 changes: 31 additions & 9 deletions SJAttributesStringMaker/SJAttributesStringMaker.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,14 @@

import UIKit

public func sj_makeAttributesString(_ task: ((SJAttributesStringMaker) -> Void)) -> NSAttributedString {

public func sj_makeAttributesString(_ task: ((SJAttributesStringMaker) -> Void)) -> NSMutableAttributedString {
let maker = SJAttributesStringMaker.init()
task(maker)
return maker.endTask()
}


public class SJAttributesRangeOperator {

fileprivate var recorder: SJAttributesRecorder = SJAttributesRecorder()
Expand All @@ -38,12 +40,18 @@ public class SJAttributesStringMaker: SJAttributesRangeOperator {
get { return self.attrStr.length}
}

public var range: NSRange {
get {
return NSRange.init(location: 0, length: self.attrStr.length)
}
}

@discardableResult
private func pauseTask() -> NSAttributedString {
private func pauseTask() -> NSMutableAttributedString {
return self.endTask()
}

public func endTask() -> NSAttributedString {
public func endTask() -> NSMutableAttributedString {
if 0 == self.attrStr.length {
return self.attrStr
}
Expand Down Expand Up @@ -73,12 +81,6 @@ public class SJAttributesStringMaker: SJAttributesRangeOperator {
return self.attrStr.attributedSubstring(from:byRange)
}

fileprivate var range: NSRange {
get {
return NSRange.init(location: 0, length: self.attrStr.length)
}
}

/// 指定范围进行编辑
@discardableResult
func rangeEdit(_ range: NSRange, _ rangeTask: (SJAttributesRangeOperator) -> Void) -> SJAttributesStringMaker {
Expand Down Expand Up @@ -482,6 +484,14 @@ public extension SJAttributesRangeOperator {
recorder.paragraphStyleM.alignment = alignment
return self
}

/// 截断模式
@discardableResult
func lineBreakMode(_ lineBreakMode: NSLineBreakMode) -> SJAttributesRangeOperator {
recorder.paragraphStyleM.lineBreakMode = lineBreakMode
return self
}

}


Expand Down Expand Up @@ -677,6 +687,18 @@ private class SJAttributesRecorder: NSObject, NSCopying {
}
}

var lineBreakMode: NSLineBreakMode? {
set {
var newV = newValue
if nil == newValue { newV = NSLineBreakMode.byWordWrapping}
self.paragraphStyleM.lineBreakMode = newV!
}

get {
return self.paragraphStyleM.lineBreakMode
}
}

func copy(with zone: NSZone? = nil) -> Any {
let new = SJAttributesRecorder.init()
new.range = range
Expand Down

0 comments on commit a30ad48

Please sign in to comment.