Weex组件:<text>

官方手册

  • <text> 是 Weex 内置的组件,用来将文本按照指定的样式渲染出来。<text> 只能包含文本值,你可以使用 {{}} 标记插入变量值作为文本内容。

特性

  • value {string}: 组件的值,与<text> 标签中的文本内容相同。

样式

  • lines {number}: 指定文本行数。默认值是 0 代表不限制行数。
  • 支持 color 样式.
  • 支持 font-size 样式. iOS默认值:32(16点,这里是px),Android:不同设备不同,H5 默认值:32.
  • 支持 font-style 样式.
  • 支持 font-weight 样式.
  • 支持 text-align 样式.
  • 支持 text-decoration 样式.
  • 支持 text-overflow 样式.
  • 支持 line-height样式 。line-height 在 iOS 中与 H5 和 Android 中不同, 文本值将放置在框的底部。

约束

  • <text> 里直接写文本头尾空白会被过滤,如果需要保留头尾空白,暂时只能通过数据绑定写头尾空格。

  • 不支持 flex-direction, justify-content, align-items 这些为子节点设置的属性,并且<text>没有子节点。

示例

<template>
  <div class="wrapper">
    <div class="panel">
      <text class="text" lines="3">Weex 是一套简单易用的跨平台开发方案,能以 Web 的开发体验构建高性能、可扩展的原生应用。Vue 是一个轻量并且功能强大的渐进式前端框架。</text>
    </div>
    <div class="panel">
      <text class="text" lines="3">Weex is an cross-platform development solution that builds high-performance, scalable native applications with a Web development experience. Vue is a lightweight and powerful progressive front-end framework. </text>
    </div>
  </div>
</template>
<style scoped>
  .wrapper {
    flex-direction: column;
    justify-content: center;
  }
  .panel {
    width: 600px;
    margin-left: 75px;
    border-width: 2px;
    border-style: solid;
    border-color: #BBB;
    padding-top: 15px;
    padding-bottom: 15px;
    padding-left: 15px;
    padding-right: 15px;
    margin-bottom: 30px;
  }
  .text {
    lines: 3;
    color: #666666;
    font-size: 32px;
  }
</style>

SDK 源码

  • 组件类:WXTextComponent
[self registerComponent:@"text" withClass:NSClassFromString(@"WXTextComponent") withProperties:nil];
  • 实现方式是NSTextStorage
- (void)viewDidLoad
{
    ((WXText *)self.view).textStorage = _textStorage;
    [self setNeedsDisplay];
}

- (UIView *)loadView
{
    return [[WXText alloc] init];
}
@interface WXText : UIView

@property (nonatomic, strong) NSTextStorage *textStorage;

@end
@interface NSTextStorage : NSMutableAttributedString

UIKit中。

  • 字符内容是放在一个NSString中,但却是画出来的,想法有点奇特
- (UIImage *)drawTextWithBounds:(CGRect)bounds padding:(UIEdgeInsets)padding
{
    if (bounds.size.width <=0 || bounds.size.height <= 0) {
        return nil;
    }
    UIGraphicsBeginImageContextWithOptions(bounds.size, self.layer.opaque, WXScreenScale());
    CGContextRef context = UIGraphicsGetCurrentContext();
    
    if ([self.wx_component _needsDrawBorder]) {
        [self.wx_component _drawBorderWithContext:context size:bounds.size];
    }
    
    NSLayoutManager *layoutManager = _textStorage.layoutManagers.firstObject;
    NSTextContainer *textContainer = layoutManager.textContainers.firstObject;
    
    CGRect textFrame = UIEdgeInsetsInsetRect(bounds, padding);
    NSRange glyphRange = [layoutManager glyphRangeForTextContainer:textContainer];
    
    [layoutManager drawBackgroundForGlyphRange:glyphRange atPoint:textFrame.origin];
    [layoutManager drawGlyphsForGlyphRange:glyphRange atPoint:textFrame.origin];
    
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    
    UIGraphicsEndImageContext();
    
    return image;
}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 问答题47 /72 常见浏览器兼容性问题与解决方案? 参考答案 (1)浏览器兼容问题一:不同浏览器的标签默认的外补...
    _Yfling阅读 13,827评论 1 92
  • 发现 关注 消息 iOS 第三方库、插件、知名博客总结 作者大灰狼的小绵羊哥哥关注 2017.06.26 09:4...
    肇东周阅读 12,262评论 4 61
  • 在学习weex的过程中看到了常用标签相关的内容,为了自己以后能够快速查阅特整理出此文档。 a 简介组件定义了指向某...
    TyroneTang阅读 4,742评论 1 3
  • 记下每天遇到的三件开心的事。 还有给自己在锻炼的品行打个分。 昨天开心的事有: 1. 走了一万多步。 2. 看了一...
    七不斯阅读 293评论 0 0
  • 前略……建立o2o汽车电商平台,打通从线下客户发展到线上交易,线下交易转化为线上客户的渠道。建议采用“先租后买”、...
    Juno窝窝小报阅读 119评论 0 0