GithubHelp home page GithubHelp logo

yufeiskyer / typeset Goto Github PK

View Code? Open in Web Editor NEW

This project forked from draveness/typeset

0.0 2.0 0.0 11.66 MB

Deal with AttributedString efficiently

License: MIT License

Objective-C 94.50% Ruby 5.50%

typeset's Introduction

[![Version](http://img.shields.io/cocoapods/v/Typeset.svg?style=flat)](http://cocoadocs.org/docsets/Typeset) [![Build Status](https://travis-ci.org/Draveness/Typeset.svg?branch=1.0.0)](https://travis-ci.org/Draveness/Typeset) ![MIT License](https://img.shields.io/github/license/mashape/apistatus.svg) ![Platform](https://img.shields.io/badge/platform-%20iOS%20-lightgrey.svg)


Typeset makes it easy to create NSAttributedString

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

Demo

![](./images/Typeset.gif)

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);
  • 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

typeset's People

Contributors

luosky avatar rpplusplus avatar squalx avatar wenchaod avatar

Watchers

James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.