2015-01-13 2 views
-3

두 개의 다른 setTitles 또는 두 개의 텍스트 세트 만있는 Swift에서 UIButton을 만들려고합니다. 이것은 가능한가? 버튼이 두 개의 서로 다른 제목을 갖는 방법에 대한 최선의 선택은 무엇입니까? 제가 질문하는 이유는 제가 직무 내용과 회사를 보여주는 데이터가있는 두 가지 쿼리를 가져 오기 때문입니다. 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로 연결하십시오.

enter image description here

내가 추가 인스턴스 변수를 추가 할 수 있습니다 텍스트의 두 번째 줄을 그리려면 :

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

이 버튼의 모습입니다 :

은 내가 사용하는 버튼 제목을 설정하려면 :

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

내 세트 제목 기능 변경 to :

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

이며 drawRect 함수에서 buttonTitle2에 buttonTitle 그리기 코드를 반복합니다 (물론 위치를 변경 함). buttonTitle2의 모든 글꼴 속성 (글꼴 스타일, 크기 및 색상)을 변경할 수도 있습니다.

+0

감사합니다! 이것은 내가 필요한 것입니다! – Tom

관련 문제