你如何在 UIButton 中放置两个不同的文本

How do you put two different text in a UIButton

我正在尝试在 Swift 中创建一个 UIButton,其中有两个不同的 setTitles 或只有两组文本。这可能吗?如果不是,关于如何使按钮具有两个不同标题的最佳选择是什么?我问的原因是因为我正在提取两个不同的查询,这些查询的数据显示职位描述和公司。我只能使用 setTitle 设置工作名称,但我不知道如何在同一个按钮中输入公司名称。这是我想要实现的目标的视觉效果。任何事情都会有帮助谢谢!

我为您提供的唯一示例是 OS X NSButton 子class,但更改为 UIButton 应该不难。

请注意,drawRect 代码就像您在 NSView(或 UIView)中执行的一样。从你发的图片来看,我想你其实已经知道怎么做了。

代码:

import Foundation
import AppKit


class WispSquareButton : NSButton
{
    var buttonTitle:String = String();

// *************************************************** Draw Rect ******************************************************

override func drawRect(dirtyRect:NSRect)
{
    let context = NSGraphicsContext.currentContext()!.CGContext       // Get the context

    let rgbColorspace:CGColorSpaceRef = CGColorSpaceCreateDeviceRGB() // Define a color space variable
    let nsBounds:NSRect = self.bounds                                 // Get the bounds
    let cgBounds:CGRect = NSRectToCGRect(nsBounds)                    // Sets the graphics bounds

    // Top Side Color
    CGContextSetRGBFillColor(context, 0.41568627, 0.69803922, 0.95686275, 1.0)

    CGContextMoveToPoint(context, 0.0, 0.0)
    CGContextAddLineToPoint(context, cgBounds.size.width, 0.0)
    CGContextAddLineToPoint(context, cgBounds.size.width * 0.95, cgBounds.size.height * 0.15)
    CGContextAddLineToPoint(context, cgBounds.size.width * 0.05, cgBounds.size.height * 0.15)
    CGContextAddLineToPoint(context, 0.0, 0.0)
    CGContextFillPath(context)


    // Right Side
    CGContextSetRGBFillColor(context, 0.03529412, 0.20784314, 0.38823529, 1.0)

    CGContextMoveToPoint(context, cgBounds.size.width, 0.0)
    CGContextAddLineToPoint(context, cgBounds.size.width, cgBounds.size.height)
    CGContextAddLineToPoint(context, cgBounds.size.width * 0.95, cgBounds.size.height * 0.85)
    CGContextAddLineToPoint(context, cgBounds.size.width * 0.95, cgBounds.size.height * 0.15)
    CGContextAddLineToPoint(context, cgBounds.size.width, 0.0)
    CGContextFillPath(context)

    // Left Side
    CGContextSetRGBFillColor(context, 0.41568627, 0.69803922, 0.95686275, 1.0)

    CGContextMoveToPoint(context, 0.0, 0.0);
    CGContextAddLineToPoint(context, cgBounds.size.width * 0.05, cgBounds.size.height * 0.15)
    CGContextAddLineToPoint(context, cgBounds.size.width * 0.05, cgBounds.size.height * 0.85)
    CGContextAddLineToPoint(context, 0.0, cgBounds.size.height);
    CGContextAddLineToPoint(context, 0.0, 0.0)
    CGContextFillPath(context)

    // Bottom Side
    CGContextSetRGBFillColor(context, 0.03529412, 0.20784314, 0.38823529, 1.0)

    CGContextMoveToPoint(context, 0.0, cgBounds.size.height)
    CGContextAddLineToPoint(context, cgBounds.size.width, cgBounds.size.height)
    CGContextAddLineToPoint(context, cgBounds.size.width * 0.95, cgBounds.size.height * 0.85)
    CGContextAddLineToPoint(context, cgBounds.size.width * 0.05, cgBounds.size.height * 0.85)
    CGContextAddLineToPoint(context, 0.0, cgBounds.size.height)
    CGContextFillPath(context)

    // Center
    CGContextSetRGBFillColor(context, 0.24117647, 0.53333333, 0.82941176, 1.0)

    CGContextMoveToPoint(context, cgBounds.size.width * 0.05, cgBounds.size.height * 0.15)
    CGContextAddLineToPoint(context, cgBounds.size.width * 0.95, cgBounds.size.height * 0.15)
    CGContextAddLineToPoint(context, cgBounds.size.width * 0.95, cgBounds.size.height * 0.85)
    CGContextAddLineToPoint(context, cgBounds.size.width * 0.05, cgBounds.size.height * 0.85)
    CGContextAddLineToPoint(context, cgBounds.size.width * 0.05, cgBounds.size.height * 0.15)
    CGContextFillPath(context);

    if(buttonTitle != "")
    {
        // ********************************************* Set up the Text
        let textSize:CGFloat = cgBounds.size.height * 0.65
        let textCenterY:CGFloat = cgBounds.size.height / 2.0
        let boundsCenterX:CGFloat = cgBounds.size.width / 2.0

         // Set the text matrix.
        let textTransform:CGAffineTransform = CGAffineTransformMake(1.0, 0.0, 0.0, -1.0, 0.0, 0.0)
        CGContextSetTextMatrix(context, textTransform)

        // Create a color that will be added as an attribute to the attrString for button text.
        let buttonTextColorComponents:[CGFloat] = [ 0.0, 0.0, 0.0, 1.0 ]
        let buttonTextColor:CGColorRef = CGColorCreate(rgbColorspace, buttonTextColorComponents)

        // Create a color that will be added as an attribute to the attrString for invisible text.
        let invisibleTextColorComponents:[CGFloat] = [ 0.0, 0.0, 0.0, 0.0 ];
        let invisibleColor:CGColorRef = CGColorCreate(rgbColorspace, invisibleTextColorComponents);

        // Create a font for text.
        let stringFontName:CFStringRef = CFStringCreateWithCString(kCFAllocatorDefault, "AppleCasual",  kCFStringEncodingASCII)
        let stringFont:CTFontRef = CTFontCreateWithName(stringFontName, textSize, nil)

        // Create a mutable attributed string with a max length of 0 for normal text.
        var attrString:CFMutableAttributedStringRef = CFAttributedStringCreateMutable(kCFAllocatorDefault, 0)

        // Create a path which will bound the area where you will be drawing text.
        var invisibleTextPath:CGMutablePathRef = CGPathCreateMutable()

        // Create a path which will bound the area where you will be drawing text.
        var buttonTextPath:CGMutablePathRef = CGPathCreateMutable()

        // Center the Title

        // Get Title Length
        var titleLength:CFIndex = CFStringGetLength(buttonTitle)

        // Measure the string length
        var invisibleTextBounds:CGRect = CGRectMake(0.0, 0.0, cgBounds.size.width * 2.0, textSize * 1.3)
        CGPathAddRect(invisibleTextPath, nil, invisibleTextBounds)

        // Copy the title into attrString
        CFAttributedStringReplaceString (attrString, CFRangeMake(0, 0), buttonTitle)

        // Set the color and font of the invisibleText
        CFAttributedStringSetAttribute(attrString, CFRangeMake(0, titleLength), kCTForegroundColorAttributeName, invisibleColor)
        CFAttributedStringSetAttribute(attrString, CFRangeMake(0, titleLength), kCTFontAttributeName, stringFont)

        // Create the framesetter with the attributed string.
        var framesetter:CTFramesetterRef = CTFramesetterCreateWithAttributedString(attrString);

        var frame:CTFrameRef = CTFramesetterCreateFrame(framesetter, CFRangeMake(0, 0), invisibleTextPath, nil);

        // Draw the invisible string
        CTFrameDraw(frame, context);

        var endingTextPoint:CGPoint = CGContextGetTextPosition(context);

        // Draw the Visible Text
        // Set a rectangular path.

        var textBounds:CGRect = CGRectMake(boundsCenterX - (endingTextPoint.x / 2.0), textCenterY, cgBounds.size.width, textSize * 1.3)
        CGPathAddRect(buttonTextPath, nil, textBounds)

        // Copy the textString into attrString
        CFAttributedStringReplaceString (attrString, CFRangeMake(0, titleLength), buttonTitle)

        // Set the color and font.
        CFAttributedStringSetAttribute(attrString, CFRangeMake(0, titleLength), kCTForegroundColorAttributeName, buttonTextColor)
        CFAttributedStringSetAttribute(attrString, CFRangeMake(0, titleLength), kCTFontAttributeName, stringFont)

        // Create the framesetter with the attributed string.
        framesetter = CTFramesetterCreateWithAttributedString(attrString)

        // Create a frame.
        frame = CTFramesetterCreateFrame(framesetter, CFRangeMake(0, 0), buttonTextPath, nil)

        // Draw the specified frame in the given context.
        CTFrameDraw(frame, context);

    }

CGContextFlush(context);

}

// ***************************************************** Set Title ****************************************************

func setTitle(aString:String)
{
    buttonTitle = aString;
}

}

我通过将自定义视图拖到 window 中来使用按钮,并在身份检查器中将视图的 class 设置为 "WispSquareButton"。我向视图声明一个 IBOutlet:

@IBOutlet var wispSquareButton:WispSquareButton!

并在IB中连接。

设置我使用的按钮标题:

wispSquareButton.setTitle("Square Button")
wispSquareButton.display()

这是按钮的样子:

要绘制第二行文本,我可能会添加一个额外的实例变量:

var buttonTitle:String = String()
var buttonTitle2:String = String()

将我的 setTitle 函数更改为:

func setTitle(aString:String, aString2:String)
{
    buttonTitle = aString;
    buttonTitle2 = aString2
}

并在 drawRect 函数中重复 buttonTitle2 的 buttonTitle 绘图代码(当然要更改它的位置)。我还可以更改 buttonTitle2 的所有字体属性(字体样式、大小和颜色)。