我想在我的視圖中顯示郵件ID,以便單擊郵件ID應打開郵件編輯器視圖。iPhone:如何在按鈕中顯示帶下劃線的文字?
我想顯示下劃線的按鈕文本以顯示它是一個超鏈接,並且按鈕單擊事件可以調用郵件編輯器視圖。目前,我無法顯示加下劃線的按鈕文字。
我想在按鈕上方放置一個標籤,並調整標籤屬性以獲得帶下劃線的文本,但這並不起作用。
有沒有不同的方式來獲得我想要的外觀和行爲?
我想在我的視圖中顯示郵件ID,以便單擊郵件ID應打開郵件編輯器視圖。iPhone:如何在按鈕中顯示帶下劃線的文字?
我想顯示下劃線的按鈕文本以顯示它是一個超鏈接,並且按鈕單擊事件可以調用郵件編輯器視圖。目前,我無法顯示加下劃線的按鈕文字。
我想在按鈕上方放置一個標籤,並調整標籤屬性以獲得帶下劃線的文本,但這並不起作用。
有沒有不同的方式來獲得我想要的外觀和行爲?
另一種替代方法是設置按鈕的圖像。創建一個顯示適當文本的圖像,該按鈕將顯示該文本而不是文本。
我同意扎克強調文本是多餘的,違反了接口語法。您首先在鏈接文本中標明文字,以表明它們具有按鈕之類的行爲。如果文本已經在按鈕中,則不需要將其強調爲表示它的行爲類似於按鈕,因爲用戶已經期望響應於點擊它的動作。
如果你有翻譯,@ TechZen,你必須爲每種語言準備圖像? – new2ios 2015-04-30 12:52:49
要做你想做的事,你需要使用CGContextStrokePath()在UILabel的CGLayer上繪製下劃線。
這就是說,我會挑戰你重新考慮你的設計選擇。使按鈕文本顯示爲超鏈接對於Windows桌面應用程序來說是合理的(例如,使用LinkLabel
)。但是,iPhone OS具有不同的用戶界面慣例,因爲它的屏幕更小,並且需要更大的目標以適應觸摸輸入。
這個問題的「Cocoa Touch」方法是使用UITableView顯示郵件ID列表。爲每行提供適當的附件按鈕。例如,您可以使用UITableViewCellAccessoryDetailDisclosureButton
或UIButton
與buttonType
的UIButtonTypeContactAdd
)。附件按鈕事件處理程序然後將調出郵件編輯器。
祝你好運,快樂的編碼!
那麼,如果你想提供一個文本塊內的網站鏈接?用戶需要一些視覺指示文字是一個鏈接,並將他們帶到某個地方。
藍色加下劃線的超鏈接外觀是預期的規範,但明白這是PC方式。那麼iPhone的方式是什麼?
我在過去使用了一個自定義按鈕,沒有背景等和藍色文本,雖然沒有下劃線,它確實允許可點擊文本。
很想知道別人怎麼做....
Swift 3。2
if let title = button.titleLabel?.text, title != "" {
let attributeString = NSMutableAttributedString(string: title)
attributeString.addAttribute(NSAttributedStringKey.underlineStyle, value: 1, range: NSMakeRange(0, title.count))
button.titleLabel?.attributedText = attributeString
}
目標C
NSString *tem = self.myButton.titleLabel.text;
if (tem != nil && ![tem isEqualToString:@""]) {
NSMutableAttributedString *temString=[[NSMutableAttributedString alloc]initWithString:tem];
[temString addAttribute:NSUnderlineStyleAttributeName
value:[NSNumber numberWithInt:1]
range:(NSRange){0,[temString length]}];
self.myButton.titleLabel.attributedText = temString;
}
由於iOS的6.0你可以使用NSAttributedString
與UIButton
。
//create an underlined attributed string
NSAttributedString *titleString = [[NSAttributedString alloc] initWithString:@"Title" attributes:@{NSUnderlineStyleAttributeName : @(NSUnderlineStyleSingle | NSUnderlinePatternSolid)}];
//use the setAttributedTitle method
[yourButton setAttributedTitle:titleString forState:UIControlStateNormal];
要做到這一點在Interface Builder
增加對@海德爾的答案截圖。
注意:只有突出顯示的文本纔會加下劃線,所以如果需要,您可以強調一個特定的範圍。
我結束了在代碼中強調也無妨,只是因爲我無法得到大膽連同下劃線工作的某些原因。可能是因爲我使用的字體,誰知道。
@IBOutlet weak var underlineButton: UIButton! {
didSet {
let attrs = [
NSFontAttributeName : UIFont(name: "MyCustomFont-Bold", size: 19.0)!,
NSUnderlineStyleAttributeName: NSUnderlineStyle.StyleSingle.rawValue,
NSForegroundColorAttributeName : UIColor.orangeColor()
]
let attrString = NSMutableAttributedString(string: "Button text", attributes:attrs)
underlineButton.setAttributedTitle(attrString, forState: .Normal)
}
}
以下是如何在故事板(XCode 6)中執行相同操作。 http://stackoverflow.com/a/26930512/309046 – satish 2015-03-03 09:39:01