如何在标签或按钮中垂直居中对齐*(星号)

时间:2016-01-20 11:57:28

标签: ios swift uibutton vertical-alignment

alignment problem of *

我尝试在自定义按钮中居中对齐符号星(*),但我不能。

如何像其他角色一样垂直居中对齐(1,2 ......)?

3 个答案:

答案 0 :(得分:5)

只需使用其他角色即可。而不是*(ASTERISK U + 002A)还有许多其他相似且集中的选项:

U + 2217 ASTERISK OPERATOR *(这是以某些字体为中心,而不是其他字体)

U + 273B TEARDROP-SPOKED ASTERISK✻

U + FE61小型ASTERISK *

U + FF0A FULLWIDTH ASTERISK *

U + 2735八点钉针星✵

U + 2736六点黑星✶

FileFormat.info提供了我最喜欢的搜索界面。但你也可以拉出角色查看器(^⌘Space)。

答案 1 :(得分:0)

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

map

答案 2 :(得分:0)

对于藻类来说,这是一个非常好的解决方案,只需根据您的喜好使用该值进行调整即可。 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)
    }
}

enter image description here