I want to give the thumb image to NSSlider in swift - swift

I`m working on an editor app, where I need to add image in the slider thumb as I'm doing macOS development so unfortunately I'm not able to do that in macOS, there is available in UISlider.
this is what I have and I want to insert the thumb Image as give below

I find out the solution, I hope this will help you all.
I made a NSSliderCell class and override the function drawKnob.
class CustomSliderCell: NSSliderCell {
#IBInspectable var knobImage: NSImage?{
didSet{
if let knobImage = knobImage{
self.knobImage = knobImage
}
}}
override func drawKnob(_ knobRect: NSRect) {
var rect = knobRect
rect.size = NSSize(width: rect.width, height: rect.height - 4)
self.knobImage?.draw(in: rect)
}
}
just assign this class to you nsslider cell from your storyboard and change the image on your storyboard.
enter image description here
The Result is given Below :
[This is the resultant Image][2]
enter image description here

Related

How to remove the space between UISlider and Minimum Image

I would like to remove the space / offset at the beginning of the UISlider and join it with the custom minimum image that I have.
Is it possible to do it programmatically in Swift?
I have checked this, but it's old and it's for Objective-C.
well, I looked at the answer you tagged, and made something up for you on playground. Not sure if it works precisely for your case, but the key is to look into the methods of UISlider that you can override here
import UIKit
class SomeController: UIViewController {
let slider = MyCustomSlider()
//etc...
}
class MyCustomSlider: UISlider {
override func maximumValueImageRect(forBounds bounds: CGRect) -> CGRect {
var r = super.maximumValueImageRect(forBounds: bounds)
r.origin.x -= 3
return r
}
}

Using TVPosterImage in TVUIKit for tvOS 12

tvOS 12 has a new framework TVUIKit, which introduces the lockup views. The class I am interested in is TVPosterView, which is basically designed as such:
Swift 4.2
open class TVPosterView : TVLockupView { // One may use UIControl to implement it in iOS
public init(image: UIImage?)
open var image: UIImage? // default is nil
open var title: String?
open var subtitle: String?
}
In my storyboard I have added an UIView element, and rightly (at least I hope so. Yesterday the IB agent kept crashing!) changed its class to TVPosterView in Identity Inspector. then in my UIViewController I have defined:
#IBOutlet var aPosterView: TVPosterView!
override func viewDidLoad() {
super.viewDidLoad()
if let myIcon = UIImage(named: "icon.png") {
self.aPosterView = TVPosterView(image: myIcon)
self.aPosterView.title = "My Title"
self.aPosterView.subtitle = "A Sub Title"
}
else {
print("ERROR: I couldn't load the icon!")
}
}
}
It compiles without any warning. When I run it, it just shows the UIView white background, nothing changes. I did some tests trying to add an UIImageView, but they were all inconclusive (apart, off course, when I did set the image of UIImageView to self.aPosterView.image).
I am far from being an expert, I just started learning a couple of weeks ago. Any idea about what I am missing? I thought the concept was that once I initiated the class the framework was taking care of displaying the poster with the (optional) title and subtitles, also taking care of all nice animations!
Eventually I managed to make it working programmatically. I tested both TVPosterView and TVMonogramView (basically a round button). The following code works with Xcode 10 beta 5, on tvOS 12 beta 5:
Swift 4.2
import UIKit
import TVUIKit
class SecondViewController: UIViewController {
var myPoster = TVPosterView()
var myMonogram = TVMonogramView()
override func viewDidLoad() {
super.viewDidLoad()
myPoster.frame = CGRect(x: 100, y: 100, width: 550, height: 625)
myPoster.image = UIImage(named: "image1.png")
myPoster.imageView.masksFocusEffectToContents = true
myPoster.title = "Poster"
myPoster.subtitle = "This is the poster subtitle"
self.view.addSubview(myPoster)
var myName = PersonNameComponents()
myName.givenName = "Michele"
myName.familyName = "Dall'Agata"
myMonogram.frame = CGRect(x: 700, y: 100, width: 500, height: 475)
myMonogram.image = UIImage(named: "image2.png")
myMonogram.title = "Monogram"
myMonogram.subtitle = "This is the Monogram subtitle"
myMonogram.personNameComponents = myName
self.view.addSubview(myMonogram)
print(myMonogram.personNameComponents)
}
}
I didn't manage to scale the poster's image with scaleAspectFit, though. Also my first image was rounded with a transparency, and only choosing a frame size that perfectly fit the squared image plus the titles (so no aspect fit needed), the glowing effect became transparent on the corners. Otherwise the whole image was opaque, using its own (quite small) rounded corners.

Colored text background with rounded corners

I'm currently working on an iOS application in Swift, and I need to achieve the text effect shown in the attached picture.
I have a label which displays some text wrote by the user, and I need to make the text background corners (not the label background) rounded.
Is there a way to do that?
I'd search the web and Stackoverflow but with no luck.
Thank you.
Here is some code that could help you. The result I got is quite similar to what you want.
class MyTextView: UITextView {
let textViewPadding: CGFloat = 7.0
override func draw(_ rect: CGRect) {
self.layoutManager.enumerateLineFragments(forGlyphRange: NSMakeRange(0, self.text.count)) { (rect, usedRect, textContainer, glyphRange, Bool) in
let rect = CGRect(x: usedRect.origin.x, y: usedRect.origin.y + self.textViewPadding, width: usedRect.size.width, height: usedRect.size.height*1.2)
let rectanglePath = UIBezierPath(roundedRect: rect, cornerRadius: 3)
UIColor.red.setFill()
rectanglePath.fill()
}
}
}
Xavier solution works great.
If you want update background in real-time, use: textView.setNeedsDisplay()

Programmatically place partial image over another in UIView using Swift 3

I just started working on Swift last week and i need a suggestion if the following approach is right ways of laying partial image on top of another image.
I have a UIView in which I am creating 3 images programmatically. Left arrow image, middle mobile image and right arrow image as shown below. Can I partially place arrow images 50% on the mobile image?
I have tried:
func setupUI(){
let mobileImage = UIImage(named: "mobile")
let arrowImage = UIImage(named: "arrow")
middleView = UIImageView(frame: CGRect(x: arrowImage!.size.width/2, y:0 , width:mobileImage!.size.width, height:mobileImage!.size.height))
middleView.image = mobileImage
middleView.layer.borderWidth = 1.0
middleView.layer.cornerRadius = 5.0
self.addSubview(middleView)
let yForArrow = mobileImage!.size.height - arrowImage!.size.height
leftArrow = UIImageView(frame: CGRect(x: 0, y:yForArrow, width:arrowImage!.size.width, height:arrowImage!.size.height))
leftArrow.image = arrowImage
self.addSubview(leftArrow)
let rightArrowX = mobileImage!.size.width
rightView = UIImageView(frame: CGRect(x: rightArrowX, y:yForArrow, width:arrowImage!.size.width, height:arrowImage!.size.height))
rightView.image = arrowImage
self.addSubview(rightView)
}
*At start it was not working, as i forgot to add setupUI() in init method. As shown in answer bellow.
Is setting frame correct way of doing it OR i should be using constraints?
To me it looks bad approach as i am hard coding the numbers in CGRect.
*This image is created in MS paint to show what it should look on iPhone.
I found the problem i missed adding setupUI() in init method.
SetupUI programatically adds images on UIView. As it was missing so no image was appearing in the iPhone simulator.
override init(frame: CGRect) {
super.init(frame: frame)
setupUI() // Code to add images in UIView
}

Show a GIF image using NSImageView

I want to show an animated gif image in my cocoa application.
I dragged the gif into Assets.xcassets in XCode. I was hoping that NSImageView can show a gif out of the box, so I tried the following code.
let imageView = NSImageView(frame: NSRect(x: 0, y: 0, width: 512, height: 512))
imageView.canDrawSubviewsIntoLayer = true
imageView.imageScaling = .ScaleNone
imageView.animates = true
imageView.image = NSImage(named: "loading-animation")
window.contentView?.addSubview(imageView)
The image does not show up. The above code works with a png image. How do I get this to work?
For me it works only if there is nothing set in the Attribute Inspector and with the code like that (hope, it will be useful for someone):
class FirstViewController: NSViewController {
#IBOutlet weak var imgImage1: NSImageView!
override func viewDidLoad() {
super.viewDidLoad()
let imgImage1 = NSImageView(frame: NSRect(x: 407, y: 474, width: 92, height: 74))
imgImage1.canDrawSubviewsIntoLayer = true
imgImage1.imageScaling = .scaleProportionallyDown
imgImage1.animates = true
imgImage1.image = NSImage(named: "mygif")
self.view.addSubview(imgImage1)
}
Enjoy :)
Xcode 12
Just add your gif file in your project (Not in Assets)
Add an NSImageView in your view controller.
Set the image name in the storyboard as your gif file name.
Tap on Animates property.
Answering my own question.
I was able to make a gif display in my app when I get the gif from a URL e.g.
imageView.image = NSImage(byReferencingURL: yourgifurl)
So, I figured there was something wrong with the way I copied the image into my project. Instead of putting the image in Assets.xcassets, I put it with the rest of source code and the gif shows up :) (but the animation speed seems to be very slow)
Suppose you have an animated gif resource named universe in an Assets.xcassets and NSImageView connected by IBOutlet named target:
...
#IBOutlet weak var target: NSImageView!
...
func applicationDidFinishLaunching(_ aNotification: Notification) {
...
target.animates = true
target.image = NSImage(data: NSDataAsset(name: "universe")!.data)
...
}
NOTE:
target.image = NSImage(named: "universe") // WILL NOT WORK!