• Stars
    star
    450
  • Rank 97,143 (Top 2 %)
  • Language
    Objective-C
  • License
    MIT License
  • Created over 9 years ago
  • Updated over 4 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Deal with AttributedString efficiently

Version Build Status MIT License Platform


Typeset makes it easy to create NSAttributedString

@"Hello typeset".typeset
   .match(@"Hello").fontSize(40)
   .match(@"type").purple
   .match(@"set").blue
   .string;

Demo

Usage

  • Method chaining

    • All the method for typeset returns a self object to chaining itself.

       @"Hello typeset".typeset
          .match(@"Hello").fontSize(40)
          .match(@"type").purple
          .match(@"set").blue
          .string;
      

    call typeset method first and call string at last returns a NSAttributedString.

  • UILabel and UITextField support

    • Add typesetBlock to UILabel or UITextField, and you can directly set it's text style with:
     label.typesetBlock = TSBlock(fontSize(40)
                            .match(@"type").purple
                            .match(@"set").blue);
     label.text = @"Hello typeset, hello.";
    
     // If you type in this text field, it will color all the `1` in text field to red
     textField.typesetBlock = TSBlock(match(@"1").red);
  • Construct complicated NSMutableAttributedString

    TSAttributedString(@"Hello".red, @" ", @"World".blue);
  • Match part of string

    • Typeset providing a series of method to match part of your string, you can use these method to select part of your string, and add attribute to it.
     @"Hello".typeset.from(0).to(2).red.string;
     @"Hello".typeset.location(0).length(2).red.string;
     @"Hello".typeset.range(NSMakeRange(0,2)).red.string;
     @"Hello".typeset.match(@"He").red.string;

    These lines of code all make @"He" of @"Hello" to red

    Match Method Explain
    from(NSUInteger) to(NSUInteger)
    location(NSUInteger) length(NSUInteger)
    range(NSRange)
    match(NSString *) match the first substring
    matchWithOptions(NSString *, NSStringCompareOptions) match the first substring with options
    matchAll(NSString) match all the substring
    matchAllWithOptions(NSString *, NSStringCompareOptions) match all the substring with options
    all select all the string
    • Match with pattern
    Match Method Pattern
    matchAllWithPattern(NSString *pattern)
    matchAllWithPatternAndOptions(NSString *pattern, NSRegularExpressionOptions options)
    matchNumbers \d+
    matchLetters [a-zA-Z]+
    matchLanguage(NSString *language) \p{script=%@}
    matchChinese \p{script=@"Han"}
  • Convinient method

    • If you don't want to change some part of the string, and only want to change the color or the font, you call call these methods directly without calling typeset first
     @"Hello".red
     @"Hello".fontSize(20).red

References

Attributes

Dictionary Key Typeset Method
NSFontAttributeName font(NSString fontName, CGFloat size)
fontSize(CGFloat size)
fontName(NSString name)
regular light italic thin bold
NSForegroundColorAttributeName color(UIColor color)
hexColor(CGFloat hexColor)
black darkGray lightGray white gray red green blue cyan yellow magenta orange purple brown clear
NSKernAttributeName kern(CGFloat kern)
NSUnderlineStyleAttributeName underline(NSUnderlineStyle underline)
NSUnderlineColorAttributeName underlineColor(UIColor *underlineColor)
NSBaselineOffsetAttributeName baseline(CGFloat baseline)
NSStrikethroughStyleAttributeName strikeThrough(NSUnderlineStyle strikeThrough)
NSStrikethroughColorAttributeName strikeThroughColor(UIColor *underlineColor)
NSLinkAttributeName link(NSString *url)
NSLigatureAttributeName ligature(NSUInteger ligature)
NSStrokeColorAttributeName strokeColor(UIColor *strokeColor)
NSStrokeWidthAttributeName strokeWidth(CGFloat strokeWidth)
NSShadowAttributeName shadow(NSShadow *shadow)
NSTextEffectAttributeName textEffect(NSString *textEffect)
NSObliquenessAttributeName obliqueness(CGFloat obliqueness)
NSExpansionAttributeName expansion(CGFloat expansion)

NSParagraphStyle

Typeset Method
lineBreakMode(NSLineBreakMode lineBreakMode)
alignment(NSTextAlignment textAlignment)
lineSpacing(CGFloat lineSpacing)
paragraphSpacing(CGFloat paragraphSpacing)
firstLineHeadIndent(CGFloat firstLineHeadIndent)
headIndent(CGFloat headIndent)
tailIndent(CGFloat tailIndent)
minimumLineHeight(CGFloat minimumLineHeight)
maximumLineHeight(CGFloat maximumLineHeight)
lineHeightMultiple(CGFloat lineHeightMultiple)
paragraphSpacingBefore(CGFloat paragraphSpacingBefore)
hyphenationFactor(CGFloat hyphenationFactor)
defaultTabInterval(CGFloat defaultTabInterval)
baseWritingDirection(NSWritingDirection baseWritingDirection)
allowsDefaultTighteningForTruncation(BOOL allowsDefaultTighteningForTruncation)

Installation

CocoaPods

CocoaPods is a dependency manager for Objective-C, which automates and simplifies the process of using 3rd-party libraries like Typeset in your projects. See the Get Started section for more details.

Podfile

pod "Typeset"

Contribute

Feel free to open an issue or pull request, if you need help or there is a bug.

Contact

License

Typeset is available under the MIT license. See the LICENSE file for more info.

Todo

  • Documentation
  • More features

More Repositories

1

analyze

Draven's Blog
Objective-C
8,101
star
2

DKNightVersion

Manage Colors, Integrate Night/Multiple Themes. (Unmaintained)
Objective-C
3,556
star
3

DKChainableAnimationKit

A DSL to make animation easy on iOS with Swift.
Swift
1,894
star
4

NightNight

Elegant way to integrate night mode to swift projects
Swift
776
star
5

RbSwift

Extensions for Swift with hundreds of handy methods.
Swift
320
star
6

go-internal

《Go 语言设计与实现》示例代码
Go
268
star
7

Ouroboros

🐍 ObjectiveC library for magical scroll interactions.
Objective-C
149
star
8

blog-comments

面向信仰编程
140
star
9

ATProperty

☀️ Create @property with shortcut
Objective-C
135
star
10

proxier

A better approach to expose application in Kubernetes.
Go
109
star
11

linux-archive

Linux archive for studying the process scheduler. 调度系统设计精要 http://draveness.me/system-design-scheduler
C
88
star
12

JSONCop

A better way to deal with JSON parsing in Swift.
Ruby
30
star
13

Mineral

A library used to separate concerns from the original UIKit framework.
Swift
29
star
14

ChainableKit

Chainable UIKit in Objective-C
Objective-C
27
star
15

oceanbook

A Modern Matching Engine without documentation YET [alpha]
Go
25
star
16

Crotalus

Elegant approach to create AttributedString in swift
Swift
19
star
17

SpaceShuttle

🚀 A library manages Inter-View value passing
Objective-C
13
star
18

go-utils

Go
11
star
19

OKObserver

A lightweight wrapper for KVO, bind view and model easily
Objective-C
11
star
20

draveness

10
star
21

git-ignore

Rust
3
star
22

tmpwatch-controller

Dockerfile
2
star
23

gitbook-plugin-gitalk

JavaScript
2
star
24

Daytabase

An key/value store database built atop sqlite
Swift
1
star