我想在Swift中創建一個UIButton,它有兩個不同的setTitles或者只有兩組文本。這甚至有可能嗎?如果沒有什麼是最好的選擇,如何使一個按鈕有兩個不同的標題呢?我之所以問,是因爲我拉兩個不同的查詢,其中有數據顯示工作描述和公司。我只能用setTitle設置一個工作名稱,但我不知道如何在同一個按鈕中輸入公司名稱。這是我想要達到的目標。任何事情都會有幫助謝謝!你如何把兩個不同的文本在UIButton中
-3
A
回答
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()
這是按鈕的樣子:
繪製文本的第二行我可能會增加一個額外的實例變量:
var buttonTitle:String = String()
var buttonTitle2:String = String()
更改我的setTitle函數於:
func setTitle(aString:String, aString2:String)
{
buttonTitle = aString;
buttonTitle2 = aString2
}
,並在drawRect中的功能重複的buttonTitle2的按鈕名稱繪圖代碼(改變其當然的位置)。我還可以更改buttonTitle2的所有字體屬性(字體樣式,大小和顏色)。
+0
謝謝!這正是我需要的! – Tom
相關問題
- 1. 你如何在兩個不同的排名中有圖表?
- 2. 如何同時實現兩個UIButton?
- 3. 你可以使用this.queryText.replace替換兩個不同的文本嗎?
- 4. JNLP:如何把/保持在兩個不同的路徑/回購
- 5. 如何把兩個不同的目錄到一個git根
- 6. 你如何刪除這兩個斜線在不同的位置
- 7. 把兩個輸入文本在同一行
- 8. UIButton的文本不居中
- 9. 如何包含同一頭文件的兩個不同版本?
- 10. 如何從兩個不同的HTML文本框中獲取值?
- 11. 如何在兩個不同的文本框上顯示值
- 12. 如何在一個版本中創建兩個不同的RPM?
- 13. 如何在UIButton中實現兩個不重疊的IBActions?
- 14. 你如何同時插入兩個表?
- 15. 如何在不同的軸上寫兩個不同的文字?
- 16. C# - 如何比較兩個不同的文本文件
- 17. 如何插入兩個不同名稱的多個文本框?
- 18. 我在兩個不同的頁面有兩個文本框
- 19. 如何在GREP中只打印兩個文本文件的不同行?
- 20. 如何在兩個不同的數據庫中打開文本文件
- 21. 如何在不同行上的單個文本視圖中顯示兩個值
- 22. 如何把兩個jasperReports放在一個zip文件中下載?
- 23. UIButton在每行中有不同顏色的文本
- 24. 如何在獨立的iframe中同步兩個文本區域?
- 25. 如何把2個不同的遷移在同一個div?
- 26. 的Windows 8.1 - 如何把不同的元素(如文本框),而不是在SettingsFlyout
- 27. 如何在TeamCity中增加兩個不同的版本號
- 28. 如何在bash腳本中使用兩個不同的用戶
- 29. 腳本從兩個不同的文件
- 30. 把不同的git項目放在同一個文件夾中
與其嘗試使用標準按鈕的setTitle方法,您可以創建一個自定義按鈕,該按鈕是UIbutton的一個子類。然後,您可以在drawRect方法中執行任何您想要的任何操作,包括在具有各種字體的不同位置繪製文本。 – jwlaughton
這很漂亮,我從來不知道!你可以給我一個例子嗎?我仍然在學習,因爲斯威夫特對我來說是新的。 – Tom