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
。
UILabel
和UITextField
支持
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);
匹配字符串部分
@"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) |
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) |
pod "Typeset"
如有需要帮助或发现错误,请随时打开问题或拉动请求。
Typeset 可在 MIT 许可下使用。有关更多信息,请参阅 LICENSE 文件。