Comments (3)
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
}
}
from smoothtab.
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.
@vitalii-tym it's great, thank you.
from smoothtab.
Related Issues (5)
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from smoothtab.