Why NSTextField with attributed string will layout automatically when I click the link text? - nsattributedstring

Environment: Xcode 10.2, macOS Mojave Version 10.14.5
I create an NSTextField with an attributed string like below:
let name = "Someone"
let link = "http://example.com/people/\(name)"
let attributedString = NSMutableAttributedString(string: name)
// link without underline
attributedString.addAttributes([.link : link, .underlineStyle : 0], range: NSRange(location: 0, length: name.count))
// bind UI
let label = NSTextField(frame: CGRect(x: 10, y: 10, width: 100, height: 30))
label.allowsEditingTextAttributes = true
label.isSelectable = true
label.attributedStringValue = attributedString
When the UI shows successfully, I click the link, the text area will be layout again and text shrink. Do I have missed something?
NSTextField's AttributedString Shrink

At last, I solved this problem by setting up the font of the attributed string. Code like as below works.
// link without underline
attributedString.addAttributes([.link : link,
.underlineStyle : 0,
.font: NSFont.systemFont(ofSize: 13)] // same as textField.

Related

Why doesn't my custom component update when I change properties with setAttribute?

I think I don't quite understand how components and entities work.
I'm using the box component that appears in the a-frame help (https://aframe.io/docs/1.3.0/introduction/writing-a-component.html#example-box-component), and I want to modify a property. When doing so, I thought that the update would be activated but it does nothing. In fact the oldData is always empty and never enters in the update option (except for the first time):
<script>
AFRAME.registerComponent("box", {
...
}
</script>
<a-scene> </a-scene>
<script>
const gScene = document.querySelector("a-scene");
var camara = document.querySelector("[camera]");
camara.setAttribute("position", { x: 0, y: 0, z: 10 });
var p00 = document.createElement("a-entity");
p00.setAttribute("box", { width: 1, height: 1, depth: 1, color: "#f00" });
gScene.appendChild(p00);
p00.setAttribute("height", 3);
p00.setAttribute("color", "#00f");
</script>
First part of the code plot a red box with 1x1x1 dimension. But when I change the height or color attribute, don't do anything. What I have to do to fire the update?
Thanks in advance
You need to update the box attribute - setAttribute("height", XXX) will trigger an update in the height component.
If color is in the box schema, then:
// this updates the color property of the box attribute,
// triggering "update" in the "box" component
p00.setAttribute("box", "color", "red")
// this update the height attribute,
// triggering "update" in the "height" component
p00.setAttribute("height", "10");

UIBarButtonItem not change image automatically when switching dark mode/light mode

I am trying to integrate dark mode support to my app. But when setting image icon for UIBarButtonItem, it seem only works when the first time UIBarButtonItem is shown, it is not changed when I switch between dark mode/light mode.
When using that image with other UIButton, it works fine.
So I wonder if I am missing something?
P/s: I have to use that trick for updating image:
let item: UIBarButtonItem = UIBarButtonItem()
let button: UIButton = UIButton(frame: CGRect(x: 0, y: 0, width: 26, height: 19))
button.setImage(UIImage(named: "hamburger"), for: .normal)
button.addTarget(self, action: action, for: .touchUpInside)
item.customView = button
Seems to be a bug in iOS, but one way around this is, to use traitCollection delegate method to set the correct image manually.
override func traitCollectionDidChange(_ previousTraitCollection: UITraitCollection?) {
super.traitCollectionDidChange(previousTraitCollection)
let imageAsset = UIImage(named: "YOUR_IMAGE_NAME")?.imageAsset
let resolvedImage = imageAsset?.image(with: traitCollection)
self.YOUR_BUTTON.image = resolvedImage
}

Firebase AuthUI iOS customisation

I am attempting to customise the colour scheme of the default Firebase AuthUI screens on iOS. I can change the NavigationBar text colour however I cannot not change the left LeftNavigationBarItem text properties or the main view background colour.
This is the code that I've used to change the main text properties on the NavigationBar:
let authVC = authUI!.authViewController()
authVC.navigationBar.barTintColor = UIColor.black
authVC.navigationBar.barStyle = .blackTranslucent
authVC.navigationBar.titleTextAttributes = [NSAttributedStringKey.foregroundColor: UIColor (displayP3Red: 255/255, green: 212/255, blue: 121/255, alpha: 1.0)]
I have tried the following code, a line at a time, to change the NavigationBarItem text properties:
authVC.navigationBar.backItem?.backBarButtonItem?.setTitleTextAttributes([NSAttributedStringKey.foregroundColor: UIColor (displayP3Red: 255/255, green: 212/255, blue:121/255, alpha: 1.0) ], for: .normal)
authVC.navigationItem.backBarButtonItem?.setTitleTextAttributes([NSAttributedStringKey.foregroundColor: UIColor (displayP3Red: 255/255, green: 212/255, blue: 121/255, alpha: 1.0) ], for: .normal)
I have tried this to change the view background properties, again a line at a time:
authVC.visibleViewController?.view.backgroundColor = UIColor.black
authVC.view.backgroundColor = UIColor.black
authVC.view.layer.backgroundColor = UIColor.black.cgColor
authVC.visibleViewController?.view.layer.backgroundColor = UIColor.black.cgColor
I'm using FirebaseUI (5.2.0), FirebaseAuth (5.0.3) & Xcode 9.4.1.
Any pointers will be appreciated.
Thanks
code below is how you would change the background color as scoobydoo pointed out, also I was able to modify my authViewController after I added the custom controller as the root like so
let authViewController = MyAuthPickerViewController(authUI: authUI)
nav = UINavigationController(rootViewController: authViewController)
import UIKit
import FirebaseUI
class MyAuthPickerViewController : FUIAuthPickerViewController {
override func viewDidLoad() {
super.viewDidLoad()
let scrollView = view.subviews[0]
scrollView.backgroundColor = .clear
let contentView = scrollView.subviews[0]
contentView.backgroundColor = .clear
let width = UIScreen.main.bounds.size.width
let height = UIScreen.main.bounds.size.height
let backgroundImage = UIImageView(frame: CGRect(x: 0, y: -1, width: width, height: height))
backgroundImage.image = UIImage(named: "switchStarback")
backgroundImage.alpha = 0.6
view.backgroundColor = UIColor.init(named: "MatteBlack")
backgroundImage.contentMode = UIView.ContentMode.scaleAspectFill
view.insertSubview(backgroundImage, at: 0)
}
}
This has been sorted by subclassing FUIAuthPickerViewController and setting the view.backgroundColor in viewDidLoad method

dynamic create checkbox as array size in swift

I Want to create a view with dynamic create checkbox i know how to create c checkbox dynamic but how to manage size of view?
Here is my Code:
var spacer: CGFloat = 23
for comments in fixer as NSArray {
var counter = ""
print(comments)
counter = comments as! String
let button = UIButton(frame:CGRect(x: 8, y: 0+spacer , width: 20, height: 20))
button.setTitle("hi", for: .normal)
self.assignfixerView.addSubview(button)
let label = UILabel(frame:CGRect(x: 40, y: 0+spacer , width: 80, height: 22))
label.text = counter
self.assignfixerView.addSubview(label)
spacer = spacer + 23
}
Here is my view :
in this user are not fix and i want status(label) and textfield position change and if i create this button than how's the selection process?
Please give me better /suggestion if any better controller than it will be ok
OR any dropdown with multiple selection and after selection all will be display in textfield
Thank You in Advanced!

Change position of a IBOutlet in Swift

So i have this button:
#IBOutlet var bouton: UIBarButtonItem!
And i want to change is position
But the buton doesn't seems to have a position property like for SKSpriteNode
So is there a way to change is position ?
If you want to programmatically change the position of any view, you can use this:
button.frame.origin = CGPoint(x: 10, y: 10)
You can't set the button with myButton.frame.origin if you are using autolayout. So you must add constraints. For example:
let buttonConstraintY = NSLayoutConstraint(item: youButton, attribute: NSLayoutAttribute.centerY, relatedBy: NSLayoutRelation.equal, toItem: view, attribute: NSLayoutAttribute.bottom, multiplier: 1, constant: 10)
self.view.addConstraint(buttonConstraintY)

Resources