Is there any way to convert a UIColor to a string?
For example,
var color = UIColor.purpleColor()
var colorString : String = //Purple
If you can help me it would be greatly appreciated.
Thanks
You can get color description
var color = UIColor.purpleColor()
var colorString : String = color.description
println(colorString)
var cgColorRef = UIColor.purpleColor().CGColor
var strColor = CIColor(CGColor: cgColorRef).stringRepresentation()
println(strColor)
Reference from : How to convert UIColor value to a named color string?
Related
Cannot draw a String right justified in NSView ( MacOs )
Here is my code, which crashes with :
-[__SwiftValue lineBreakMode]: unrecognized selector sent to instance 0x600003408750
let attrs = [NSAttributedString.Key.foregroundColor:NSColor.white,
NSAttributedString.Key.paragraphStyle:NSTextAlignment.right
] as [NSAttributedString.Key : Any]
let absolutPoint = NSPoint(x: 0.0, y: 0.0)
var convertedPt = convertPoint(dataPoint:se,scaleX:scaleX,scaleY:scaleY)
convertedPt.x = absolutPoint.x
let scalaLabel = NSString(format:"%3.0f",se.y)
let place = NSMakeRect(convertedPt.x, convertedPt.y, 80.0, 12.0)
scalaLabel.draw(in:place ,withAttributes:attrs )
If I do not set paragraphStyle in attrs, text is drawn correctly. But not right-justified.
Any idea what´s wrong with my code ?
NSAttributedString.Key.paragraphStyle:NSTextAlignment.right
That's not valid, that's why it's crashing.
The value should be a NSParagraphStyle, not a NSTextAlignment as stated in the documentation:
The value of this attribute is an NSParagraphStyle object. Use this
attribute to apply multiple attributes to a range of text. If you do
not specify this attribute, the string uses the default paragraph
attributes, as returned by the default method of NSParagraphStyle.
let paragraphStyle = NSMutableParagraphStyle()
paragraphStyle.alignment = .right
let attrs = [NSAttributedString.Key.foregroundColor:NSColor.white,
NSAttributedString.Key.paragraphStyle: paragraphStyle
] as [NSAttributedString.Key : Any]
Side note, if you state the type before, it might be shorten:
let attrs: NSAttributedString.Key: Any] = [foregroundColor: NSColor.white,
paragraphStyle: paragraphStyle]
Hi i am trying to get the screen size of device
I have tried this code:
layout.Width;
layout.Height;
but the width and the height are returning 0,0
anybody knows how to get the screen size?
Xamarin "DetectScreenSize" Recipe:
var metrics = Resources.DisplayMetrics;
var widthInDp = ConvertPixelsToDp(metrics.WidthPixels);
var heightInDp = ConvertPixelsToDp(metrics.HeightPixels);
private int ConvertPixelsToDp(float pixelValue)
{
var dp = (int) ((pixelValue)/Resources.DisplayMetrics.Density);
return dp;
}
re: https://github.com/xamarin/recipes/blob/master/Recipes/android/resources/device_specific/detect_screen_size/DetectScreenSize/Activity1.cs
using Xamarin.Essentials
var mainDisplayInfo = DeviceDisplay.MainDisplayInfo;
var realHeightInPx = mainDisplayInfo.Height;
I have a NSViewController and a variable num. I want to change the size of the window dynamically according to that variable. Is there any way to do that in swift?
Let's say your window has an IBOutlet named "window", and your dynamic number is named "myDynamicNumber":
func resize() {
var windowFrame = window.frame
let oldWidth = windowFrame.size.width
let oldHeight = windowFrame.size.height
let toAdd = CGFloat(myDynamicNumber)
let newWidth = oldWidth + toAdd
let newHeight = oldHeight + toAdd
windowFrame.size = NSMakeSize(newWidth, newHeight)
window.setFrame(windowFrame, display: true)
}
In Swift 3 to resize the window you use setFrame.
An example from the ViewController:
func resizeWin(size:(CGFloat,CGFloat)){
self.view.window?.setFrame(NSRect(x:0,y:0,width:size.0,height:size.1), display: true)
}
I needed to toggle viewing a text view so I overlaid the window an invisible view - hideRect just short of the text view; in this way I can resize to the smaller (hideRect) and restore later to the original size - origRect. Hide and original rect captured at viewDidLoad(). Swift 3/Xcode 8.3.3
// class global contants
let kTitleUtility = 16
let kTitleNormal = 22
#IBOutlet var hideView: NSView!
var hideRect: NSRect?
var origRect: NSRect?
#IBAction func toggleContent(_ sender: Any) {
// Toggle content visibility
if let window = self.view.window {
let oldSize = window.contentView?.bounds.size
var frame = window.frame
if toggleButton.state == NSOffState {
frame.origin.y += ((oldSize?.height)! - (hideRect?.size.height)!)
window.setFrameOrigin(frame.origin)
window.setContentSize((hideRect?.size)!)
window.showsResizeIndicator = false
window.minSize = NSMakeSize((hideRect?.size.width)!,(hideRect?.size.height)!+CGFloat(kTitleNormal))
creditScroll.isHidden = true
}
else
{
let hugeSize = NSMakeSize(CGFloat(Float.greatestFiniteMagnitude), CGFloat(Float.greatestFiniteMagnitude))
frame.origin.y += ((oldSize?.height)! - (origRect?.size.height)!)
window.setFrameOrigin(frame.origin)
window.setContentSize((origRect?.size)!)
window.showsResizeIndicator = true
window.minSize = NSMakeSize((origRect?.size.width)!,(origRect?.size.height)!+CGFloat(kTitleNormal))
window.maxSize = hugeSize
creditScroll.isHidden = false
}
}
}
This also preserved the widow's visual origin, and sizing minimum.
I'm building a swift game and a need to set up some images.
My code works with string or integer:
for var i = 0; i < globalCurrentMembers.count; ++i {
var MembersDefaultName = NSUserDefaults.standardUserDefaults()
MembersDefaultName.setValue(globalCurrentMembers[i].name, forKey: "globalCurrentMembersName\(i)")
MembersDefaultName.synchronize()
}
But I have an error for an image
for var i = 0; i < globalCurrentMembers.count; ++i {
var MembersDefaultImage = NSUserDefaults.standardUserDefaults()
MembersDefaultImage.setValue(globalCurrentMembers[i].image,
forKey: "globalCurrentMembersImage\(i)")
MembersDefaultImage.synchronize()
}
globalCurrentMembers is an array of Member which looks like that:
class Member {
var image = UIImage ()
var name = String ()
var progression = Int()
var round = Int()
var level = Int()
var imageProgression = [UIButton]()
func Init(){
image = UIImage(named: "default.png")!
name = "default"
progression = 0
round = 0
level = 0
}
}
So please can you tell me how doing that.
UIImage does not implement NSCoding protocol, you need to convert UIImage to NSData first.
Change your code to the following:
var imageData = UIImagePNGRepresentation(globalCurrentMembers[i].image)
var myEncodedImageData = NSKeyedArchiver.archivedDataWithRootObject(imageData)
MembersDefaultImage.setObject(myEncodedImageData, forKey: "globalCurrentMembersImage\(i)")
Of course storing image to NSUserDefaults is not the best practice, you should store your image in some file directory.
You can't store images in NSUserDefaults. Have you considered storing just the path to the image, or the name of the image, instead? That would be enormously more efficient, and also work.
I have what should be a fairly simple question here. Basically I'm trying to move an object (a UIImageView) from a point A (where it is set in the storyboard) to a point B which I define programatically.
My first pass through this resulted in this code
UIView.animateWithDuration(0.75, delay: 0.5, options: UIViewAnimationOptions.CurveLinear, animations: {
self.signInButton.alpha = 1
self.signInButton.center.y = 10
}, completion: nil)
However, what this code does is to basically move the button offcenter and then back to its original location.
I then looked into QuartzCore to help me, but everything is in Objective-C. I have this method:
func moveImage(view: UIImageView){
var toPoint: CGPoint = CGPointMake(0.0, -10.0)
var fromPoint : CGPoint = CGPointZero
var movement = CABasicAnimation(keyPath: "movement")
movement.additive = true
movement.fromValue = fromPoint
movement.toValue = toPoint
movement.duration = 0.3
view.layer.addAnimation(movement, forKey: "move")
}
However the problem here is that movement.fromValue cannot accept a CGPoint. I know that in objective C there was a function that converted a CGPoint to a NSValue, however this function seems to be deprecated out of Swift and I can't find the other way of doing this.
Therefore my question is either, how do I convert CGPoint to NSValue to make my moveImage() function work, or is there a better way to move an object from point A to point B?
Thanks!
I'm looking at this question Animate UIImage in UIImageView Up & Down (Like it's hovering) Loop
Use NSValue(CGPoint: cgpiont) instead of NSValue.valueWithCGPoint(<#point: CGPoint#>) which is deprecated in swift. NSValue(CGPoint: cgpiont) is constructor given for that which can be used to convert CGPoint to NSValue
in swift.Following code will work
func moveImage(view: UIImageView){
var toPoint: CGPoint = CGPointMake(0.0, -10.0)
var fromPoint : CGPoint = CGPointZero
var movement = CABasicAnimation(keyPath: "movement")
movement.additive = true
movement.fromValue = NSValue(CGPoint: fromPoint)
movement.toValue = NSValue(CGPoint: toPoint)
movement.duration = 0.3
view.layer.addAnimation(movement, forKey: "move")
}
SWIFT 3 UPDATES
func moveImageView(imgView: UIImageView){
var toPoint:CGPoint = CGPoint(x: 0.0, y: -10.0)
var fromPoint:CGPoint = CGPoint.zero
var movement = CABasicAnimation(keyPath: "movement")
movement.isAdditive = true
movement.fromValue = NSValue(cgPoint: fromPoint)
movement.toValue = NSValue(cgPoint: toPoint)
movement.duration = 0.3
imgView.layer.add(movement, forKey: "move")
}