Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ability to set selected segment's label's text color #26

Closed
StainlessStlRat opened this issue Sep 18, 2019 · 34 comments
Closed

Ability to set selected segment's label's text color #26

StainlessStlRat opened this issue Sep 18, 2019 · 34 comments
Assignees

Comments

@StainlessStlRat
Copy link

When the segment is selected, it sets the label color to .background.

This is fine for the default colors, but my tint is a shade of blue. I'd prefer that when the segment is selected, the text color is white, but in night mode the text will be black.

@yonat
Copy link
Owner

yonat commented Sep 19, 2019

I made two changes that should help:

  1. use the real background color of the view behind instead of the system background.
  2. implement setTitleTextAttributes(_:for:) (works like its UISegmentedControl counterpart).

Hope it works for you!

@StainlessStlRat
Copy link
Author

Perfect! Thanks for being so responsive!

@StainlessStlRat
Copy link
Author

Not sure what I'm doing wrong here, but I get an unrecognized selector error in obj C (even though the function autofills). _ownedComponents is the multiselectedsegmentedcontrol:

NSDictionary *attributes = @{NSForegroundColorAttributeName : [UIColor whiteColor]};
[_ownedComponents setTitleTextAttributes:attributes for:UIControlStateSelected];

@StainlessStlRat
Copy link
Author

So I was thinking that you have the two colors: tint and background. Shouldn't they just be switched when the item is selected?

Ex: My tint is blue-ish, my background is white.
Unselected == white background / blue text
Selected == blue background / white text

Is that what you meant for #1? If so, that #1 behavior is not working on my end.

@yonat
Copy link
Owner

yonat commented Sep 20, 2019

About the undefined selector:
I think the objc counterpart is setTitleTextAttributes:forState (instead of the for: in Swift) - like in https://developer.apple.com/documentation/uikit/uisegmentedcontrol/1618570-settitletextattributes?language=objc .

@StainlessStlRat
Copy link
Author

It won't compile if I set it to that. Says "no recognized selector"

@yonat
Copy link
Owner

yonat commented Sep 20, 2019

About the background color behavior:
What do you see when you select a segment?
Would you upload a picture? (with some background around the control for comparison)

@yonat
Copy link
Owner

yonat commented Sep 20, 2019

Do you get auto completion for the objc?

@StainlessStlRat
Copy link
Author

Yes it autocompleted weirdly enough!

@StainlessStlRat
Copy link
Author

Screen Shot 2019-09-20 at 12 49 21 AM

@StainlessStlRat
Copy link
Author

I want the same colors whether or not it's night mode if that matters

@StainlessStlRat
Copy link
Author

Also anyway to make the divider thinner? Feels like it's thicker than it used to be

@yonat
Copy link
Owner

yonat commented Sep 20, 2019

Where does the white comes from? The unselected background is clear.
If you make the white view into the superview of the control, it should work.

@StainlessStlRat
Copy link
Author

I've got it set to white in interface builder. If I set it to clear won't the background be dark? That'll clash with the blue color/make it hard to read

@yonat
Copy link
Owner

yonat commented Sep 20, 2019

dividers are the same width as border, so:

multi.borderWidth = 0.5

@StainlessStlRat
Copy link
Author

Ah I looked in the code and could swear it was set to 1.0

@yonat
Copy link
Owner

yonat commented Sep 20, 2019

Hmmm. didn't thing to use the control's own backgroundColor. That's a good idea actually.

@StainlessStlRat
Copy link
Author

@IBInspectable open dynamic var borderWidth: CGFloat = 1 {

Does this mean it's set to 1.0?

@yonat
Copy link
Owner

yonat commented Sep 20, 2019

Yes.

@StainlessStlRat
Copy link
Author

Ohhh gotcha

@StainlessStlRat
Copy link
Author

Small defects if I set it lower. Look at the left hand side. You can see white around the border
Screen Shot 2019-09-20 at 12 57 40 AM

@yonat
Copy link
Owner

yonat commented Sep 20, 2019

Fixing the background color thing, will push in a few minutes.

@StainlessStlRat
Copy link
Author

Awesome! Thanks for being so responsive!

@yonat
Copy link
Owner

yonat commented Sep 20, 2019

You caught me in a good time!
Just pushed 2.1.1 with the fixes. Now your code should just work, without setting foreground color.

@StainlessStlRat
Copy link
Author

Hm still getting the same behavior

@StainlessStlRat
Copy link
Author

Did you fix the sliver showing on the left? I still see that too. Maybe I messed something up

@yonat
Copy link
Owner

yonat commented Sep 20, 2019

Weird. Sure you updated the pod/package? Maybe do a Product>Clean and then try again.

@StainlessStlRat
Copy link
Author

Clean rebuild fixed it!

@StainlessStlRat
Copy link
Author

Screen Shot 2019-09-20 at 1 19 03 AM

@StainlessStlRat
Copy link
Author

There's still an ever so small sliver of white on the leftmost segment if it's selected. Any idea what that is? I'm just worried that on other devices it might be even more pronounced!

@yonat
Copy link
Owner

yonat commented Sep 21, 2019

Probably antialiasing. Doesn't reproduce for me, maybe try to ensure the view is aligned to whole pixels and see if it changes anything.

@StainlessStlRat
Copy link
Author

Ya it's definitely whole pixels. I didn't even realize you could set constraints that were sub pixel. Happens on another view where I use the tool that just uses autoresizemasks too

@StainlessStlRat
Copy link
Author

When you test for the sliver of white on the side, have you set the border width to 0.5? It only happens when I try to make the border sizes look more like they used to by setting them to 0.5

@yonat
Copy link
Owner

yonat commented Sep 27, 2019

Half pixel can happen unintentionally, as a result of centering or indenting.
Anyway, it doesn't reproduce for me so I can't fix it. If you send me a project that reproduce this behavior (and specify on which simulator to run it) then I can try to fix it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants