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

问题描述:

alignment problem of *如何垂直居中对齐*(星号)的标签或按钮

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

如何垂直居中对齐就像其他字符(1,2 ...)?

+0

可以尝试使用这个unicode:http://www.fileformat.info/info/unicode/char/2217/index.htm – Surely

+0

如果你想让它居中,你应该对它做一些定制。对于'*',默认情况下不会发生。 –

+0

字符不是以居中。它们被制作成与基线对齐。如果你想要一个字符以不同的方式排列,你必须手动进行对齐或使用图像。 – Sulthan

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

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

这是“海藻”给你一个很好的解决方案,正好与价值发挥你喜欢调整。 https://*.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

只需使用一个不同的角色。而不是*(星号U + 002A),也有很多是相似的,中心的其他选项:

U + 2217星号符*(这是集中在某些字体,而不是其他人)

U + 273B TEARDROP -SPOKED ASTERISK✻

U + FE61 SMALL星号*

U + FF0A FULLWIDTH星号*

U + 2735 EIGHT指出针轮STAR✵

U + 2736 SIX POINTED BLACK STAR✶

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