使用Stevia简化UI代码

Example 1: 项目实践

请添加图片描述

01 - View hierarchy

Native:

view.addSubview(backgroundImageView)
view.addSubview(screenshotImageView)
screenshotImageView.addSubview(maskView)
view.addSubview(titleLabel)
view.addSubview(arrowImageView)

Stevia:

view.subviews {
    backgroundImageView
    screenshotImageView.subviews {
        maskView
    }
    titleLabel
    arrowImageView
}

02 - Layout

Masonry:

if let maker = backgroundImageView.mas_makeConstraints() {
    maker.leading.trailing().bottom().top().offset()(0)
    maker.install()
}
 
if let maker = screenshotImageView.mas_makeConstraints() {
    maker.leading.trailing().bottom().top().offset()(0)
    maker.install()
}
 
if let maker = maskView.mas_makeConstraints() {
    maker.leading.trailing().bottom().offset()(0)
    maker.top.equalTo()(screenshotImageView.mas_centerY)
    maker.install()
}
 
if let maker = titleLabel.mas_makeConstraints() {
    maker.bottom.offset()(-16)
    maker.centerX.equalTo()(view)
    maker.install()
}
 
if let maker = arrowImageView.mas_makeConstraints() {
    maker.left.equalTo()(titleLabel.mas_right)
    maker.centerY.equalTo()(titleLabel)
    maker.size.equalTo()(CGSize(width: 12, height: 12))
    maker.install()
}

Stevia:

backgroundImageView.fillContainer()
         
screenshotImageView.fillContainer()
         
maskView.bottom(0).fillHorizontally()
maskView.Top == view.CenterY
         
titleLabel.bottom(16).centerHorizontally()
         
arrowImageView.Left == titleLabel.Right
arrowImageView.CenterY == titleLabel.CenterY
arrowImageView.size(12)

Example2:官方demo

请添加图片描述

Native:

// 01 - View Hieararchy
email.translatesAutoresizingMaskIntoConstraints = false
password.translatesAutoresizingMaskIntoConstraints = false
login.translatesAutoresizingMaskIntoConstraints = false
addSubview(email)
addSubview(password)
addSubview(login)

// 02 - Layout (using latest layoutAnchors)
email.topAnchor.constraint(equalTo: topAnchor, constant: 100).isActive = true
email.leftAnchor.constraint(equalTo: leftAnchor, constant: 8).isActive = true
email.rightAnchor.constraint(equalTo: rightAnchor, constant: -8).isActive = true
email.heightAnchor.constraint(equalToConstant: 80).isActive = true
 
password.topAnchor.constraint(equalTo: email.bottomAnchor, constant: 8).isActive = true
password.leftAnchor.constraint(equalTo: leftAnchor, constant: 8).isActive = true
password.rightAnchor.constraint(equalTo: rightAnchor, constant: -8).isActive = true
password.heightAnchor.constraint(equalToConstant: 80).isActive = true
 
login.topAnchor.constraint(lessThanOrEqualTo: password.bottomAnchor, constant: 20).isActive = true
login.bottomAnchor.constraint(equalTo: bottomAnchor, constant: 0).isActive = true
login.leftAnchor.constraint(equalTo: leftAnchor).isActive = true
login.rightAnchor.constraint(equalTo: rightAnchor).isActive = true
login.heightAnchor.constraint(equalToConstant: 80).isActive = true

Stevia:

subviews {
    email
    password
    login
}
 
layout {
    100
    |-email-| ~ 80
    8
    |-password-| ~ 80
    >=20
    |login| ~ 80
    0
}

相比之下,Stevia更加简洁,可读性更强。简洁的代码可以显著降低维护成本。

Q: 为什么不用XIB?
A: 因为没有什么比纯代码更真实🤓。XIB和storyboards很重,很难维护,很难Merge。而且将视图概念拆分为两个独立的文件,增加了调试的复杂度。

https://github.com/freshOS/Stevia

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
您可以使用Elasticsearch来为多个字段添加前缀索引。以下是一个示例的步骤: 1. 创建一个索引: ``` PUT my_index ``` 2. 定义索引的映射(mapping),在映射中指定字段的类型和属性: ``` PUT my_index/_mapping { "properties": { "field1": { "type": "text", "fields": { "prefix": { "type": "text", "analyzer": "autocomplete" } } }, "field2": { "type": "text", "fields": { "prefix": { "type": "text", "analyzer": "autocomplete" } } } } } ``` 3. 创建一个自定义的分析器(analyzer),用于生成带有前缀的索引: ``` PUT my_index/_settings { "analysis": { "analyzer": { "autocomplete": { "tokenizer": "autocomplete", "filter": [ "lowercase" ] } }, "tokenizer": { "autocomplete": { "type": "edge_ngram", "min_gram": 2, "max_gram": 10, "token_chars": [ "letter", "digit" ] } } } } ``` 4. 插入文档到索引中: ``` PUT my_index/_doc/1 { "field1": "apple", "field2": "banana" } ``` 5. 搜索带有前缀的字段: ``` GET my_index/_search { "query": { "prefix": { "field1.prefix": "ap" } } } ``` 在上述示例中,我们创建了一个名为`my_index`的索引,并定义了两个字段`field1`和`field2`。然后,我们为这两个字段创建了带有前缀的索引。最后,我们插入了一个文档,并使用前缀搜索查询来查找以"ap"开头的字段。 请注意,上述示例中的分析器和索引设置是简化的示例,您可以根据自己的需求进行调整和扩展。此外,您还可以使用其他搜索查询来满足您的需求,如通配符查询、正则表达式查询等。 希望以上信息对您有所帮助!如有更多问题,请随时提问。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值