YNRichLabel
XML rich text label
How to use?
- init just like useing UILabel
YNRichLabel *label = [[YNRichLabel alloc] init];- set normalAttributes, This is the normal attributes for rich text
label.normalAttributes = @{NSFontAttributeName: [UIFont systemFontOfSize:20],
NSForegroundColorAttributeName: [UIColor redColor]};- set up with YNRichTagAdapter
for <a href="www.google.com">this is content</a>
[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;
})];for <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;
})];for <br>
[label addTagAdapter:({
YNRichTagAdapter *adapter = [[YNRichTagAdapter alloc] init];
adapter.tagName = @"br";
adapter.textBlock = ^NSString *(YNRichTagModel *model) {
return @"\n";
};
adapter;
})];for ??? any HTML tag what you want~~~
- set text
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>";- done, have fun.
Demo
you can run demo in YNRichLabel.xcworkspace, just pod install it
Requirements
iOS 7.0