Display values outside of the piechart - swift

I am having trouble displaying the values outside of my piechart. Currently the values overlap, is there anyway to improve this? Please see image below.
chartDataSet.valueLinePart1Length = 0.5
chartDataSet.valueLinePart2Length = 1
//set.xValuePosition = .outsideSlice
chartDataSet.yValuePosition = .outsideSlice

func setupPiChart() {
// This will align label text to top right corner
let l = pieChartView.legend
l.horizontalAlignment = .left
l.verticalAlignment = .bottom
l.orientation = .horizontal
l.xEntrySpace = 10
l.yEntrySpace = 0
l.font = self.isPhone ? Typography.robotoRegular14 : Typography.robotoRegular18
self.pieChartView.entryLabelFont = Typography.robotoRegular14
self.pieChartView.drawHoleEnabled = false
self.pieChartView.drawEntryLabelsEnabled = false
self.pieChartView.notifyDataSetChanged()
}

Related

sendSubviewToBack is not working when an UIView is added to a custom UILabel

I am iterating values and add them to a container UIView.
for value in bla {
// Create a new label
let labelHashtag = UILabelBadge()
labelHashtag.backgroundColor = .white
labelHashtag.frame.size.width = labelHashtag.intrinsicContentSize.width + tagPadding
labelHashtag.frame.size.height = tagHeight
labelHashtag.layer.cornerRadius = labelHashtag.layer.frame.size.height / 2
labelHashtag.topInset = 2
labelHashtag.bottomInset = 2
labelHashtag.rightInset = 6
labelHashtag.leftInset = 6
labelHashtag.textAlignment = .center
// !!! Here I am trying to add an UIView and send it back !!!
let bla = UIView()
bla.frame = labelHashtag.bounds
bla.backgroundColor = .red
labelHashtag.addSubview(bla)
labelHashtag.sendSubviewToBack(bla)
Container.addSubview(labelHashtag)
}
It also does not work when I use insertSubview at: 0. It will stay on the top of my UILabel.
How can I add an UIView and send it back?
When you add subview on UILabel means that UILabel is the root and all subviews will on the front of root view.
From your question, there are two approaches to achieve that.
First one, create a root UIView and add your UILabel and UIView into
for value in bla {
let rootUIView = UIView()
// Create a new label
let labelHashtag = UILabelBadge()
labelHashtag.backgroundColor = .white
labelHashtag.frame.size.width = labelHashtag.intrinsicContentSize.width + tagPadding
labelHashtag.frame.size.height = tagHeight
labelHashtag.layer.cornerRadius = labelHashtag.layer.frame.size.height / 2
labelHashtag.topInset = 2
labelHashtag.bottomInset = 2
labelHashtag.rightInset = 6
labelHashtag.leftInset = 6
labelHashtag.textAlignment = .center
// !!! Here I am trying to add an UIView and send it back !!!
let backgroundView = UIView()
backgroundView.frame = labelHashtag.bounds
backgroundView.backgroundColor = .red
rootUIView.addSubview(labelHashtag)
rootUIView.addSubview(backgroundView)
rootUIView.sendSubviewToBack(backgroundView)
Container.addSubview(rootUIView)
}
Second one, make your UIView as a root and add your custom UILabel into it
for value in bla {
// Create a new label
let labelHashtag = UILabelBadge()
labelHashtag.backgroundColor = .white
labelHashtag.frame.size.width = labelHashtag.intrinsicContentSize.width + tagPadding
labelHashtag.frame.size.height = tagHeight
labelHashtag.layer.cornerRadius = labelHashtag.layer.frame.size.height / 2
labelHashtag.topInset = 2
labelHashtag.bottomInset = 2
labelHashtag.rightInset = 6
labelHashtag.leftInset = 6
labelHashtag.textAlignment = .center
// !!! Here I am trying to add an UIView and send it back !!!
let backgroundView = UIView()
backgroundView.frame = labelHashtag.bounds
backgroundView.backgroundColor = .red
backgroundView.addSubview(labelHashtag)
Container.addSubview(backgroundView)
}

Swift 4, UIStackViews Not Displaying

Strange bug(s) I'm encountering..
This code:
// Label 1
let textLabel1 = UILabel()
textLabel1.backgroundColor = UIColor.yellow
textLabel1.text = "Label1"
textLabel1.textAlignment = .center
// Label 2
let textLabel2 = UILabel()
textLabel2.backgroundColor = UIColor.blue
textLabel2.text = "Label2"
textLabel2.textAlignment = .center
// Label 3
let textLabel3 = UILabel()
textLabel3.backgroundColor = UIColor.green
textLabel3.text = "Label3"
textLabel3.textAlignment = .center
let h1StackView = UIStackView()
h1StackView.axis = UILayoutConstraintAxis.horizontal
h1StackView.distribution = .fillEqually
h1StackView.alignment = UIStackViewAlignment.fill
h1StackView.spacing = 0
h1StackView.addArrangedSubview(textLabel3)
h1StackView.addArrangedSubview(textLabel2)
self.view.addSubview(h1StackView)
let h2StackView = UIStackView()
h2StackView.axis = UILayoutConstraintAxis.horizontal
h2StackView.distribution = .fillEqually
h2StackView.alignment = UIStackViewAlignment.fill
h2StackView.spacing = 0
h2StackView.addArrangedSubview(textLabel3)
h2StackView.addArrangedSubview(textLabel1)
self.view.addSubview(h2StackView)
//Stack View
let stackView = UIStackView(frame: CGRect(x: 0, y: pos.VERT_STACK_Y, width: pos.SCREEN_WIDTH, height: pos.VERT_STACK_HEIGHT))
stackView.axis = UILayoutConstraintAxis.vertical
stackView.distribution = .fillEqually
stackView.alignment = UIStackViewAlignment.fill
stackView.spacing = 0
stackView.addArrangedSubview(h1StackView)
stackView.addArrangedSubview(h2StackView)
self.view.addSubview(stackView)
Gives this view:
Buggy Image 1
This is close to correct, but wrong because I'm not expecting that big blue bar, rather, half blue - half green.
It gets a little weirder though for me...
If I change just one character in the code (textLabel2 to textLabel1):
(This):
let h1StackView = UIStackView()
h1StackView.axis = UILayoutConstraintAxis.horizontal
h1StackView.distribution = .fillEqually
h1StackView.alignment = UIStackViewAlignment.fill
h1StackView.spacing = 0
h1StackView.addArrangedSubview(textLabel3)
h1StackView.addArrangedSubview(textLabel2)
self.view.addSubview(h1StackView)
(To this):
let h1StackView = UIStackView()
h1StackView.axis = UILayoutConstraintAxis.horizontal
h1StackView.distribution = .fillEqually
h1StackView.alignment = UIStackViewAlignment.fill
h1StackView.spacing = 0
h1StackView.addArrangedSubview(textLabel3)
h1StackView.addArrangedSubview(textLabel1)
self.view.addSubview(h1StackView)
Then I get this view:
Buggy Image 2
It seems there are two problems and I can't quite fix either.. any ideas? I'm hoping this is a me problem and not a Swift/Xcode one.
(P.S. Sorry not enough rep to embed images)
The main problem resides in the logic of that single instance can only be inside one view at a time , so first when you repeated adding lbl3 to both the horizontal stackViews , it appeared at the last one (stack2) , same when you repeated adding 1,3 , they only show inside horizontal stack 2 , so to repeat you have to create another object , also if you decided to create a main stack to hold the 2 horizontal stacks then comment these 2 lines
self.view.addSubview(h1StackView)
self.view.addSubview(h2StackView)
arrangedSubViews is a subset of all subviews —
From the docs on UIView
A parent view may contain any number of subviews but each subview has only one superview.
This is expanded on in addSubView(_:)
Views can have only one superview. If view already has a superview and that view is not the receiver, this method removes the previous superview before making the receiver its new superview.
So when you add textLabel3 to h2StackView in this line:
h2StackView.addArrangedSubview(textLabel3)
You're implicitly removing it from h1StackView

iOS-Charts - X Axis Labels Disappear when Scrolling

I've been working with iOS-Charts from Daniel Gindi and it is a GREAT Framework!
However, I've been struggling with this issue for a long time now. I'm using a BarChart and managed to display the correct Values # the Axis labels.
Now I Don't know why this happens but the labels seem to DISAPPEAR While I'm Scrolling...
Here is a simple example with 2 bars:
And now I Scroll a bit to the Right, and PUFFF! The label is Gone!
// HERE IS MY SETUP CODE:
func setupChartView() {
// Default Texts:
noDataText = ""
chartDescription?.text = ""
// xAxis:
xAxis.drawGridLinesEnabled = false
xAxis.drawAxisLineEnabled = false
xAxis.enabled = true
if (UIScreen.main.nativeBounds.height < iPhone6ScreenHeight) {
xAxis.yOffset = smallScreenYOffset
} else {
xAxis.yOffset = graphLabelYOffset
}
xAxis.labelPosition = .bottom
xAxis.labelFont = defaultTextFont!
xAxis.labelTextColor = .engieChartGrayColor()
xAxis.granularity = 1.0
// Left/Right Axis:
leftAxis.enabled = false
rightAxis.drawGridLinesEnabled = false
rightAxis.drawAxisLineEnabled = false
rightAxis.drawLabelsEnabled = false
rightAxis.drawZeroLineEnabled = false
// Other UI Vars:
dragEnabled = true
drawMarkers = false
drawBordersEnabled = false
clipValuesToContentEnabled = true
drawValueAboveBarEnabled = false
setScaleMinima(4.0, scaleY: 1.0)
legend.enabled = false
doubleTapToZoomEnabled = false
pinchZoomEnabled = false
resetZoom()
scaleXEnabled = false
scaleYEnabled = false
}
Can someone Explain to me why this is?
Thanks, I Really Appreciate the help...

Adding min/max images to NSSlider using Swift

I created a NSSlider programmatically and implemented it in a NSMenuItem:
let menuVolumeSliderItem = NSMenuItem()
let menuVolumeSlider = NSSlider()
menuVolumeSlider.sliderType = NSSliderType.linear
menuVolumeSlider.isEnabled = true
menuVolumeSlider.isContinuous = true
menuVolumeSlider.action = #selector(AppDelegate.doSomething)
menuVolumeSlider.minValue = 0.0
menuVolumeSlider.maxValue = 1.0
menuVolumeSlider.floatValue = 0.5
menuVolumeSlider.frame.size.width = menu.size.width
menuVolumeSlider.frame.size.height = 20.0
menuVolumeSliderItem.view = menuVolumeSlider
menu.addItem(menuVolumeSliderItem)
And essentially, it work’s like a charm:
But the result I want to achieve is a Slider with a min/max image on the left/right edge of the Slider like this one:
How can I achieve a NSMenuItem like that?
EDIT:
I’ve created a custom NSView:
let view = NSView()
view.frame.size.width = menu.size.width
view.frame.size.height = 25.0
... then created a NSImage ...
let img = NSImageView()
img.frame.size.width = 16.0
img.frame.size.height = 16.0
img.image = NSImage(named: "NSStatusAvailable")!
... and added menuVolumeSlider and img to my custom view:
view.addSubview(img)
view.addSubview(menuVolumeSlider)
menuVolumeSliderItem.view = view
menu.addItem(menuVolumeSliderItem)
... and it worked! But can anyone please give me a hint how to change the positions of the subViews?
Create a NSView that will include the slider and the two images. Then set menuVolumeSliderItem.view to be that NSView.

Diagonal focussing in tvOS using UIFocusGuide

I have 4 UIButtons in my view:
All four are UIButtons with two UIButtons on the left side and another two on the right side. Focus is going from the top button to the bottom button when I select down arrow. I want focus to navigate through all four buttons from top to bottom and again from bottom to top when I press the down arrow and up arrow.
I also want to change the background color of the UIBUtton when it is highlighted(Currently white is coming by default).
I tried below code to navigate the focus but I couldn't get what I wanted.
let focusGuide = UIFocusGuide()
view.addLayoutGuide(focusGuide!)
focusGuide!.rightAnchor.constraintEqualToAnchor(self.btnPlay.rightAnchor).active = true
focusGuide!.topAnchor.constraintEqualToAnchor(self.switchFirst.topAnchor).active = true
focusGuide!.widthAnchor.constraintEqualToAnchor(self.btnPlay.widthAnchor).active = true
focusGuide!.heightAnchor.constraintEqualToAnchor(self.switchFirst.heightAnchor).active = true
focusGuide!.preferredFocusedView = self.switchFirst
in viewdidLoad and
Below code in didUpdateFocusInContext
guard let nextFocusedView = context.nextFocusedView else { return }
switch nextFocusedView {
case self.switchFirst:
self.focusGuide!.preferredFocusedView = self.btnPlay
case self.btnPlay:
self.focusGuide!.preferredFocusedView = self.switchFirst
case self.switchAudioType:
self.focusGuide!.preferredFocusedView = self.btnAdd
case self.btnAddToWishList:
self.focusGuide!.preferredFocusedView = self.switchSecond
Well one simple approach would be to use 4 different focus guides. I've included a very basic image of where they would be. Focus Guide Placement:
// [1] in viewDidLoad
let firstFocusGuide = UIFocusGuide()
view.addLayoutGuide(firstFocusGuide)
firstFocusGuide.leftAnchor.constraintEqualToAnchor(self.btnPlay.leftAnchor).active = true
firstFocusGuide.topAnchor.constraintEqualToAnchor(self.btnPlay.bottomAnchor).active = true
firstFocusGuide.heightAnchor.constraintEqualToAnchor(self.btnPlay.heightAnchor).active = true
firstFocusGuide.widthAnchor.constraintEqualToAnchor(self.switchFirst.widthAnchor).active = true
firstFocusGuide.preferredFocusedView = self.switchFirst
let secondFocusGuide = UIFocusGuide()
view.addLayoutGuide(secondFocusGuide)
secondFocusGuide.rightAnchor.constraintEqualToAnchor(self.switchFirst.rightAnchor).active = true
secondFocusGuide.bottomAnchor.constraintEqualToAnchor(self.switchFirst.topAnchor).active = true
secondFocusGuide.heightAnchor.constraintEqualToAnchor(self.switchFirst.heightAnchor).active = true
secondFocusGuide.widthAnchor.constraintEqualToAnchor(self.switchFirst.widthAnchor).active = true
secondFocusGuide.preferredFocusedView = self.btnPlay
let thirdFocusGuide = UIFocusGuide()
view.addLayoutGuide(thirdFocusGuide)
thirdFocusGuide.leftAnchor.constraintEqualToAnchor(self.btnAdd.leftAnchor).active = true
thirdFocusGuide.bottomAnchor.constraintEqualToAnchor(self.btnAdd.topAnchor).active = true
thirdFocusGuide.heightAnchor.constraintEqualToAnchor(self.btnAdd.heightAnchor).active = true
thirdFocusGuide.widthAnchor.constraintEqualToAnchor(self.switchSecond.widthAnchor).active = true
thirdFocusGuide.preferredFocusedView = self.switchSecond
let fourthFocusGuide = UIFocusGuide()
view.addLayoutGuide(fourthFocusGuide)
fourthFocusGuide.rightAnchor.constraintEqualToAnchor(self.switchSecond.rightAnchor).active = true
fourthFocusGuide.topAnchor.constraintEqualToAnchor(self.switchSecond.bottomAnchor).active = true
fourthFocusGuide.heightAnchor.constraintEqualToAnchor(self.switchSecond.heightAnchor).active = true
fourthFocusGuide.widthAnchor.constraintEqualToAnchor(self.switchSecond.widthAnchor).active = true
fourthFocusGuide.preferredFocusedView = self.btnAdd
Please keep in mind, this will only allow for the Up & Down movements between the four buttons. Not to mention it is a little tedious...Hope that helps!