I am trying to create custom segment control using CollectionViewCell. So i can manage text size of the segment control in different devices. But it is not working properly in all the devices.
I have taken one cell inside collection view and used sizeForItemAt
delegate method to equally set the cells. There will be only 3 cells. That is shown in the image.
This is the code for sizeForItemAt
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
return CGSize(width: (collectionView.frame.width / 3), height: 50.0)
}
Here is the output that i am getting
so i am trying to find solution using which i can manage text size in all the devices and all cells can take equal spacing as well.
2
Answers
try this
Below is one way I can think of doing it but by no means the only way.
First, I started by setting up a collection view with a horizontal flow layout.
Once I do this, I run into the same problem, that depending on the width of the screen, my text could get cut off.
So once the width of each segment is determined, we have to calculate the maximum font size to show the complete text for the longest segment and that font size should be applied to all
In this case, the long segment is
Vibration Intensity
in terms of string length.Then set the
segmentFontSize
incellForItemAt indexPath
This will give you something like this:
If you found some part difficult to follow, here is the link to the complete code: https://gist.github.com/shawn-frank/03bc06d13f90a54e23e9ea8c6f30a70e