排版 3.4.1

Typeset 3.4.1

测试已测试
语言语言 Obj-CObjective C
许可证 MIT
发布最新发布2017年5月

Dravenessdraveness维护。



Typeset 3.4.1

  • 作者
  • Draveness


Typeset使得创建NSAttributedString变得简单

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

演示

用法

  • 方法链式调用

    • 所有排版方法都返回一个self对象,以便链式调用。

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

    先调用typeset方法,最后调用string方法,返回一个NSAttributedString

  • UILabelUITextField支持

    • typesetBlock添加到UILabel或UITextField中,您可以直接使用以下方式设置其文本样式
    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);
  • 构建复杂的NSMutableAttributedString

    TSAttributedString(@"Hello".red, @" ", @"World".blue);
  • 匹配字符串部分

    • Typeset提供了一系列方法来匹配字符串的一部分,您可以使用这些方法来选择字符串的一部分,并为其添加属性。
    @"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;

    以下这些行代码都会将@"Hello"中的@"He"变为红色

    匹配方法 说明
    from(NSUInteger) to(NSUInteger)
    location(NSUInteger) length(NSUInteger)
    range(NSRange)
    match(NSString *) 匹配第一个子串
    matchWithOptions(NSString *, NSStringCompareOptions) 以选项匹配第一个子串
    matchAll(NSString) 匹配所有子串
    matchAllWithOptions(NSString *, NSStringCompareOptions) 以选项匹配所有子串
    all 选择所有字符串
    • 按模式匹配
    匹配方法 模式
    matchAllWithPattern(NSString *pattern)
    matchAllWithPatternAndOptions(NSString *pattern, NSRegularExpressionOptions options)
    matchNumbers \d+
    matchLetters [a-zA-Z]+
    matchLanguage(NSString *language) \p{script=%@}
    matchChinese \p{script=@"Han"}
  • 方便的方法

    • 如果您不希望更改字符串的一部分,而只想更改颜色或字体,您可以直接调用这些方法,而无需首先调用typeset
    @"Hello".red
    @"Hello".fontSize(20).red

参考

属性

字典键 Typeset 方法
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)
NSSIDashStyleAttributeName slashThrough(NSUnderlineStyle slashThrough)
NSSIDashColorAttributeName slashThroughColor(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 方法
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)

Podfile

pod "Typeset"

Contribute

如有需要帮助或发现错误,请随时打开问题或拉动请求。

Contact

License

Typeset 可在 MIT 许可下使用。有关更多信息,请参阅 LICENSE 文件。

Todo

  • 文档
  • 更多功能