Place Label under each point from spaceBetweenPoints - ios

I'm using a custom UIView for creating this control with 4 hotspots with the same space between the points. I'm trying to create for labels exactly under the points and it seems like the first label is correctly aligned, but i cant seem to figure out why all of them aren't correct?
Here you see the space between each point and the radius on the points.
customSlider?.spaceBetweenPoints = 95
customSlider?.radiusPoint = 5
illustration at the moment:
customSlider?.frame = CGRectMake((self.frame.width-335)/2, 20, 335, 34)
for var i = 0;i<4;i++
{
var xPointSpace = 95
var xPos = xPointSpace * i
var xSpace = (self.frame.width-335)/2
var xWidth = Int(xSpace) + xPos
var xFloat = CGFloat(xWidth)
var textLabel:UILabel = UILabel(frame: CGRectMake(xFloat, 34, 30, 21))
textLabel.text = "lol"
textLabel.backgroundColor = UIColor.redColor()
textLabel.textAlignment = NSTextAlignment.Center
textLabel.font = UIFont(name: "HelveticaNeue", size: 12)
self.addSubview(textLabel)
}
adding the labels:

customSlider?.frame = CGRectMake((self.frame.width-335)/2, 20, 335, 34)
for var i = 0;i<4;i++
{
var xPointSpace = 95
var radiusPoint = 5
var xPos = (xPointSpace + radiusPoint * 2)* i
var xSpace = customSlider?.frame.origin.x
var xWidth = xSpace + xPos
var xFloat = CGFloat(xWidth)
var textLabel:UILabel = UILabel(frame: CGRectMake(xFloat, 34, 30, 21))
textLabel.text = "lol"
textLabel.backgroundColor = UIColor.redColor()
textLabel.textAlignment = NSTextAlignment.Center
textLabel.font = UIFont(name: "HelveticaNeue", size: 12)
self.addSubview(textLabel)
}

Related

Add UILabel on random position on UIView

How do I Achieve Something like below image,
I want to add n number of UILabel to screen on random position without overlapping of label, and also randomly set rotate.
Here is my code, but it is overlapping label to another label:
override func viewDidLoad() {
super.viewDidLoad()
var textDatas = ["aaa","bbb","cccccc","ddddddd","ee","ffffff","ggggg","hhhh","iiiiii","jjjjjj","kkkkkk","ll","mmmmm","nnnnnnnn","ooooooooo","ppppppppp","qqqqqqqqqqqq","rrrrrrrr","sssssss","ttttttttt","uuuuuuu","vvvv","wwww","xxxxxx","yyyy","zzzzzzz"]
textDatas.forEach { (textData) in
let randomIntX = Int.random(in: 10..<Int(ScreenSize.SCREEN_WIDTH-200))
let randomIntY = Int.random(in: 50..<Int(ScreenSize.SCREEN_HEIGHT-50))
let randomIntFontSize = Int.random(in: 10..<40)
let randomIntLabelRotate = Int.random(in: 1..<3)
let label = UILabel(frame: CGRect(x: randomIntX, y: randomIntY, width: 200, height: 30))
label.font = UIFont(name: FONT_AVENIR_HEAVY,
size: CGFloat(randomIntFontSize))
label.textColor = .white
label.textAlignment = .center
label.text = textData
label.sizeToFit()
if randomIntLabelRotate == 1 {
label.transform = CGAffineTransform(rotationAngle: -CGFloat.pi / 2)
}
self.view.addSubview(label)
}
}
Is there any library or solution of this will be very appreciated. Thanks

issue with title in navigation bar in arabic

I am having an issue with this function:
func setTitle(title:String, subtitle:String) -> UIView {
let titleLabel = UILabel(frame: CGRect(x:0, y:-5, width:0, height:0))
titleLabel.backgroundColor = UIColor.clear
titleLabel.textColor = UIColor.gray
titleLabel.font = UIFont.boldSystemFont(ofSize: 17)
titleLabel.text = title
titleLabel.sizeToFit()
let subtitleLabel = UILabel(frame: CGRect(x:0, y:18, width:0, height:0))
subtitleLabel.backgroundColor = UIColor.clear
subtitleLabel.textColor = UIColor.black
subtitleLabel.font = UIFont.systemFont(ofSize: 12)
subtitleLabel.text = subtitle
subtitleLabel.sizeToFit()
let titleView = UIView(frame: CGRect(x:0, y:0, width:max(titleLabel.frame.size.width, subtitleLabel.frame.size.width), height:30))
titleView.addSubview(titleLabel)
titleView.addSubview(subtitleLabel)
let widthDiff = subtitleLabel.frame.size.width - titleLabel.frame.size.width
if widthDiff > 0 {
var frame = titleLabel.frame
frame.origin.x = widthDiff / 2
titleLabel.frame = frame.integral
} else {
var frame = subtitleLabel.frame
frame.origin.x = abs(widthDiff) / 2
titleLabel.frame = frame.integral
}
return titleView
}
// Use :
self.navigationItem.titleView = setTitle("title", "subtitle")
// Source : http://stackoverflow.com/questions/12914004/uinavigationbar-titleview-with-subtitle
as shown above images, whenever someone writes in arabic[picture 2] the title will be messed up, but if someone wrote it in English[picture 1]. the label of title will remain good.
thank you in advance.
I ran code with all combinations of arabic and english words and found the problem in code. Check it out.
if widthDiff > 0 {
var frame = titleLabel.frame
frame.origin.x = widthDiff / 2
titleLabel.frame = frame.integral
} else {
var frame = subtitleLabel.frame
frame.origin.x = abs(widthDiff) / 2
titleLabel.frame = frame.integral /* subtitleLabel.frame = frame.integral */
}

Not seeing all views in views using views.addSubviews

I am running into a basic problem with views on iPad. I have a viewController. The view is using a UIImageView with a full screen image as a background image. I am trying to overlay labels on the top. If I set labels individually, both views show up. If I call a function with the same information only one view shows up. I need to extend it many labels. Here is the code:
class ViewController: UIViewController {
#IBOutlet weak var backGroundImageView: UIImageView!
var cFrame:[CGRect?] = [CGRect?](repeating: nil, count: 13)
var offsets:[CGRect?] = [CGRect?](repeating: nil, count: 13)
var labels: [UILabel?] = [UILabel?](repeating:UILabel(), count:13)
override func viewDidLoad() {
super.viewDidLoad()
cFrame[0] = CGRect(x:450,y:530,width:251,height:68)
cFrame[1] = CGRect(x:147,y:676,width:222,height:24)
loadFrameValues()
var frame = CGRect(x: 450, y: 520, width: 251, height: 68)
let label0 = UILabel(frame: frame)
label0.backgroundColor = .white
label0.numberOfLines = 0
label0.lineBreakMode = .byWordWrapping
label0.textAlignment = .center
label0.text = "Text 1"
frame = CGRect(x: 152, y: 686, width: 210, height: 16)
let label1 = UILabel(frame: frame)
label1.backgroundColor = .cyan
label1.lineBreakMode = .byWordWrapping
label1.textAlignment = .left
label1.text = "Text 2"
label1.font = label1.font.withSize(12)
backGroundImageView.addSubview(label0)
backGroundImageView.addSubview(label1)
// showView(label: labels[0]!, frame: cFrame[0]!)
// showView(label: labels[1]!, frame: cFrame[1]!)
// }
}
func showView(label: UILabel, frame:CGRect) {
label.frame = frame
label.backgroundColor = .white
label.numberOfLines = 0
label.lineBreakMode = .byWordWrapping
label.textAlignment = .center
label.text = "Syed Tariq"
backGroundImageView.addSubview(label)
}
You can't add subview (label in your case) to UIImageView. So add label as subview to the superview of your image view.
override func viewDidLoad() {
cFrame[0] = CGRect(x:50,y:130,width:151,height:68)
cFrame[1] = CGRect(x:114,y:276,width:122,height:24)
showView(frame: cFrame[0]!)
showView(frame: cFrame[1]!)
}
func showView(frame:CGRect) {
let label = UILabel(frame: frame)
label.backgroundColor = .green
label.numberOfLines = 0
label.lineBreakMode = .byWordWrapping
label.textAlignment = .center
label.text = "Syed Tariq"
self.view.addSubview(label)
}

Adding subview is not working

I'm trying to make a popUp view.
Here I have
let popOver = UIView()
popOver.hidden = true
popOver.frame = CGRectMake(self.view.frame.width / 2 - 150, self.view.frame.height / 2 - 150, 250, 250)
popOver.backgroundColor = UIColor.whiteColor()
let lbl = UILabel()
lbl.text = "blablabla"
lbl.backgroundColor = UIColor.redColor()
lbl.textColor = UIColor.yellowColor()
popOver.addSubview(lbl)
lbl.center = popOver.center
popOver.bringSubviewToFront(lbl)
self.view.addSubview(popOver)
popOver.center = self.view.center
popOver.layer.cornerRadius = 9
UIView.animateWithDuration(2.0, animations: {
self.popOver.hidden = false
})
}
Everything is ok except that the label is now shown any time I try to execute my application. I see only white frame:
What my mistake is?
try this
let popOver = UIView()
popOver.hidden = true
popOver.frame = CGRectMake(self.view.frame.width / 2 - 150, self.view.frame.height / 2 - 150, 250, 250)
popOver.backgroundColor = UIColor.whiteColor()
popOver.center = self.view.center
popOver.layer.cornerRadius = 9
self.view.addSubview(popOver)
let lbl = UILabel()
lbl.text = "blablabla"
lbl.backgroundColor = UIColor.redColor()
lbl.textColor = UIColor.yellowColor()
lbl.frame = CGRectMake((popOver.frame.size.width / 2)-50, (popOver.frame.size.height / 2)-50, 100,100)
//lbl.center = popOver.center
popOver.addSubview(lbl)
popOver.bringSubviewToFront(lbl)
UIView.animateWithDuration(1.0, animations: {
popOver.hidden = false
})
the output is
You are wrong at the position of the label
let lbl = UILabel()
lbl.frame = CGRectMake(0, 0, 25, 25)
lbl.text = "blablabla"
lbl.backgroundColor = UIColor.redColor()
lbl.textColor = UIColor.yellowColor()
popOver.addSubview(lbl)
//lbl.center = popOver.center
lbl.center = CGPointMake(popOver.frame.width / 2, popOver.frame.height/2)

Why the second "if" is not working?

I am trying to populate a view and I am writing the code of this view. The code:
class DetailViewController: UIViewController, UIScrollViewDelegate {
#IBOutlet weak var tituloView: UILabel!
#IBOutlet weak var scrollView: UIScrollView!
var txtTitulo: String?
var txt1: String?
var imageView: UIImageView?
var img1: String?
var txt2: String?
var texto2: String?
var imageView2: UIImageView?
var img2: String?
var txt3: String?
var img3: String?
var txt4: String?
var img4: String?
var arrayHeights: [Int] = []
override func viewDidLoad() {
super.viewDidLoad()
tituloView.text = txtTitulo
tituloView.textAlignment = .Center
tituloView.font = UIFont(name: "HelveticaNeue-UltraLight", size: 40)
scrollView.zoomScale = 1
var paragraphStyle1 = NSMutableParagraphStyle()
paragraphStyle1.alignment = NSTextAlignment.Justified
var attributedString1 = NSAttributedString(string: txt1!,
attributes: [
NSParagraphStyleAttributeName: paragraphStyle1,
NSBaselineOffsetAttributeName: NSNumber(float: 0)
])
let texto1 = UILabel(frame: CGRectMake(154, -55, 697, 200))
texto1.attributedText = attributedString1
texto1.numberOfLines = 0
texto1.font = UIFont(name: "HelveticaNeue-UltraLight", size: 28)
texto1.lineBreakMode = .ByWordWrapping
texto1.sizeToFit()
scrollView.addSubview(texto1)
let imagem = UIImage(named: img1!)
let imageView = UIImageView(image: imagem)
imageView.frame = CGRect(x: 320, y: texto1.frame.size.height, width: 400, height: 250)
imageView.contentMode = .ScaleAspectFit
scrollView.addSubview(imageView)
if (txt2 != nil) {
var paragraphStyle2 = NSMutableParagraphStyle()
paragraphStyle2.alignment = NSTextAlignment.Justified
var attributedString2 = NSAttributedString(string: txt2!,
attributes: [
NSParagraphStyleAttributeName: paragraphStyle2,
NSBaselineOffsetAttributeName: NSNumber(float: 0)
])
let texto2 = UILabel(frame: CGRectMake(164, imageView.frame.origin.y + imageView.frame.size.height + 20, 697, 200))
texto2.attributedText = attributedString2
texto2.numberOfLines = 0
texto2.font = UIFont(name: "HelveticaNeue-UltraLight", size: 28)
texto2.lineBreakMode = .ByWordWrapping
texto2.sizeToFit()
scrollView.contentSize = CGSizeMake(texto1.frame.size.width, texto1.frame.size.height + imageView.frame.size.height + texto2.frame.size.height + 20)
scrollView.addSubview(texto2)
if (img2 != nil) {
let imagem2 = UIImage(named: img2!)
let imageView2 = UIImageView(image: imagem2)
imageView2.frame = CGRect(x: 320, y: texto2.frame.origin.y + texto2.frame.size.height, width: 400, height: 250)
imageView2.contentMode = .ScaleAspectFit
scrollView.contentSize = CGSizeMake(texto1.frame.size.width, texto1.frame.size.height + imageView.frame.size.height + imageView2.frame.size.height + texto2.frame.size.height + 20)
scrollView.addSubview(imageView2)
} else {
scrollView.contentSize = CGSizeMake(texto1.frame.size.width, texto1.frame.size.height + imageView.frame.size.height)
}
}
scrollView.scrollEnabled = true
}
}
The problem is: if the img2 is nil, this code is not executed and my scrollView is not set properly:
var paragraphStyle2 = NSMutableParagraphStyle()
paragraphStyle2.alignment = NSTextAlignment.Justified
var attributedString2 = NSAttributedString(string: txt2!,
attributes: [
NSParagraphStyleAttributeName: paragraphStyle2,
NSBaselineOffsetAttributeName: NSNumber(float: 0)
])
let texto2 = UILabel(frame: CGRectMake(164, imageView.frame.origin.y + imageView.frame.size.height + 20, 697, 200))
texto2.attributedText = attributedString2
texto2.numberOfLines = 0
texto2.font = UIFont(name: "HelveticaNeue-UltraLight", size: 28)
texto2.lineBreakMode = .ByWordWrapping
texto2.sizeToFit()
scrollView.contentSize = CGSizeMake(texto1.frame.size.width, texto1.frame.size.height + imageView.frame.size.height + texto2.frame.size.height + 20)
scrollView.addSubview(texto2)
Why is this happening? If I img2 is not nil, the code runs with no problems. I added a println to see if the code inside the if statements were executed, and it was.
All infos (txtTitulo, txt1, imageView, txt2....) are being sent by another View Controller through a segue.
This view, the DetailViewController was created in StoryBoard with a label and a scrollView, the rest was added programmatically as you can see here.
I am sorry if this problem might seem too easy, but this is my first iOS app. Thanks for answering!
In a comment, you said:
The problem here is that when this happens, I mean, when the img2 is
nil, the
scrollView.contentSize = CGSizeMake(texto1.frame.size.width,
texto1.frame.size.height + imageView.frame.size.height +
texto2.frame.size.height + 20)
never gets executed. See what I am saying?
That code is not being executed because you told it not to. You have an if statement that says if img2 != nil and then a block of code that is only run if img2 != nil. Now you're asking why that code isn't executed when img2 == nil?
The answer is "Because that's how if statements work."

Resources