How to resize an NSImageView in an NSStackView? - swift

I'm working on a project where I want the following in an NSTableViewCell:
Image
Text
Subtitle text
The NSTableView is in a window which the user can expand or contract. When the window expands and contracts, the text wraps as needed.
That much is working.
When it comes to the image view, I can't get the thing to resize at all. I don't understand how the text automatically wraps, but images don't automatically scale. I've been working on iOS so long that I might have missed something in how stack views differ between iOS and macOS, but I never had this problem in iOS.
I don't have much code because the text wraps properly without any code at all, so instead I posted a minimal project showing the problem on Github:
NSImageTableViewTest
Some things I tried:
I have to set the width/height of the image view, or the text won't wrap. It seems to me that the reason why is that if I keep the image view unbound, the table view starts at the width of the image.
I tried setting the leading and bottom constraints of the image view to no avail.
I tried setting the constraints of the NSStackView, but that doesn't help constrain the frame of the image view.
Question: do I have to change the frame of the image view in code? I did try that, to no avail.
At this point, I'm stumped and I'm sure the fix is something easy that I overlooked.
Thanks.

The image view doesn't shrink because the default Content Compression Resistance Priority is too high. Set the Content Compression Resistance Priority to (a bit lower than) "Low (250)".

Related

How to overlay text on image using swift?

First of all, I am using Auto Layout for this Swift application. In one view, i have an image that scales to full screen. And over this image there is an overlay text.
The problem is; When opening in iPhone6, the image scales and fits nicely but the text misplaces (normally) on the image, since the constraints are still the same.
I was thinking of merging the text and the image into one image layer so they scale and fit together. Is this possible ?
Or, anyone knows any other solution rather than trying to understand device and changing text placement contstraints accordingly ?
if you are using auto layout for display text in Lable. than must you have to play with constrain from interface builder. share you screenshot

Vertically aligning text within a UILabel and getting views below it to move up accordingly

I'm using the workaround described here to vertically align text in a multiline UILabel to the top. Unfortunately that solves only half of the problem because I also want to collapse the remaining space and move other labels below the label to move up and consume the space.
Is there anyone way to achieve this without having to resort to calculating the Frame's of the other views yourself and thus taking over the layouting?
Here's a screenshot of my problem. To make it easier I've colored the background of the relevant labels. The Blue label is supposed to move up and consume the unused space of the red label. The Red Label is configured for 3 Lines and Font-Autoshrink is off.
You must re-calculate your label frames and then rearrange your labels in the view.
Or you can try to use CoreText to make more complex layouts without the need to cope with these frames rearrangement. On GitHub there is a nice work BSD-licensed, called NSAttributedString+HTML: https://github.com/Cocoanetics/NSAttributedString-Additions-for-HTML which tries to port in the iOS the same methods already available in OSX. It is based on CoreText and with this class you can write your page as html and render it on screen without using UIWebView (and without its limitations). Based on your screenshot, it would be easy to achieve the wished result with simple html.

UI Button with Image Smaller than Touchable Area

I've not much response so am adding some more info.
My buttons are not rectangular, nor organised in a grid so I need a way of creating what looks like a button (and shows that it has been pressed visually, as per a standard UIButton) but where the touchable area is different to the image area.
I am using a transparent PNG and that element works fine. I've added the buttons in Interface Builder and am wondering if that is the problem.
However, if I change imageEdgeInsets, it distorts the image display, which is obviously not what I want.
Bizarrely, if I increase the dimemsions of the button, it doesn't change the image, but if I decrease them it does.
I have tried different combinations of mode (scale to fill etc), but to no avail.
I am aware that there is an image and background image property, but in IB there is only one.
Essentially, I don't understand how the geometry works and the Apple documentation doesn't seem to help.
Surely, I can't be the only person to try to do this. Any help would be warmly welcomed.
Many thanks,
Chris.
Try setting the buttons setting to Aspect Fit. This will fill the button with your image so a smaller image than the button size would leave the space around the image.
Also set the button type to custom.
In the end, I stumbled across Ole Begemann's Non-rectangular buttons class. It just does what I need - to be able to create buttons where the touchable area follows the visible element of a non rectangular image.
#Helium3 - thanks - that allowed me to use a larger touch area, bit not a smaller one.

iOS playing cards

I currently have an image which has every suit and value of a deck of playing cards in it. There is an UIImageView to show this picture, with width and height set to show just one card at a time and view mode set to "top left" so it doesn't stretch.
Now I'm just showing what happens to be the top left card, the ace of clubs. I tried finding a way to offset the view to show a different portion of the UIImage, but couldn't find a suitable property (tried fiddling with "frame" and "bounds").
Is there a way to get such an offset working, or am I going about this the wrong way? I'd like to keep all the cards in one image in "sprite sheet" style so that they would be easy to modify later instead of having a bunch of separate pics.
Set the bounds to what you'd like to show, enable 'clipToBounds' and make sure the content stretch mode is set properly. That should work.
EDIT: I do recommend making different images though, because of memory issues (even though not the whole image is shown, you still fully have it in memory).

Forcing UITextView to stay the same size while typing

I have a UITextView that I shrink upon bringing up the keyboard. An issue I just ran into though is as I type past the keyboard, rather than the text start scrolling, the UITextView automatically gets bigger. How can I possibly turn this off?
EDIT
This is not a subclass, this is a plain old UITextView. It's definitely resized and gets visibly bigger (the box around the text extends past the keyboard). Not sure what you mean about flexible width/height. I tried setting it to grow and shrink with the window maybe? But that did nothing for me.
I've tried changing the content size and/or frame size, that didn't help.
I agree something very odd is going on here. Still looking for why it could be resizing.
EDIT
Nevermind. A bad fix I had to force the textview to refresh was being called unexpectedly and causing it to grow.
To my knowledge, a standard UITextView with no special flags applied shouldn't do that. In fact, I don't know that a UITextView does that at all, so my response would be in the form of a question: are you sure that your UITextView is in fact resized to fit completely within the bounds of the visible area above the keyboard? Are you using a plain ol' UITextView and not a subclass? (I know that the TTTextEditor is a class from the Three20 project whose sole purpose is to grow as you type, which is why I'm suspicious about your class doing that by default)
If you want to make sure it doesn't resize, you can try turning off the flexible width and flexible height sections in the measurements tab in IB, and maybe also uncheck "auto-resizes subviews" on the UITextView's parent. Although none of these should be making a difference, turning them off may help diagnose the real problem.