Custom NSMenuItem with right-aligned detail text label - ios

What I want to implement
I am looking for a way to display a title label and a detail text label in a NSMenuItem.
Preferably it would look something like this:
The Title Label is left aligned with all the other NSMenuItem's
titles
The Detail Text Label is right-aligned
The Detail Text Label has a different text color than the title
Selection/Submenus etc. work as expected
What have I tried already
By reading the documentation I found the following possible implementations:
Create a custom NSView and set NSMenuItem.view
Use a default NSMenuItem and use a NSAttributedString
First I tried to use a custom NSView. However I could not get the NSMenuItem to size correctly in order to display all the available text. I guess some autoresizing masks do not work correctly but I am not sure. Also this way I would need to re-implement selection/arrow for the submenu, ...
Then I started to experiment with NSAttributedString. I calculate the title with the most characters and then pad the string with title += string.padding(toLength: maxTitleLength, withPad: " ", startingAt: 0). The NSAttributedString colors the title and the detail label differently. However this does not seem to work since the detail labels are not correctly aligned although the title is padded to the same length. I guess this makes sense since characters have different widths?
TL;DR - Question
So is there any other way to implement the desired design which I did not find? Do you have any advice for me on how to implement this?

Nevermind I found the answer myself. Actually it works for me now by using the view property of the NSMenuItem to set a custom view. This answer lead me to the right direction: Highlighting a NSMenuItem with a custom view?.

Related

Custom iOS keyboard - Custom font output

I am trying to create a custom keyboard using the app keyboard extension. I am happy with the layout but the output is depended on the UITextField's font.
Is there a way to force a different font (use special characters?) while using the keyboard ?
Thank you
It depends.
Text field (or any other view that draws text) uses 2 informations on how to show some text. One is the sequence of characters called String and the other one is how the string should be represented. The second one is then split it things like fonts, colors, line height, line breaking and wrapping...
So the keyboard alone is not enough to for instance present a certain part of word using different fonts. You need at least a bit of access to the item that represents the text. So if you have no access to your text field then the answer is; No, you can not fore a different font when using different keyboard.
If you do have the access then the answer should lie in NSAttributedString. It is a string you can assign to most items under attributedText. This class wraps your raw string and can add many properties to parts of text you want to change. That includes using a different font.
Another approach would be using HTML tags. Again you will need to process this using for instance NSAttributedString or display it with another element like web view.
I would try it with using NSAttributedString. Hook up to delegate and implement textField(: shouldChangeCharactersIn: replacementString:. The implementation itself may still not be easy though.

UIButton word wrap doesn't work when entering text?

I've got a UIButton, it's a simple segue to another page.
I've set Title to attributed and then selected word wrap. This works fine, the second word wraps down to the next line.
However, it is all left justified. When I select "Align Centre" (using the buttons just under the "Title", the word wrap no longer works and simply runs .... so you can't see it all. (e.g. "next pa" instead of "next page")
Am I missing something here? It seems like such a trivial thing to do! There's an old answer here can't get word wrap to work on UIButton but it's both old and uses code - surely you don't need code to centre the button text if you want to word wrap it to 2 lines!?
I've set Title to attributed and then selected word wrap. This works fine, the second word wraps down to the next line. However, it is all left justified.
Once you've decided to use an attributed string, you must do everything with the attributed string. So give your attributed string a paragraph style that centers the text.
let para = NSMutableParagraphStyle()
para.alignment = .center
para.lineBreakMode = .byWordWrapping
let s = NSAttributedString(
string: "Hello World", attributes: [.paragraphStyle : para])
self.button.setAttributedTitle(s, for: .normal)
You will also need to set the button's title label to allow multiple lines.
self.button.titleLabel?.numberOfLines = 0
Result:
surely you don't need code to centre the button text if you want to word wrap it to 2 lines!?
Not to center it, no; you can set the centering in the storyboard. So you could eliminate the first batch of code and configure it in the storyboard. But you must use code to turn the title label into a multiline label:
self.button.titleLabel?.numberOfLines = 0
There is no way to do that in the storyboard, because you have no access to the title label there.
I've just been playing round with this and I've found it works if you set it to 'character wrap' rather than 'word wrap' once you've selected centre alignment.
If anyone has a better solution please add it, as I guess this might have issues if you slightly change the width etc when using auto layout for different screen sizes etc if you want it to adapt its width so this might not be the best solution but it does work

iOS, textView half editable

I need help in string formatting in iOS UITextView. I have a textView, for now it`s editable. Now I want to add a few strings. they should be like header and user must be able to type only under it, and when he press ENTER or DELETE line sting should change their position, so at the end it should look like
Header 1(with color not editable)
/* user can type here
*/
Header 2 (with color not editable)
/* user can type here
*/
I think i need to use delegate and attributed string or something,
but I can`t figure it out. HELP please
The best way to achieve this is to make two labels and two text views on top of a scroll view. like
The heights of two text views can by dynamic depending on the text length. And in this case you can avoid endless IF conditions as you would need to put everything in a single Text view.
The advantage of this approach is the label text format will be very easy to set, you can simply set the attributed string in the storyboard.

Getting view with liquid buttons

Consider that we have a formula: buttona texta buttonb textb buttonc textc buttond text. Fixed number of buttons (each of which has different length of course) and possibly dynamic number of text labels.
What is the most easiest way to get a UIView with liquid buttons as well as text labels based on that? Please, see an example.
Of course we can find a correlation with html displaying. Perhaps we have existing components to do that?
You may achieve this using NSAttributedString class. Check this link for examples.

How to do word wrap in a DevExpress TcxGrid?

I've got a long line of text that would be a lot easier to view if it would just word wrap around multiple lines, but I can't seem to find the option for it. Does anyone know how to enable word-wrap functionality?
Set the OptionsView.CellAutoHeight of the view you're working on?
From the help: "If the widths of the View’s columns are insufficient to display their entire content, then text clipping occurs. Use the CellAutoHeight property to prevent this. If this property value is True, then the cell content is displayed in multiple lines where necessary."

Resources