Reputation: 743
I tried centre aligning symbol star(*) in a custom button but I couldn't.
How to vertically centre align just like other characters(1,2...) ?
Upvotes: 14
Views: 18322
Reputation: 299355
Just use a different character. Rather than * (ASTERISK U+002A) there are many other options that are similar and centered:
U+2217 ASTERISK OPERATOR ∗ (this is centered in some fonts, but not others)
U+273B TEARDROP-SPOKED ASTERISK ✻
U+FE61 SMALL ASTERISK ﹡
U+FF0A FULLWIDTH ASTERISK *
U+2735 EIGHT POINTED PINWHEEL STAR ✵
U+2736 SIX POINTED BLACK STAR ✶
FileFormat.info gives my favorite search interface. But you can also just pull up the character viewer (^⌘Space).
Upvotes: 36
Reputation: 156
This is a very good solution for you by 'algal', just play with the value to adjust it as you prefer. https://stackoverflow.com/a/24294816/4205432
import UIKit
class ViewController: UIViewController {
@IBOutlet weak var button: UIButton!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
button.centerLabelVerticallyWithPadding(5)
button.backgroundColor = UIColor.grayColor()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
extension UIButton {
func centerLabelVerticallyWithPadding(spacing:CGFloat) {
// update positioning of image and title
let imageSize = self.imageView!.frame.size
self.titleEdgeInsets = UIEdgeInsets(top:0,
left:-imageSize.width,
bottom:-(imageSize.height + spacing),
right:0)
let titleSize = self.titleLabel!.frame.size
self.imageEdgeInsets = UIEdgeInsets(top:-(titleSize.height + spacing),
left:0,
bottom: 0,
right:-titleSize.width)
// reset contentInset, so intrinsicContentSize() is still accurate
let trueContentSize = CGRectUnion(self.titleLabel!.frame, self.imageView!.frame).size
let oldContentSize = self.intrinsicContentSize()
let heightDelta = trueContentSize.height - oldContentSize.height
let widthDelta = trueContentSize.width - oldContentSize.width
self.contentEdgeInsets = UIEdgeInsets(top:heightDelta/2.0,
left:widthDelta/2.0,
bottom:heightDelta/2.0,
right:widthDelta/2.0)
}
}
Upvotes: 0
Reputation: 169
you can simply use this line of code.
button.titleEdgeInsets=UIEdgeInsetsMake(left,bottom,right,top)
Upvotes: 1