Giter Club home page Giter Club logo

Comments (3)

vitalii-tym avatar vitalii-tym commented on September 24, 2024

So far came to this solution to calculate best itemsMargin:

let widestImageSize = items.map({ $0.image.size.width }).max() ?? 0
let longestTitle = items.max(by: { $1.title.count > $0.title.count })?.title ?? ""
let closedItemwidth = widestImageSize + options.innerPadding * 2
let maxPossibleOpenItemWidth = longestTitle.expectedWidth(for: options.titleFont) + widestImageSize + options.innerPadding * 2 + options.imageTitleMargin
let expectedMargin = (UIScreen.main.bounds.size.width - closedItemwidth * CGFloat(items.count - 1) - maxPossibleOpenItemWidth) / CGFloat(items.count - 1)

options.itemsMargin = expectedMargin

where expectedWidth(for) is a calculation of text's width. For example, like this:

extension String {
    func expectedHeight(for font: UIFont, and width: CGFloat) -> CGFloat {
        return self.boundingRect(
            with: CGSize(width: width, height: CGFloat.greatestFiniteMagnitude),
            options: [.usesLineFragmentOrigin, .usesFontLeading],
            attributes: [NSAttributedStringKey.font: font],
            context: nil).height
    }
}

screenshot 2018-08-31 18 48 56

screenshot 2018-08-31 18 49 11

from smoothtab.

vitalii-tym avatar vitalii-tym commented on September 24, 2024

In my PR #6 SmoothTabItem is able to calculate its inner width now.
Once accepted, it will be possible to recalculate best itemsMargin easier. Example code, assuming there might be different number of tabs:

let widestImageSize = tabItems.map({ $0.image.size.width }).max() ?? 0
let closedItemwidth = widestImageSize + options.innerPadding * 2

let tabWidths = tabItems.map({ $0.expectedWidth(for: options.titleFont) + options.innerPadding * 2 + options.imageTitleMargin })
let widestOpenTabWidth: CGFloat = tabWidths.max() ?? CGFloat.greatestFiniteMagnitude

let sumOfOpenTabs = tabWidths.reduce(0, +)
let screenWidth = UIScreen.main.bounds.width
if tabItems.count == 2 && widestOpenTabWidth <= screenWidth / 2 { // if only 2 of them, and the biggest one takes less than half screen
   options.itemsMargin = screenWidth / 2 - (tabWidths.first ?? 0) // placing 2nd tab right in the second half of the screen
} else if sumOfOpenTabs <= screenWidth { // if all tabs would fit screen when opened
   options.itemsMargin = (screenWidth - sumOfOpenTabs) / CGFloat(tabItems.count - 1) // trying to place them evenly
} else {
   let expectedMargin = min(max(0, (screenWidth - closedItemwidth * CGFloat(tabItems.count - 1) - widestOpenTabWidth) / CGFloat(tabItems.count - 1)), screenWidth)
   options.itemsMargin = expectedMargin // distance between buttons
}

from smoothtab.

yervandsar avatar yervandsar commented on September 24, 2024

@vitalii-tym it's great, thank you.

from smoothtab.

Related Issues (5)

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.