美文网首页
swift 计算tag有多少行

swift 计算tag有多少行

作者: 松龄学编程 | 来源:发表于2020-05-16 19:31 被阅读0次
    import UIKit
    
    var str = "Hello, playground"
    
    let arr:[CGFloat] = [2, 5, 3, 6, 4, 3, 2]
    
    let a:CGFloat = 8
    
    var newArr: [[CGFloat]] = []
    
    let b:[CGFloat] = [2]
    newArr.append(b)
    
    var iterator = arr.makeIterator()
    var next = iterator.next()
    next = iterator.next()
    while next != nil {
        guard let value = next else { break }
        if var arr = newArr.last {
            let sum = arr.reduce(value, +)
            print(sum)
            if sum >= 8 {
                print("value: \(value)")
                var newA: [CGFloat] = []
                newA.append(value)
                newArr.append(newA)
            } else {
                arr.append(value)
                newArr[newArr.count - 1] = arr
            }
        }
        
        next = iterator.next()
    }
    
    print(newArr)
    
    struct TagCalculation<T: FloatingPoint> {
        
        let widths: [T]
        let maxWidth: T
        
        init(widths: [T], maxWidth: T) {
            self.widths = widths
            self.maxWidth = maxWidth
        }
        
        func getRows() -> Int {
            let arr = widths
    
            let a = maxWidth
    
            var newArr: [[T]] = []
    
            let b = [widths[0]]
            newArr.append(b)
    
            var iterator = arr.makeIterator()
            var next = iterator.next()
            next = iterator.next()
            while next != nil {
                guard let value = next else { break }
                if var arr = newArr.last {
                    let sum = arr.reduce(value, +)
                    print(sum)
                    if sum >= a {
                        print("value: \(value)")
                        var newA: [T] = []
                        newA.append(value)
                        newArr.append(newA)
                    } else {
                        arr.append(value)
                        newArr[newArr.count - 1] = arr
                    }
                }
                
                next = iterator.next()
            }
    
            print(newArr)
            return newArr.count
        }
    }
    
    let c = TagCalculation(widths: arr, maxWidth: a)
    
    c.getRows()
    
    

    增加了gap,2个label间距,需要注意gap,是否算对了。如果用了marginlabel,记得把marginlabel中margin也加上。

    struct TagCalculation<T: FloatingPoint> {
        
        let widths: [T]
        let maxWidth: T
        let gap: T
        
        init(widths: [T], maxWidth: T, gap: T) {
            self.widths = widths
            self.maxWidth = maxWidth
            self.gap = gap
        }
        
        func getRows() -> [[T]] {
            let arr = widths
            
            guard arr.count > 0 else { return [] }
    
            let a = maxWidth
    
            var newArr: [[T]] = []
    
            let b = [widths[0]]
            newArr.append(b)
    
            var iterator = arr.makeIterator()
            var next = iterator.next()
            next = iterator.next()
            while next != nil {
                guard let value = next else { break }
                if var arr = newArr.last {
                    let sum = arr.reduce(value) { (result, new) -> T in
                        return result + new + gap
                    }
                    if sum >= a {
                        var newA: [T] = []
                        newA.append(value)
                        newArr.append(newA)
                    } else {
                        arr.append(value)
                        newArr[newArr.count - 1] = arr
                    }
                }
                
                next = iterator.next()
            }
    
            return newArr
        }
    }
    

    相关文章

      网友评论

          本文标题:swift 计算tag有多少行

          本文链接:https://www.haomeiwen.com/subject/erirohtx.html