YNRichLabel 版本 1.0.9

YNRichLabel 版本 1.0.9

Yanni 维护。



YNRichLabel 版本 1.0.9

  • 作者:
  • 码猴

YNRichLabel

XML 富文本标签

如何使用?

  1. 初始化就像使用 UILabel 一样
YNRichLabel *label = [[YNRichLabel alloc] init];
  1. 设置 normalAttributes,这是富文本的常规属性
label.normalAttributes = @{NSFontAttributeName: [UIFont systemFontOfSize:20],
                          NSForegroundColorAttributeName: [UIColor redColor]};
  1. 使用 YNRichTagAdapter 设置

例如 <a href="http://www.google.com">this</a> is content

[label addTagAdapter:({
        YNRichTagAdapter *adapter = [[YNRichTagAdapter alloc] init];
        adapter.tagName = @"a";
        adapter.attributesBlock = ^NSDictionary<NSAttributedStringKey,id> *(YNRichTagModel *model, NSString *text, NSDictionary<NSAttributedStringKey,id> *normalAttributes) {
            NSMutableDictionary *dic = [NSMutableDictionary dictionaryWithDictionary:normalAttributes];
            [dic addEntriesFromDictionary:@{NSForegroundColorAttributeName: [UIColor blueColor]
                                            }];
            return dic;
        };
        adapter.clickBlock = ^(YNRichTagModel *model) {
            NSLog(@"%@",model.attributes);
        };
        adapter;
    })];

例如 <b>this is content</b>

[label addTagAdapter:({
        YNRichTagAdapter *adapter = [[YNRichTagAdapter alloc] init];
        adapter.tagName = @"b";
        adapter.attributesBlock = ^NSDictionary<NSAttributedStringKey,id> *(YNRichTagModel *model, NSString *text, NSDictionary<NSAttributedStringKey,id> *normalAttributes) {
            NSMutableDictionary *dic = [NSMutableDictionary dictionaryWithDictionary:normalAttributes];
            [dic addEntriesFromDictionary:@{NSFontAttributeName: [UIFont boldSystemFontOfSize:24]
                                            }];
            return dic;
        };
        adapter;
    })];

例如 <br>

[label addTagAdapter:({
        YNRichTagAdapter *adapter = [[YNRichTagAdapter alloc] init];
        adapter.tagName = @"br";
        adapter.textBlock = ^NSString *(YNRichTagModel *model) {
            return @"\n";
        };
        adapter;
    })];

例如 <???> 任何您想使用的 HTML 标签~~

  1. 设置文本
label.text = @"这里是<br/>普通内容\n<a href=\"www.google.com\">这里是a的内容</a>\n<b>这里是b的内容</b>\n<a>这里是a<b>嵌套</b>b的内容</a>\n<b>这里是b<a>嵌套</a>a的内容</b>\n这里是普通内容\n<d>这里是d的内容</d>\n这里是普通内容<br/>换行标签<br/><br/><br/>Free for min order<br/> of <b>SGD</b> 20.00 from<a href=\"www.google.com\">Taobao Collection</a>";
  1. 设置完毕,享受乐趣。

演示

您可以在 YNRichLabel.xcworkspace 中运行演示,只需 pod install 它即可

requirements

iOS 7.0