ios 修改html标签样式,ios-slash: 这是一个用来对 NSAttributedString 进行样式化的简单、可扩展的标识语言,跟 HTML 很像,不过每个标签都可以自定义 - 木兰确实...

Slash

8aed8b1efa16c8a4e4886cb6cda3e670.png

Slash is a simple, extensible markup language for styling NSAttributedStrings. The language is similar in appearance to HTML, however the meaning of each tag is user-defined.

Usage

The first paragraph of this readme might be expressed in Slash using the markup:

Slash

Slash is a simple, extensible markup language

that simplifies the creation of NSAttributedStrings. The language is similar in

appearance to HTML, however the meaning of each tag is user-defined.

We can create a new NSAttributedString from this markup with:

NSAttributedString *myAttributedString = [SLSMarkupParser attributedStringWithMarkup:markup error:NULL];

The resulting string will be formatted much as if you'd dropped the equivalent HTML into a browser.

On OS X and iOS 6.0 onwards, Slash provides the following tags:

h1

h2

h3

h4

h5

h6

em

strong

To customize the appearance of these tags, define additional tags, or use a completely different set of tags, pass in a dictionary defining an attributes dictionary for each tag.

NSDictionary *style = @{

@"$default" : @{NSFontAttributeName : [UIFont fontWithName:@"HelveticaNeue" size:14]},

@"strong" : @{NSFontAttributeName : [UIFont fontWithName:@"HelveticaNeue-Bold" size:14]},

@"em" : @{NSFontAttributeName : [UIFont fontWithName:@"HelveticaNeue-Italic" size:14]},

@"h1" : @{NSFontAttributeName : [UIFont fontWithName:@"HelveticaNeue-Medium" size:48]},

@"h2" : @{NSFontAttributeName : [UIFont fontWithName:@"HelveticaNeue-Medium" size:36]},

@"h3" : @{NSFontAttributeName : [UIFont fontWithName:@"HelveticaNeue-Medium" size:32]},

@"h4" : @{NSFontAttributeName : [UIFont fontWithName:@"HelveticaNeue-Medium" size:24]},

@"h5" : @{NSFontAttributeName : [UIFont fontWithName:@"HelveticaNeue-Medium" size:18]},

@"h6" : @{NSFontAttributeName : [UIFont fontWithName:@"HelveticaNeue-Medium" size:16]}

};

NSAttributedString *myAttributedString = [SLSMarkupParser attributedStringWithMarkup:markup style:style error:NULL];

When a piece of text belongs to multiple elements, the attributes applied will be the union of each tag's dictionary, with the innermost elements' attributes taking priority. For a list of attributes supported by the Cocoa/Cocoa Touch text rendering system, see the documentation for iOS or OSX.

If you want to use either the ">" or ").

Note that the linked attributes are only supported on iOS from 6.0 onwards.

Installation

Using CocoaPods

Add pod 'Slash' to your podfile

#import

As an Xcode Subproject

git clone https://github.com/chrisdevereux/Slash.git

Add Slash.xcodeproj as a child project

Add Slash-iOS or Slash-OSX as a project dependency

Link with libSlash-iOS.a or libSlash-OSX.a

Add $(BUILT_PRODUCTS_DIR)/include to your project's header search paths (if it isn't already there)

#import

Requirements

iOS 4.3 or OS X 10.6 (64 bit) upwards.

Attributed string handling is limited prior to iOS 6, and certain features of Slash require iOS 6. Be sure to check the header documentation if you are targeting earlier. You will also need to use a custom view (such as NIAttributedLabel or TTTAttributedLabel) to display the strings, and the format required of the attribute dictionaries in your style will be defined by that view.

Performance

Constructing a 200 character attributed string with 5 tagged sections takes approximately 0.5ms on an iPad 3rd gen when built in release mode. If you are parsing large strings, consider doing so on a background queue.

License

Slash is released under an MIT license.

Contact

Chris Devereux

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值