NSSegmentedControl with bold, italic, and underlined segment - cocoa

I'm trying to build a segmented control much like the Bold/Italic/Underline control in TextEdit.
Is there a way to set a custom attributed string for each segment? [set attributedStringValue:] on the cell had no effect (there's no way to set per segment anyway).
If I have to resort to custom drawing, how can I reproduce the inset/blue highlight in the selected state?
Example:

I think these are all icons you need to pre-create and set in the segmented control. Custom drawing would be another option, but I think simply using 3 icons (plus their hires variants) is more flexible.
For custom drawing you'd use an NSAttributedString where you set font and color. Draw it twice, once in white with one pixel vertical offset and and once in blue/black.

Related

XCODE 11, SWIFT 5. Drawing Lines, Boxes etc

I have recently switched to Xcode 11 and Swift 5. Also switched my entire project from UISwift to Storyboard as I read it has many advantages.
Now I’m finding disadvantages, I can’t draw boxes/lines on my storyboard, I can do it using the code but that creates further problems with making changes to my project in storyboard and element constrains.
Does anyone know if it’s possible to draw background boxes/lines between text? In some instances I have 6 labels that would all go under one box, so I can’t use label background feature.
See the online example below, of grey boxes
Now I’m finding disadvantages, I can’t draw boxes/lines on my storyboard
The storyboard editor isn't a drawing program -- it's mainly for laying out views and connecting them to each other and to other objects e.g. view controllers. If you're trying to use it to draw lines and boxes, you're barking up the wrong tree.
The storyboard editor does let you configure the views you create, so for example you can set the image displayed by a UIImageView, or the text displayed in a label, or the background color of any view. The gray boxes in your example are easy to do by just setting the background color of a view to gray. Or better, use a table to display those views, and programmatically set the background color of the cells depending on their row.
Does anyone know if it’s possible to draw background boxes/lines between text?
There are some hacky options. For example, you could very easily create a view class that draws a horizontal or vertical line through it's center point, or a view class that draws a line around its perimeter for a box. (You can actually use key-value coding to set properties on the view's layer to do this even without creating a subclass, but it's not something you want to have to do every time you need a box.) Those are fine for occasional use, but if you have any complex drawing to do, it's probably time to write a view class that draws the necessary content in code.
In some instances I have 6 labels that would all go under one box, so I can’t use label background feature.
Those 6 labels should all be contained inside another view, so you'd just set the background color for the view that contains them. Again, from your example, it looks like those are rows in a table. Each row in a table is its own view (or "cell"), and it's easy to set the background color in the same code that configures the rest of the subviews in that cell.

two labels horizontally in tableview?

I would like to have two labels in my tableview.
but the label resize option is disabled.
How to have the two labels horizontally?
Select UIlabel instead of the cell and then you can set Auto resize like follow.
You may need AutoLayout and Custom Tableview Call. Use two different labels, e.g. one for username (left aligned text) and another for garbage text (right aligned text).
Now, set Content Compression Resistance Priority for one of both label, according to your requirement for data visibility when there is long texts in user name.
Look at these snapshots:
Labels with default content compression resistance priority
I changed content compression resistance priority for label blablabla blablabla, from 750 to 749.
Result is:
For more details see Apple document: Setting Content-Hugging and Compression-Resistance Priorities
Drag a horizontal UIStackView from object library and set it's constraints like this inside the cell
//
drag 2 labels inside it and make the distribution .FillEqually
//
Are you sure you have selected Label?
If you know about Layout constraints, it will be easier than this auto resizer.
Label 1==> Set Leading, Top & Bottom constraints to SuperView. Set Trailing constraint to Label2.
Label2==> Set Trailing, Top & Bottom constraints to SuperView. Set Trailing constraint to Label2.
Now, after this, it will show Red error arrow. Now, you have to decide which Lable width is a priority. After deciding, select that Lable and set Horizontal Content Hugging to 251(High) and Comprehensive to 751(High). Also, change lower priority label Content Hugging and Comprehensive to 249 and 749 respectively. Now, Red error arrow will not be shown and in the cell it will show all text in both labels without and cut.
if you want a easy and quick fix for it, implement a stack view inside the tableview cell. You can change the stackview as you want and also you dont have to worry about applying constraints.

How to resize between 3.5" and 4" screens (dynamic constraints in autolayout?)

I have one view filling the screen with a background image. Other views (text fields) are in exact positions (the background image includes the text field background images). When I change from 3.5" screen to 4", the text fields don't change in the same way that the background resizes. The bg image simply resizes to fill the screen, but the text fields jump out of alignment.
Is there a way to have two sets of constraints, one for each screen size? or is there a way to have views resize proportionally to another view?
EDIT:
Is there a way to have two sets of constraints, one for each screen
size?
Yes, by adding constraints programatically and checking [[UIScreen mainScreen] bounds] to get the screen size.
or is there a way to have views resize proportionally to another view?
Yes, you could set this up in interface builder. But it will be hard to manage, I would manage the constraints manually in code since you are using a custom background image the textviews need to position exactly
You're going to struggle to get these things to line up properly with the text view backgrounds being part of the background image.
You should amend your image assets and use the background property of UITextField to have an actual background image, and remove the boxes from your main background image. The icons could be separate images as well.
Failing that, it would make more sense to have the image stretch underneath the text boxes rather than on top.
Your layout is doing what it should do based on your description, but the image isn't stretching in the right way. In your screenshots, username is always the same distance from the top, and the others are the same distance from the bottom, but that isn't how image stretching works. I don't know how you've set it up but it would make sense to have a single image the size of the 4 inch screen, which has the bottom cut off for 3.5 inch devices, and constrain everything from the top.

Glow around Button

How can I draw a slight white 'glow' around a button or label in 10.5 and later? I have seen some apps do it, but I am still confused how I should do this.
See NSShadow. You'd create and set a shadow (saving your graphics context beforehand), then draw the basic shape of your button, unset it (by restoring your graphics context), then continue drawing as usual.
In the case of a ready-made control like NSButton, you will need to subclass and override its cell drawing (and possibly make the host NSButton control itself a bit larger to accommodate the larger area needed to encompass the "glow" of the cell).
You might be able to avoid this with a label by setting its font shadow, but I don't think IB lets you do this, so you'd programmatically give the label an attributed string (via its -setAttributedString: method). The attributes would include the NSShadow (configured as desired) as the NSShadowAttributeName.

Add shadow (recessed text effect) to Cocoa label without degrading text rendering quality

I'd like to create statusbar with text effect like in Safari or iTunes, i.e. recessed text.
However, if I simply add shadow in Interface Builder using Core Animation panel, OS X's worst text rendering kicks in:
What's the trick to get recessed text on a label and keep proper subpixel rendering?
There is a built-in way to do this:
[[yourTextField cell] setBackgroundStyle:NSBackgroundStyleRaised];
It's a cheap old trick: You draw the text in white at an offset and then draw the black text on top of it.
There is a hook for shadows in the text-drawing system, NSAttributedString's NSShadowAttributeName. But testing this out, it appears to kill the subpixel antialiasing as well.

Resources