UIButton vertical alignment doesn't work
UIButton has a very nifty property named "titleEdgeInsets
" which you can use (via UIEdgeInsetsMake
to reposition the top and bottom margins of the title and get the thing centered, vertically.
This behavior is due to the baselineAdjustment
default property of the button's titleLabel
. If you set this to UIBaselineAdjustmentNone
, you should get the effect you're looking for.
btn2.titleLabel.baselineAdjustment = UIBaselineAdjustmentNone;
From the docs for UILabel
:
baselineAdjustment
Controls how text baselines are adjusted when text needs to shrink to fit in the label.
@property(nonatomic) UIBaselineAdjustment baselineAdjustment
Discussion
If the
adjustsFontSizeToFitWidth
property is set toYES
, this property controls the behavior of the text baselines in situations where adjustment of the font size is required. The default value of this property isUIBaselineAdjustmentAlignBaselines
. This property is effective only when thenumberOfLines
property is set to1
.
and
UIBaselineAdjustmentAlignBaselines
Adjust text relative to the position of its baseline.
Available in iOS 2.0 and later.
UIBaselineAdjustmentAlignCenters
Adjust text based relative to the center of its bounding box.
Available in iOS 2.0 and later.
UIBaselineAdjustmentNone
Adjust text relative to the top-left corner of the bounding box. This is the default adjustment.
Available in iOS 2.0 and later.
Note that the default adjustment for UILabel
differs from that of a button's titleLabel
.