Coder Social home page Coder Social logo

haovbvb / stringcalculate Goto Github PK

View Code? Open in Web Editor NEW

This project forked from notfound9/stringcalculate

0.0 1.0 0.0 49 KB

一种更加方便和高效计算多行Label高度的新方法,A new way to calculate label height more convenient and efficient。

Swift 98.70% Objective-C 1.30%

stringcalculate's Introduction

StringCalculate

一种更加方便和高效计算多行Label高度的新方法

与系统的提供的boundingRect相比,优点主要有以下两点:

1.耗时较少

在Demo和我们实际项目中测试结果中,时间消耗约为系统的boundingRect方法的30%。

2.调用更加方便。

在使用系统的boundingRect方法进行Label高度计算时,通常情况下,我们需要根据最大行数来估算Label最大的高度,类似于这样:

let maxLine = 3//最大行数
let singleLineHeight = 20//单行高度
let maxHeight = CGFloat(maxLine * singleLineHeight)//计算得到最大宽度
let rect = text.boundingRect(with: CGSize(width: UIScreen.main.bounds.size.width, height: maxHeight),
                                      options: .usesLineFragmentOrigin,
                                      attributes: [NSAttributedString.Key.font: UIFont.boldSystemFont(ofSize: 16)],
                                      context: nil).size.width

但是在使用这种新方法计算时,我们只需要指定最大高度就行了,类似于这样:

let rect = text.boundingRectFast(withMaxWidth: UIScreen.main.bounds.size.width, 
                                          font: UIFont.boldSystemFont(ofSize: 16), 
                                          maxLine: maxLine)

介绍文章:https://mp.weixin.qq.com/s/zHAlxALPMFZLv8M8bJaRKA

有问题或者建议欢迎加我微信ruiwendelll,我们一起探讨学习,谢谢了!

stringcalculate's People

Contributors

notfound9 avatar

Watchers

James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.