Is there an option to fit image size within a button? - red

If %heads.jpg is big, in the case of button it won't resize:
view [
image 140x140 %heads.jpg
button 140x140 %heads.jpg
]
Is there an option to fit image size within a button ?

There is one, though currently only supported on Windows:
view [button 140x140 %head.png with [image: reduce [image size]]
So this changes the /image facet of the button face, to refer to a block containing the image value and a new target size for the image.

Related

How do you create a NSTouchBarItem that uses the remaining width in the Touch Bar?

I'm trying to create a NSButton that takes up the remaining space on the Touch Bar. My goal is to have a button similar to the "Search or enter website name" button in Safari.
I've looked at setting constraints with widthAnchor, but I'm completely lost at how to set it properly.
NSButton has an intrinsicContentSize that fits its content, and a default horizontal contentHuggingPriority of 750 (NSLayoutPriorityDefaultHigh), meaning it will strongly try to match that content width.
You can decrease that hugging priority (e.g. [button setContentHuggingPriority:1.0 forOrientation:NSLayoutConstraintOrientationHorizontal]) in order to make it more able to stretch away from that content, and in this case fill up the extra space in the touch bar.

iOS - Interface Builder: UIButton title disappears when setting image

I have made a project previously where I set a UIButton to have both text and also an image (in interface builder).
I have created another project and I want to achieve the same thing, but when I set the button to have an image, the title disappears (it's not getting hidden behind the image).
I can't seem to figure out why this is so if anyone could shed some light on this subject I'd appreciate it!
Thanks.
Change your button to custom type. put the image on then put the text in.
You will need to put your own background on it, but at that point you will be able to use the edge settings to position both the text and the image.
Every button that I have with image and text is set to custom and ultimately has its own bg image as well.
Oh, another thing I use. Is the cornerRadius. To give the view the rounded effect like the buttons have by default.
saveButton.layer.cornerRadius = roundingNumber.floatValue;
Additionally you should be able to use the borderWidth and borderColor in conjunction with cornerRadius to get a button that looks quite similar to the original button
saveButton.layer.borderRadius = 1.0f;
saveButton.layer.borderColor = [[UIColor blueColor] CGColor];
// These are not the exact values (or maybe they are) but you get the idea.
hope that helps.
Correction
After Spending a little time investigating, it appears that when you put an image in the button. the text is shoved off to the right.
Use the edge settings to bring it back over the image.
With the button selected, Look in "Attributes Inspector" > Button > Edge > (Dropdown "Title")
the equasion I have come up with is
[Edge inset for Title] Left = -(imageWidth * 2)
This should Left align your title with the image that you have put in.
Custom positioning will need to be done to make it look correct.
This can be done entirely in the Interface Builder and you can use RoundedRect button style
Here are some images demonstrating the change.
Here I set the Button type to "Custom" to remove the border and background.
Here I set the Top and Left in the Edge set for "Title" (Title is an option in the drop down)
EDIT
Newest Xcode has moved the size settings to a new location
Just put the image in the background field instead. This way the text will appear in front of the image.
It sounds like you are setting the image for the button but not the background image so reverse that.
I noticed that if the UIButton colour is set to default, adding an image makes it white and thus invisible. Changing the colour manually makes it visible again.

Can we increase button height if the button label text is so long in iPhone?

I have put 4 buttons with 4 labels on the button as button title using interface builder. My button label shows the showroom name, email id...etc with font size 17 correct. But if Showroom name or email id get bigger then my button label shows information with small font size which looks odd and not according to apple HIG.
So can we increase button height programmatically to show button label title with consistent font size 17.
you can use 'sizeToFit' function to RESIZE button to it's content optimum display,
or
you can use 'sizeThatFits:CGSizeZero' to actually GET the optimum frame and do whatever you want with it.
OBS: this will only work if 'sizeThatFits' is bigger that original size, so i suggest you to set size to something like (1,1) before calling the functions above
Did you try with, something like this:
button.frame = CGRectMake(0,0,80,200)
to change the height of the button?

UIButton image gets pixelated when highlighted

I have an UIButton (type:custom) in an UIView set up in the interface builder, add an image for the default state to it with retina sizes. When I try on my iPhone, I see the retina version of the image, when I highlight it by tapping it it gets pixelated while being highlighted.
The funny part is when I change the image of the UIButton by code, there is no pixelated error.
Does anyone know a workaround without loading all my button images by code? thnx!
In interface builder you can specify both regular state and highlighted state images: simply choose the required "State Config" and configure the button.
If you specified an image for Default state, and left the image for "Highlighted" state empty, and checked the "Highlighted Adjusts Image" property on, then the button will use the Default state image and gray it out when it's pressed.
However, if your Highlighted state image is also set, the button will use that instead of generating its own highlighting.
Other things to check: make sure both versions of the image are included in the project.
E.g. say you have "button_image.png" and "button_image#2x.png". Make sure both of them are in the project. Also when you specify the image in the interface builder, use the name of the standard definition image, cocoa will pick the 2x version automatically - i.e. you would put "button_image.png" as your button's image, not "button_image#2x.png"
Hope this helps

set gradient colour (color) for uibutton background image iphone sdk

I have purchased the Glyphish set of icons.. but they are all dark. I was wondering if there's a way to set the PNG gradient colour when I set the image as the background of a UIButton?
I was thinking of something like when the button is disabled, it changes colour. Is there any way to replicate this functionality??
The simplest method would be to create copies of the icons you're using, and tint/modify them in your favorite image editor. Name the various images something like:
icon.png
icon-disabled.png
Then, you can set one image for each button state. In Interface Builder, just select the button and in the Inspector window you'll see a popup menu that says "Default State Configuration". The image(s) you specify here will be used for all the various button state by default. In the Background field, enter icon.png. Then select "Disabled State Configuration" from the menu and enter icon-disabled.png as the Background image. Be sure to set the menu back to Default State Configuration to avoid getting confused in future edits.
If you want to set the same programmatically, just use something like:
[myButton setBackgroundImage:[UIImage imageNamed:#"icon.png" forState:UIControlStateNormal];
[myButton setBackgroundImage:[UIImage imageNamed:#"icon-disabled.png" forState:UIControlStateDisabled];
It's possible to take an image and modify it programmatically with a gradient, but unless you're doing it all the time and modifying the gradient based on user input or something, you'll save yourself a lot of headaches and time by just doing it this way.
I Have some code for the gradient... if you want simple color for different states of the button.. i. e
selected, disabled and all.
You can create different configuration from the interface builder.
Code which I have will give you gradient color for button but then you check the status of the button to use different gradient colors..
Make your button backgrounds with this free app: http://itunes.apple.com/us/app/uibutton-builder/id408204223?mt=8