简体   繁体   中英

How to vertically centre align * (star) in label or button

对齐问题*

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...) ?

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).

你可以简单地使用这行代码。

button.titleEdgeInsets=UIEdgeInsetsMake(left,bottom,right,top)

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)
    }
}

在此输入图像描述

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM