Generate Labels and Align in Middle

Generate labels and align in middle

This will do it:

var lastLabel : UILabel?
var count = 1

@IBAction func addlabel(sender: AnyObject) {
var label = UILabel()
label.textAlignment = NSTextAlignment.Center
label.backgroundColor = UIColor.redColor()
label.text = count.description;
count++
label.tag = 5;

var centerX = NSLayoutConstraint(item: label, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: self.view, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 0)
centerX.priority = 999
//This if you want to center it vertically also
var centerY = NSLayoutConstraint(item: label, attribute: NSLayoutAttribute.CenterY, relatedBy: NSLayoutRelation.Equal, toItem: self.view, attribute: NSLayoutAttribute.CenterY, multiplier: 1, constant: 0)
var height = NSLayoutConstraint(item: label, attribute: NSLayoutAttribute.Height, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 35)
var width = NSLayoutConstraint(item: label, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 45)

label.setTranslatesAutoresizingMaskIntoConstraints(false)
self.view.addSubview(label);

if let prevlabel = lastLabel {
var align = NSLayoutConstraint(item: prevlabel, attribute: NSLayoutAttribute.TrailingMargin, relatedBy: NSLayoutRelation.Equal, toItem: label, attribute: NSLayoutAttribute.LeadingMargin, multiplier: 1, constant: -20)
//Here just add the NSLayoutConstraint you want to apply, if its only horizontal then not add centerY

NSLayoutConstraint.activateConstraints([align, centerY, height, width, centerX])
}else {
NSLayoutConstraint.activateConstraints([centerX, centerY, height, width])
}
lastLabel = label
}

Align label to centre and left positions in DIV

Following css will give your expected result:



div {

text-align: center;

}

#orderName {

vertical-align: middle;

}

#orderNo {

float: left;

}
 <div id="mydiv">

<label id="orderName" >Order Name</label>

<label id="orderNo">Order No</label>

</div>

CSS center align in label field

Remove float:left and just use verticle align with the image and that's it. Try below:

<div>
<img src="http://lh3.googleusercontent.com/mjejVTJKT4ABNKq2HGlkDs36f-QvzI2hKFER098vBIgiAoZ2H-SN5QPvFaZEVDZRxfujrS6pszZ_J-_di2F57w0IFE3KAciDwGAh-9RcCA=s660" style="width: 20%; vertical-align:middle" alt/>
<label class="lbl">Martin Luther King, Jr. Day 2015</label>
</div>

EDIT: Above will vertically align the text. In case you want to align it horizontally, do the below:

<img src="http://lh3.googleusercontent.com/mjejVTJKT4ABNKq2HGlkDs36f-QvzI2hKFER098vBIgiAoZ2H-SN5QPvFaZEVDZRxfujrS6pszZ_J-_di2F57w0IFE3KAciDwGAh-9RcCA=s660" style="width: 20%;" alt/>
<label class="lbl">Martin Luther King, Jr. Day 2015</label>

And use the css below:

.lbl {
font-size: larger;
position:absolute;
text-align:center;
bottom:0;
width:100%;
left:10%;
}

Working Plnkr : Plnkr

How to center align the label horizontally with select (drop down) in a form?

You can update it by using following flex box code

label {

font-size: 12px;

margin:0 10px **15px** 0;

}

select {

height: 40px;

}

.form-inline {

display: flex;

align-items: center;

justify-content: flex-start;

}

.form-control {

display: inline-block;

width: auto;

vertical-align: middle;

height: 34px;

padding: 6px 12px;

font-size: 14px;

line-height: 1.42857143;

color: #555;

background-color: #fff;

background-image: none;

border: 1px solid #ccc;

border-radius: 4px;

-webkit-box-shadow: inset 0 1px 1px rgba(0,0,0,.075);

box-shadow: inset 0 1px 1px rgba(0,0,0,.075);

-webkit-transition: border-color ease-in-out .15s,-webkit-box-shadow ease-in-out .15s;

-o-transition: border-color ease-in-out .15s,box-shadow ease-in-out .15s;

transition: border-color ease-in-out .15s,box-shadow ease-in-out .15s;

}
<div class="form-inline">

<label for="admin-picker" class="label-admin">User type</label>

<select class="form-control" id="admin-picker" name="admin_privilege" required>

<option value="0">Normal</option>

<option value="1">Admin</option>

</select>

</div>

Align labels in form next to input

While the solutions here are workable, more recent technology has made for what I think is a better solution. CSS Grid Layout allows us to structure a more elegant solution.

The CSS below provides a 2-column "settings" structure, where the first column is expected to be a right-aligned label, followed by some content in the second column. More complicated content can be presented in the second column by wrapping it in a <div>.

[As a side-note: I use CSS to add the ':' that trails each label, as this is a stylistic element - my preference.]

/* CSS */

div.settings {

display:grid;

grid-template-columns: max-content max-content;

grid-gap:5px;

}

div.settings label { text-align:right; }

div.settings label:after { content: ":"; }
<!-- HTML -->

<div class="settings">

<label>Label #1</label>

<input type="text" />

<label>Long Label #2</label>

<span>Display content</span>

<label>Label #3</label>

<input type="text" />

</div>

How do I align a label and a textarea?

You need to put them both in some container element and then apply the alignment on it.

For example:

.formfield * {

vertical-align: middle;

}
<p class="formfield">

<label for="textarea">Label for textarea</label>

<textarea id="textarea" rows="5">Textarea</textarea>

</p>

How can I center a label in the middle of a panel in C#?

 Label myLabel;
Panel myPanel;
int x = (myPanel.Size.Width - myLabel.Size.Width) / 2;
int y = (myPanel.Size.Height - myLabel.Size.Height)/2;
label1.Location = new Point(x, y);


Related Topics



Leave a reply



Submit