How to make `NSPopUpButton` with `NSControl.ControlSize.mini` programmatically? - appkit

I tried setting controlSize, and it didn't work.
I tried setting NSMenuItem.attributedTitle, and it didn't work.
Setting NSMenuItem.attributedTitle made text smaller, but didn't make checkmarks smaller. Therefore result looks broken.
Here's code to reproduce.
let testFont1 = NSFont.menuFont(ofSize: NSFont.systemFontSize(for: .mini))
let menu1 = NSMenu()
menu1.font = testFont1
menu1.addItem(withTitle: "AAA", action: nil, keyEquivalent: "")
let item2 = NSMenuItem()
item2.attributedTitle = NSAttributedString(string: "BBB", attributes: [.font: testFont1])
menu1.addItem(item2)
let popup1 = NSPopUpButton()
popup1.menu = menu1
popup1.controlSize = .mini
popup1.sizeToFit()
window.contentView?.addSubview(popup1)
How to make a mini-sized pop-up button properly with no NIB and only code?

Just assign mini-size font to the NSPopUpButton instance.
popup1.font = NSFont.systemFont(ofSize: NSFont.systemFontSize(for: .mini))
Nothing else is really required. The code can be shorten like this.
let popup1 = NSPopUpButton()
popup1.controlSize = .mini
popup1.font = NSFont.systemFont(ofSize: NSFont.systemFontSize(for: .mini))
popup1.addItem(withTitle: "AAA")
popup1.addItem(withTitle: "BBB")
popup1.sizeToFit()
window.contentView?.addSubview(popup1)
This applies equally to any other control-based classes. You need to set both controlSize and font. It seems controlSize controls only graphical appearance part and font controls text renderings.

Related

iOS UIKit Multiline label and UITextView give incorrect visual height width

Im trying to setup a multiline set of views in a UIScrollView and ultimately a UIStackView. I found out I need to dynamically set the scrollviews height to the content to make it work. So when I went to debug why the values never worked I found that the labelType.frame.size is giving me a massive width or like the text was never wrapped, and the height is the height of the devices view.
If I change around the anchors for the content view I can make the text never line break fueling what I see as the CGSize numbers corresponding to the visual size
While the parent view does not even get a size which is why the default UIScrollView won't even work
content frame (0.0, 0.0)
uitextType (6791.0, 478.0)
labelType (6781.0, 460.5)
In this example I am testing both UITextView and UILabel, I know UITextView is not recommended but testing anyway, it has the same behavior with or without
Worse case I guess I can mix SwiftUI into the Uiview and carry on with work.
override func viewDidLoad() {
super.viewDidLoad()
}
override func loadView() {
super.loadView()
let content = UIView()
content.backgroundColor = UIColor.green
content.translatesAutoresizingMaskIntoConstraints = false
self.view.addSubview(content)
let constraints2 = [
content.topAnchor.constraint(equalTo: self.view.topAnchor),
content.leadingAnchor.constraint(equalTo: self.view.leadingAnchor),
content.heightAnchor.constraint(equalTo: self.view.heightAnchor, constant: 0),
content.widthAnchor.constraint(equalTo: self.view.widthAnchor),
// content.bottomAnchor.constraint(equalTo: self.view.bottomAnchor),
// content.trailingAnchor.constraint(equalTo: self.view.trailingAnchor),
]
NSLayoutConstraint.activate(constraints2)
let uitextType = UITextView()
uitextType.backgroundColor = .clear
uitextType.text = lotatext
uitextType.isScrollEnabled = false
uitextType.translatesAutoresizingMaskIntoConstraints = false
content.addSubview(uitextType)
uitextType.topAnchor.constraint(equalTo: content.topAnchor).isActive = true
uitextType.leadingAnchor.constraint(equalTo: content.leadingAnchor).isActive = true
uitextType.heightAnchor.constraint(equalTo: content.heightAnchor).isActive = true
uitextType.widthAnchor.constraint(equalTo: content.widthAnchor).isActive = true // -40 2x?! lame
uitextType.font = UIFont.preferredFont(forTextStyle: .body)
uitextType.contentInsetAdjustmentBehavior = .automatic
uitextType.sizeToFit()
uitextType.contentMode = .topLeft
let labelType = UILabel()
labelType.backgroundColor = .clear
labelType.text = lotatext
labelType.numberOfLines = 0
labelType.lineBreakMode = .byWordWrapping
labelType.translatesAutoresizingMaskIntoConstraints = false
content.addSubview(labelType)
labelType.topAnchor.constraint(equalTo: content.topAnchor).isActive = true
labelType.leadingAnchor.constraint(equalTo: content.leadingAnchor).isActive = true
labelType.heightAnchor.constraint(equalTo: content.heightAnchor).isActive = true
labelType.widthAnchor.constraint(equalTo: content.widthAnchor).isActive = true
labelType.font = UIFont.preferredFont(forTextStyle: .body)
labelType.sizeToFit()
print("content frame \(content.frame.size)")
print("uitextType \(uitextType.frame.size)")
print("labelType \(labelType.frame.size)")
}
// testing text
let lotatext = """
Every day is taco ipsum tuesday. It’s a wonderful morning for breakfast tacos. 50 cent tacos! I’ll take 30. CARNE ASADA!! BARBACOA!! I’d have to say, those tacos are on fleek. It’s long been rumored that the chupacabra is really just a crazed man who’s local taco shop went out of business. Does guac cost extra? BARBACOA!! TACOS!! It’s raining tacos, from out of the sky, tacos, don’t even ask why. CARNE ASADA!!
50 cent tacos! I’ll take 30. I’d have to say, those tacos are on fleek. Let’s do a beef and a chicken, and one with both. I’ve been following that taco truck around all day. Make it a double there pal. It’s long been rumored that the chupacabra is really just a crazed man who’s local taco shop went out of business. I think I’ve overdosed on tacos. Tacos Al pastor/De Adobada are made of thin pork steaks seasoned with adobo seasoning, then skewered and overlapped on one another on a vertical rotisserie cooked and flame-broiled as it spins.
... snipped extras paragraphs
"""

Adding different alignment and font size to different lines in one TextLabel in TableCell swift

so i'm trying to implement a simple english to farsi dictionary in iOS
i'd like to include both words in one table cell, problem is that english is L>R and farsi is R>L, also i'd like to make the farsi word a bit bigger.
I created an AttributedMutableString and I thought I put down all the correct values but it looks like there is a problem since it isn't rendering correctly.
code:
cell.textLabel?.numberOfLines = 0
var myString = "\(englishConvo[indexPath.row])\n\(farsiConvo[indexPath.row])"
var mutableString = NSMutableAttributedString()
var lenOfLang1 = englishConvo[indexPath.row].characters.count
var lenOfLang2 = farsiConvo[indexPath.row].characters.count
let increaseFontSize = UIFont(name: (cell.textLabel?.font.fontName)!, size: (cell.textLabel?.font?.pointSize)! + 5)
let paragraphStyle = NSMutableParagraphStyle()
paragraphStyle.alignment = NSTextAlignment.Right
mutableString = NSMutableAttributedString(string: myString)
mutableString.addAttribute(NSParagraphStyleAttributeName, value: paragraphStyle, range: NSRange(location: lenOfLang1 + 1, length: lenOfLang2))
mutableString.addAttribute(NSFontAttributeName, value: increaseFontSize!, range: NSRange(location: lenOfLang1 + 1, length: lenOfLang2))
cell.textLabel?.attributedText = mutableString
If i convert to a string using this code this is what I get
cell.textLabel?.text = String(mutableString)
Any thoughts / ideas would be super appreciated
Table cells already come with a layout that gives you two labels (text and detail), so why not just use it? Here, for example, in a language app of mine, I'm displaying a Latin word in the text label and an English translation in the detail label. You could easily do the same with Farsi and English.

Applying image to random object

I've created an NSArray with buttons and now I wish to apply an image to a randomly chosen button from that array;
let buttons:NSArray = [button1, button2, button3, button4, button5, button6, button7, button8, button9, button10, button11, button12]
let range: UInt32 = UInt32(buttons.count)
let randomNumber = Int(arc4random_uniform(range))
let buttonstring = buttons.objectAtIndex(randomNumber)
buttonstring.image() = UIImage(named: "code2")
This last piece of code is where I wish to apply the image to the randomly chosen button. But it has an error saying;
Cannot assign to value: function call returns immutable value
If buttonstring is a UIButton, you have to use something like this:
buttonstring.setImage(UIImage(named: "code2"), forState: .Normal)
Hope this helps.

NSButton won't let me set imagePosition property

I'm trying to build a menubar application in swift. I want to set an image and text for the status item. Evidently NSStatusItem has deprecated setting a custom view since 10.10, which is fine, since I am able to set an image and text on the status item's button. However, I'm unable to set the imagePosition property for some reason and so the text and the image overlap.
This is my code:
let statusItem = NSStatusBar.systemStatusBar().statusItemWithLength(-1)
func applicationDidFinishLaunching(aNotification: NSNotification) {
let icon = NSImage(named: "statusIcon")
icon!.setTemplate(true) // best for dark mode
statusItem.button!.image = icon
statusItem.button!.imagePosition = ImageLeft
statusItem.button!.title = "Hello, world"
statusItem.menu = menu;
}
The problem is that Xcode gives me an error on this line:
statusItem.button!.imagePosition = ImageLeft
It says "Use of unresolved identifier 'ImageLeft'", but from what I can tell from the documentation (https://developer.apple.com/library/mac/documentation/Cocoa/Reference/ApplicationKit/Classes/NSCell_Class/index.html#//apple_ref/c/tdef/NSCellImagePosition) that is the identifier I would want to use.
Can anyone help?
I figured it out. Evidently I have much to learn about Swift syntax.
This line allows it to work:
statusItem.button!.imagePosition = NSCellImagePosition.ImageLeft

iconview icon selection appearance

I work on a project using Gtk & Ruby. I want when one icon was selected, it shouldn't seem as a rectangle.
http://i.imgur.com/RIEBipq.png
It just seem icon bound was selected. How can supply this?
http://i.imgur.com/LzT4H3C.png
I use iconview like below:
iconview = Gtk::IconView.new
file_store = Gtk::ListStore.new(String, String, TrueClass, Gdk::Pixbuf)
iconview.model = file_store
iconview.text_column = COL_DISPLAY_NAME
iconview.pixbuf_column = COL_PIXBUF
How can I code for this appearance?

Resources