Swift 2 - Cant Loop UIView with Containing UITextField and Label - ios

I am using swiftyjson to grab some json data and display it in a message box UITextView and UILabel. I have a for LOOP but i can not get the contents to loop inside of my UIScrollview.
I am creating a message box that displays username, date and message inside of a UIView.
My code:
for (_, subJson) in json["messageData"] {
self.outterMessageBox.hidden = false
self.usernameBox.text = subJson["user"].string
self.messageTXTBOX?.text = subJson["message"].string
self.dateBox.text = subJson["datetime"].string
}
And this is the output. Looks fine, just will not loop inside of my scroll view. I've also tried to programmatically create these views with the below code... It still does not loop. What can I be doing wrong?
Programmatically Created Views (i've displays only one iteration of the message box and does not loop as expected).
let myField: UITextField = UITextField (frame: self.ms.bounds)
myField.text = subJson["message"].string! + "\n"
self.ms.addSubview(myField)
let myNameLabel: UILabel = UILabel (frame: self.nm.bounds);
myNameLabel.text = subJson["user"].string
self.nm.addSubview(myNameLabel)
let myDt: UILabel = UILabel (frame: self.dt.bounds)
myDt.text = subJson["datetime"].string
self.dt.addSubview(myDt)
Any help with this would be greatly appreciated.

Related

How to dynamically change the display text of a button in xcode using tags

Im trying to create an email us button in my app where the text displayed changes depending on what version of the app youre using. Currently the text is "Email: " and i want to add an email address to that text.
The button is in a table view cell, and i want to change this text using tags, how would i go about this or is there another option?
P.S The reason i want to do it with tags is when i try to hook the button up to an outlook the build will fail
let sendEmailButton = cell.viewWithTag(666)!
var rect = sendEmailButton.frame
rect.origin.x = 19
sendEmailButton.frame = rect
rect.origin.y = 75
sendEmailButton.frame = rect
change text here to be add email address
In this code i'm moving the button into its position, and then i want to change its text. Again, there no outlet as that caused fails due to it being in a table view. I realise this screen should not be in a table view, but its the way my company does it and i have to work with it
cast your button as UIButton like this:
guard let sendEmailButton = cell.viewWithTag(666) as? UIButton else { return }
and than just set title
sendEmailButton.setTitle("here goes your text", for: .normal)
For your code it will be work.
if let button = cell.viewWithTag(666) as? UIButton {
button.setTitle("Set Your Text here", for: .normal)
}

Swift 3 - Access nested subviews properties in code

For a pure matter of training I'm developing a weather app coding the entire UI rather than using storyboard.
I have a nested structure of views as follows:
SuperView --> UIView (with 5 subviews of type UIView).
Each of the 5 UIViews contains: 1 UIImageView, 2 UILabels
Now, when I'm calling my delegate function to retrieve the weather I'm having trouble updating those values with weather icon, weather description, day.
I tried using Tags for each of the subviews but no joy.
To give you something to look at:
This is where I retrieve my forecast data (icons, description, day):
//MARK: Forecast Wheel elements
let forecastWeatherWheel = UIView()
var forecastDays = [String]()
var forecastDescriptions = [String]()
var forecastIcons = [String]()
func setForecastWeather(forecast: ForecastWeatherData) {
forecastDays = forecast.forecastDay
forecastDescriptions = forecast.weatherDescription
forecastIcons = forecast.icon
for (index,forecastContainerView) in (forecastWeatherWheel.subviews.filter{$0 is UIView}).enumerated(){
for (index,iconImageView) in (forecastContainerView.subviews.filter{$0 is UIImageView}).enumerated(){
let iconImage = iconImageView as! UIImageView
iconImage.image = UIImage(imageLiteralResourceName: forecastIcons[index])
}
}
}
With that nested for I've been - somehow - able to access the image property of my nested view but rather than looping through the array of icons it's using always the same Icon in all the 5 subviews...
Any help is highly appreciated as I'm struggling with this since more than 12 hrs :|
The real answer is of course to use a view subclass, with accessors for the image view and each label, instead of using the subview hierarchy like this. But here's what's wrong with what you're doing right now:
for (index,forecastContainerView) in (forecastWeatherWheel.subviews.filter{$0 is UIView}).enumerated(){
The filter here is pointless; everything in subviews is a UIView. You'll get 5 passes through here.
for (index,iconImageView) in (forecastContainerView.subviews.filter{$0 is UIImageView}).enumerated(){
Your filter here is only going to return a single view - the image view, since the others aren't image views. That means this loop is only going to execute once.
let iconImage = iconImageView as! UIImageView
iconImage.image = UIImage(imageLiteralResourceName: forecastIcons[index])
Which means that index here is your inner index, which is always 0.
Either use a different name for each index variable, or write it something like this (untested, typed in browser):
for (index, forecastContainerView) in forecastWeatherWheel.subviews.enumerated() {
let imageView = forecastContainerView.subviews.first(where: { $0 is UIImageView } ) as! UIImageView
imageView.image = UIImage(imageLiteralResourceName: forecastIcons[index]
}

Card.io Override frame getter

I'm using Card.io to scan cards inside a custom UIView. The issue I have is that the camera view is taking up the view frame resulting in borders left and right. There's a frame property called cameraPreviewFrame that assigns the window and I think I need to override this property and return it's width and height.
Is this possible or is there something else I need to do?
My current code is:
var cardScanView: CardIOView = {
let csv = CardIOView()
csv.guideColor = .blue
csv.hideCardIOLogo = true
csv.allowFreelyRotatingCardGuide = false
csv.backgroundColor = .purple
return csv
}()
I'm adding this programatically inside a collection view cell and it does work in it's current form. Just the view is off.
Thanks

Swift UITextField padding top

I use UITextField and I want padding top.
So... How to in UITextField text padding top?
Sorry for my English:/
If I am not wrong then you want translate your text inside the textfield by y. UITextField have property know as sublayerTransform which can be used to translate the text. For example, to give a text view translate x by 10 and y by 10 you would use this code:
let titleTextField = UITextField(frame: CGRectZero)
titleTextField.layer.sublayerTransform =
let shiftTextByX = 10
let shiftTextByY = 10
titleTextField.layer.sublayerTransform CATransform3DMakeTranslation(shiftTextByX, shiftTextByY, 0)

How to enable hyperlink in UILabel

What I have is a UILabel (loaded with html) that I put on a UIScrollView.
In the html I have lots of links that I can´t get to "fire-up" when clicked.
I have added UserInteractionEnabled = true both on the UILabel and on the UIScrollview but I can´t get any reaction from the taps on the hyperlinks.
This is the code that create the label with the content.
var attr = new NSAttributedStringDocumentAttributes();
var nsError = new NSError();
attr.DocumentType = NSDocumentType.HTML;
var entry = "some text and url <a href='http://www.google.com'>url</a>";
var myHtmlData = NSData.FromString(entry, NSStringEncoding.Unicode);
contentLabel.AttributedText = new NSAttributedString(myHtmlData, attr, ref nsError);
contentLabel.Frame = new CGRect(0, 20, scrollView.Frame.Size.Width , scrollView.Frame.Size.Height);
contentLabel.LineBreakMode = UILineBreakMode.WordWrap;
contentLabel.Lines = 0;
contentLabel.SizeToFit();
Maybe this is just wrong.. putting html content into a label? But I don´t want to use a webview since it does not seem to scale that well.
Why not using a UITextView instead ? Enable Links detection and disable editable and Selectable option.
Adding HTML to UILabel will not make it interactive.
Use UITextView or https://github.com/TTTAttributedLabel/TTTAttributedLabel

Resources