2015-01-13 19 views
-3

我想在Swift中創建一個UIButton,它有兩個不同的setTitles或者只有兩組文本。這甚至有可能嗎?如果沒有什麼是最好的選擇,如何使一個按鈕有兩個不同的標題呢?我之所以問,是因爲我拉兩個不同的查詢,其中有數據顯示工作描述和公司。我只能用setTitle設置一個工作名稱,但我不知道如何在同一個按鈕中輸入公司名稱。這是我想要達到的目標。任何事情都會有幫助謝謝!你如何把兩個不同的文本在UIButton中

enter image description here

+1

與其嘗試使用標準按鈕的setTitle方法,您可以創建一個自定義按鈕,該按鈕是UIbutton的一個子類。然後,您可以在drawRect方法中執行任何您想要的任何操作,包括在具有各種字體的不同位置繪製文本。 – jwlaughton

+0

這很漂亮,我從來不知道!你可以給我一個例子嗎?我仍然在學習,因爲斯威夫特對我來說是新的。 – Tom

回答

1

我對你唯一的例子是OS X NSButton子類,而是更改爲一個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; 
} 

} 

我通過拖動一個自定義視圖到窗口使用的按鈕,在身份檢查器中設置視圖的類「WispSquareButton」。我向視圖聲明一個IBOutlet:

@IBOutlet var wispSquareButton:WispSquareButton! 

並將它連接到IB中。

要設置按鈕標題我用:

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

這是按鈕的樣子:

enter image description here

繪製文本的第二行我可能會增加一個額外的實例變量:

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

更改我的setTitle函數於:

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

,並在drawRect中的功能重複的buttonTitle2的按鈕名稱繪圖代碼(改變其當然的位置)。我還可以更改buttonTitle2的所有字體屬性(字體樣式,大小和顏色)。

+0

謝謝!這正是我需要的! – Tom

相關問題