Coding With Fun
Home Docker Django Node.js Articles Python pip guide FAQ Policy

Which is the right way to set uibutton title and title color?


Asked by Charlee Walls on Dec 13, 2021 FAQ



This is how you should actually do it: Perhaps now I'll finally remember the right way to do it, and I hope you will too!
One may also ask,
Though there's no built in way to put padding between the image and the title of a UIButton, we can fake it by using what we know about the various inset properties. Let's assume we want a button with 10pt padding around the content and 10pt padding between the image and the title.
In this manner, As the title's drawing rect compresses (i.e., as positive insets are added), letters get cut off with ellipses. As with imageEdgeInsets, negative values translate the text without otherwise affecting the button's layout.
Subsequently,
To create a new UIButton, set its width, height, and position the button within a view programmatically, you can do with CGRect class. For example: Setting text on a button is very simple. It can be done with a function call setTitle (_ title: String?, for state: UIControl.State) . A title on the button can be set for different button states.
In respect to this,
Unfortunately, UIButton doesn't have support for NSDirectionalEdgeInsets currently, so you have to query the userInterfaceLayoutDirection to figure out whether to use the right-to-left version. Hopefully this post helps some folks wrap their heads around a weird UIKit API.