HTMLAttributedString 0.0.1

HTMLAttributedString 0.0.1

测试已测试
语言语言 Obj-CObjective C
许可证 自定义
发布最新版本2014 年 12 月

Yichi Zhang 维护。



  • 作者:
  • Mohammed Islam

不再需要与 NSRanges 打交道;用快速属性标记您的字符串。

想象一下,现在您可以利用推送通知通过 加粗斜体 文字在有效载荷中做些什么!或者想象一下,您如何在您的 UILabel 上创建格式化的描述和说明!

HTMLAttributedString Example

HTMLAttributedString 使用了 iOS 7.0 的 initWithData:options:documentAttributes:error: 方法,为您将 HTML 和 CSS 的功能带到了日常 iOS 开发需求中。

如果您想在 iOS 应用的任何标签和显示的文本上快速格式化,或者希望将格式通过互联网传递到您的应用程序中,可以使用 HTMLAttributedString 类来完成这项工作。

设置

用以下方式初始化

HTMLAttributedString *string  = [[HTMLAttributedString alloc] initWithHtml:@"<h1>Header</h1> Test string." andBodyFont:[UIFont fontWithName:@"Helvetica" size:18.0]];
NSAttributedString *attributedString = string.attributedString;

但您也可以通过以下方式快速得到一个字符串

NSAttributedText *attributedString = [HTMLAttributedString attributedStringWithHtml:@"<h2>Quick Header</h2> Isn't that simple?" andBodyFont:[UIFont fontWithName:@"Helvetica" size:12.0]];

控件

您可以使用以下方式添加 CSS 规则、删除它们或清除它们

- (void)addCssAttribute:(NSString *)cssAttribute;
- (void)removeCssAttribute:(NSString *)cssAttribute;
- (void)clearCssAttributes;

示例

HTMLAttributedString *string  = [[HTMLAttributedString alloc] initWithHtml:@"<h1>Header</h1> Test string." withFont:[UIFont fontWithName:@"Helvetica" size:18.0]];
[string addCssAttribute:@"h1{font-family : 'Avenir-Roman'; font-size : 24px;}"];

并且您可以用属性改变 body 字体

@property (nonatomic, strong) UIFont *bodyFont;

就这样!享受吧!