【问题标题】:How to vertically centre align * (star) in label or button如何在标签或按钮中垂直居中对齐*(星号)
【发布时间】:2016-04-26 06:15:58
【问题描述】:

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

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

【问题讨论】:

  • 可以尝试使用这个unicode:fileformat.info/info/unicode/char/2217/index.htm
  • 如果你想让它居中,你应该对其进行一些自定义。对于 '*',默认情况下不会发生。
  • 字符不居中。它们与基线对齐。如果您希望字符以不同方式对齐,则必须手动对齐或使用图像。
  • @zp_x - 怎么做...我尝试将 U+2217 分配给标题..但没有成功
  • 使用“*”的图像不是更容易吗?

标签: ios swift uibutton vertical-alignment


【解决方案1】:

只需使用不同的字符。除了 * (ASTERISK U+002A) 之外,还有许多其他类似且居中的选项:

U+2217 ASTERISK OPERATOR ∗(在某些字体中居中,但在其他字体中不居中)

U+273B 泪珠状星号 ✻

U+FE61 小星号﹡

U+FF0A 全宽星号 *

U+2735 八角风车之星✵

U+2736 六角黑星✶

FileFormat.info 给出了我最喜欢的搜索界面。但是你也可以只拉起字符查看器(^⌘Space)。

【讨论】:

【解决方案2】:

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

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

【讨论】:

    【解决方案3】:

    这是 'algal' 为您提供的一个非常好的解决方案,只需根据您的喜好调整值即可。 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)
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-01-11
      • 2012-09-14
      • 2018-07-11
      • 2017-08-21
      • 2016-09-01
      • 1970-01-01
      • 2023-04-07
      • 2017-04-16
      相关资源
      最近更新 更多