Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save Alfian878787/4af4ada137f86993f41b51bcae834d5f to your computer and use it in GitHub Desktop.
Save Alfian878787/4af4ada137f86993f41b51bcae834d5f to your computer and use it in GitHub Desktop.
import UIKit
// 1. Write a function that takes no arguments and returns a view that is the size of the screen and is colored red. The function should return an AnyObject instead of a UIView, since a UIView cannot pass the C/C++ boundary.
func gimmeUIView() -> AnyObject {
let newView = UIView(frame: UIScreen.mainScreen().bounds)
newView.backgroundColor = .redColor()
return newView
}
//2. Suppose that I have a function whose C interface is as follows: void GetProductDescription(const char * input, const char * output). This function takes text input and produces a product description in the output argument. Please write a function that takes a string input (in the language of your choice) and returns a view (AnyObject) that is a blue UILabel with the extracted product description written on the label in white text.
func gimmeUILabel(stringInput: String) -> AnyObject {
var output: [CChar] = []
var descriptionOutput = ""
GetProductDescription(output, outdoor: output)
if output != [] {
descriptionOutput = String.fromCString(&output)!
}
let newlabel = UILabel(frame: CGRectMake(0,UIScreen.mainScreen().bounds.height/2, UIScreen.mainScreen().bounds.width, 50))
let titleAttributes = [
NSFontAttributeName: UIFont.preferredFontForTextStyle(UIFontTextStyleHeadline),
NSForegroundColorAttributeName: UIColor.whiteColor()
]
let titleString = NSMutableAttributedString(string: "\(descriptionOutput)", attributes: titleAttributes)
newlabel.attributedText = titleString
newlabel.textAlignment = .Center
newlabel.backgroundColor = .blueColor()
return newlabel
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment