自定义IOS简单富文本组件(显示html标签内容)
<template>
<div class="wrapper">
<image :src="logo" class="logo" />
<!-- <router-view/> -->
<div class="panel">
<textcomponent style="justify-content: center;" ref="richtext" :text="t"></textcomponent>
</div>
<text class="greeting">The environment is ready,lll拉萨京津冀的方式的经适房的副驾驶的福建省昂科拉计算机设计师的方式!</text>
</div>
</template>
<script>
export default {
name: 'App',
data () {
return {
logo: 'https://gw.alicdn.com/tfs/TB1yopEdgoQMeJjy1XaXXcSsFXa-640-302.png',
t: '<p style="font-family:verdana;color:red">这里要厕所个富文本<p style="font-family:times;color:green;font-size:30px">This text is in Times and green<a style="font-size:20px">This text is 50 pixels high</a><img class="image" alt="cover" src="https://gd2.alicdn.com/bao/uploaded/i2/T14H1LFwBcXXXXXXXX_!!0-item_pic.jpg">123456</img><p style="font-family:verdana;color:purple">我们来不来,整一个吧<p style="font-family:times;color:green;font-size:30px">那算是东风路按劳动法就开始灾难性的服你</p>is in Verdana and red</p>'
}
},
mounted: function () {
this.aaa()
},
methods: {
aaa: function () {
}
}
}
组件在iOS工程中创建,继承自WXComponent

.m文件
#import "RFRichTextComponent.h"
#import
@implementationRFRichTextComponent{
NSString*_text;
CGFloat_fWidth;
CGFloat_fHeight;
}
- (id)initWithRef:(NSString*)ref type:(NSString*)type styles:(NSDictionary*)styles attributes:(NSDictionary*)attributes events:(NSArray*)events weexInstance:(WXSDKInstance*)weexInstance
{
if(self= [superinitWithRef:reftype:typestyles:stylesattributes:attributesevents:eventsweexInstance:weexInstance]) {
[selffillAttributes:attributes];
[selffillStyles:styles];
}
return self;
}
- (UIView*)loadView
{
UIView*lbl = [UIViewnew];
lbl.userInteractionEnabled = YES;
returnlbl;
}
- (void)viewDidLoad
{
[selfrepaint];
}
- (void)fillStyles:(NSDictionary*)styles{
idw = styles[@"width"];
if(w) {
_fWidth= [wfloatValue];
}
idh = styles[@"height"];
if(h) {
_fHeight= [hfloatValue];
}
}
- (void)fillAttributes:(NSDictionary*)attributes
{
idtext = attributes[@"text"];
if(text) {
_text= [WXConvertNSString:text];
[self setNeedsLayout];
}
}
- (void)repaint{
for(UIView *sub in self.view.subviews){
[subremoveFromSuperview];
}
UILabel *label = [[UILabel alloc] initWithFrame:self.view.bounds]; //CGRectMake(0, 0, _fWidth, _fHeight)
label.numberOfLines=0;
[self.viewaddSubview:label];
// NSString *content = @"
这里要厕所个富文本This text is in Times and greenis in Verdana and red
This text is 50 pixels high
";NSData *data = [_text dataUsingEncoding:NSUnicodeStringEncoding];
NSDictionary *options = @{NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType};
NSAttributedString *html = [[NSAttributedString alloc]initWithData:data
options:options
documentAttributes:nil
error:nil];
label.attributedText= html;
NSLog(@"====前:%.1f,%.1f,%.1f,%.1f",label.frame.origin.x,label.frame.origin.y, label.frame.size.width, label.frame.size.height);
[labelsizeToFit];
NSLog(@"====后:%.1f,%.1f,%.1f,%.1f",label.frame.origin.x,label.frame.origin.y, label.frame.size.width, label.frame.size.height);
_fWidth= label.frame.size.width;
_fHeight= label.frame.size.height;
}
- (void)updateAttributes:(NSDictionary*)attributes{
[superupdateAttributes:attributes];
}
注册组件
[WXSDKEngine registerComponent:@"textcomponent" withClass:[RFRichTextComponent class]];