Ios 居中UIButton文本,用于类似收件箱应用程序的浮动操作按钮

Ios 居中UIButton文本,用于类似收件箱应用程序的浮动操作按钮,ios,swift,uibutton,uilabel,Ios,Swift,Uibutton,Uilabel,结果如下: 正如您所看到的,加号按钮没有正确地对准中心。我怎样才能把它放在中间而不增加UIABEL作为子视图?< /P> < P>你可以简单地用“< UIBezierPath >代码>”来画你的按钮,这里是你的代码: floatingButton = UIButton(x: blocx, y:blocy, width: imageSize, height: imageSize, target: self, action: "clickedFloatingButton") floatingBut

结果如下:


正如您所看到的,加号按钮没有正确地对准中心。我怎样才能把它放在中间而不增加UIABEL作为子视图?< /P> < P>你可以简单地用“< UIBezierPath >代码>”来画你的按钮,这里是你的代码:

floatingButton = UIButton(x: blocx, y:blocy, width: imageSize, height: imageSize, target: self, action: "clickedFloatingButton")
floatingButton!.backgroundColor = CozyColors.StatCardSkip
floatingButton!.layer.cornerRadius = floatingButton!.frame.size.width / 2
floatingButton!.setTitle("+", forState: UIControlState.Normal)
floatingButton!.setTitleColor(CozyColors.ThemeWhite, forState: UIControlState.Normal)
floatingButton!.titleLabel?.font = UIFont(name: CozyStyles.ThemeFontName, size: imageSize*2/3)
view.addSubview(floatingButton!)
您的结果将是:

您可以参考示例项目以了解更多信息。您可以根据需要对其进行修改


希望它能对您有所帮助。

您将标题边缘插入设置如下。更改其值并将标题设置为中间

import UIKit

class PushButtonView: UIButton {

    override func drawRect(rect: CGRect) {
        var path = UIBezierPath(ovalInRect: rect)
        UIColor.redColor().setFill()
        path.fill()

        //set up the width and height variables
        //for the horizontal stroke
        let plusHeight: CGFloat = 3.0
        let plusWidth: CGFloat = min(bounds.width, bounds.height) * 0.6

        //create the path
        var plusPath = UIBezierPath()

        //set the path's line width to the height of the stroke
        plusPath.lineWidth = plusHeight

        //move the initial point of the path
        //to the start of the horizontal stroke
        plusPath.moveToPoint(CGPoint(x:bounds.width/2 - plusWidth/2 + 0.5, y:bounds.height/2 + 0.5))

        //add a point to the path at the end of the stroke
        plusPath.addLineToPoint(CGPoint(x:bounds.width/2 + plusWidth/2 + 0.5, y:bounds.height/2 + 0.5))

        //Vertical Line

        //move to the start of the vertical stroke
        plusPath.moveToPoint(CGPoint(x:bounds.width/2 + 0.5, y:bounds.height/2 - plusWidth/2 + 0.5))

        //add the end point to the vertical stroke
        plusPath.addLineToPoint(CGPoint(x:bounds.width/2 + 0.5, y:bounds.height/2 + plusWidth/2 + 0.5))

        //set the stroke color
        UIColor.whiteColor().setStroke()

        //draw the stroke
        plusPath.stroke()
    }
}
根据下面的功能变化值

floatingButton.titleEdgeInsets = UIEdgeInsetsMake(-24, 0, 0, 0);

嗯,我认为一个简单的方法就是为它设置一个背景图像

func UIEdgeInsetsMake(top: CGFloat, left: CGFloat, bottom: CGFloat, right: CGFloat) -> UIEdgeInsets
您可以应用转换或从谷歌上找到类似的图像

背景图像在这里

请尝试以下代码:

    floatingButton.setBackgroundImage(UIImage(named: "icon.png"), forState: UIControlState.Normal)

很抱歉,如果大小是动态的,并且edgeinset值将是动态的,您无法做什么?标题顶部空间取决于字体大小和高度,因此必须根据其大小进行更改。如果大小是动态的,并且edgeinset值将是动态的,您该怎么办?@Esq您必须根据大小动态计算顶部边缘。否则,您可以按照#DharmeshKheni的建议使用
UIBezierPath
    var floatingButton = UIButton(frame: CGRectMake(10, 20, 50, 50))
    floatingButton.backgroundColor = UIColor.redColor()
    floatingButton.layer.cornerRadius = floatingButton.frame.size.width / 2
    floatingButton.setTitle("+", forState: UIControlState.Normal)
    floatingButton.setTitleColor(UIColor.whiteColor(), forState: UIControlState.Normal)
    floatingButton.titleLabel?.font = UIFont(name: floatingButton.titleLabel!.font.familyName , size: 50)
    floatingButton.titleEdgeInsets = UIEdgeInsetsMake(-10, 0, 0, 0)
    view.addSubview(floatingButton)