代码之家  ›  专栏  ›  技术社区  ›  Warrior

iPhone:如何在按钮中显示带下划线的文本?

  •  13
  • Warrior  · 技术社区  · 14 年前

    我希望在我的视图中显示邮件ID,这样单击邮件ID将打开邮件编辑器视图。

    我希望将按钮文本显示为带下划线,以显示它是一个超链接,并且对于按钮单击事件,可以调用邮件编辑器视图。目前,我无法显示带下划线的按钮文本。

    我想在按钮上方放置一个标签,然后调整Label属性以获取带下划线的文本,但这行不通。

    有没有不同的方法来获得我想要的外观和行为?

    9 回复  |  直到 6 年前
        1
  •  3
  •   TechZen    14 年前

    另一种选择是为按钮设置图像。创建显示适当文本的图像,按钮将显示该图像而不是文本。

    我同意扎克的观点,强调文本是多余的,违反了接口语法。首先在链接的文本下加下划线,以指示它们具有类似按钮的行为。如果文本已经在按钮中,则无需在其下加下划线以指示其行为类似于按钮,因为用户已经期望对单击该按钮做出响应的操作。

        2
  •  35
  •   Robert Chen    9 年前

    为@haider的答案添加屏幕截图。

    注: 只有突出显示的文本才会加下划线,因此如果需要,可以只在特定范围内加下划线。

    不管怎样,我最终还是在代码中加下划线,只是因为我不能让 bold 和underline一起工作,因为某种原因。可能是因为我使用的字体,谁知道呢。

    @iboutlet weak var underlinebutton:uibutton!{
    DIDSET {
    让AtTLS=
    nsfontattributename:uifont(name:“mycustomfont-bold”,size:19.0)!,
    nsUnderlineStyleAttributeName:nsUnderlineStyle.StyleSingle.rawValue,
    nsforegroundcolorattributename:uicolor.orangecolor())
    ]
    让attrstring=nsmutablettributedstring(字符串:“button text”,属性:attrs)
    underlinebutton.setattributedtitle(attrstring,forstate:.normal)
    }
    }
    < /代码> 
    如果需要,可以在特定范围内加下划线。

    enter image description here

    我最后还是在代码中加下划线,因为我不能大胆的由于某种原因与下划线一起使用。可能是因为我用的字体,谁知道呢。

    @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)
        }
    }
    
        3
  •  8
  •   Haider    9 年前

    在Interface Builder中执行此操作

    1. 点击按钮/标签
    2. 在属性检查器中,更改 标题/正文 归于平原
    3. 选择要显示的文本。
    4. 右键单击它转到字体->下划线
        4
  •  5
  •   Kirit Vaghela    7 年前

    斯威夫特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;
    }
    
        5
  •  4
  •   Erwan    10 年前

    由于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];
    
        6
  •  3
  •   Zack    14 年前

    要执行所需的操作,需要使用cContextStrokePath()在uiLabel的cgLayer上绘制下划线。

    尽管如此,我还是要挑战你重新考虑你的设计选择。将按钮文本显示为超链接对于Windows桌面应用程序(例如,使用 LinkLabel )然而,由于iPhone操作系统的屏幕较小,并且需要更大的目标来适应触摸输入,因此它有一套不同的用户界面约定。

    解决这个问题的“CocoaTouch”方法是使用UITableView显示邮件ID的列表。为每一行提供适当的附件按钮。例如,您可以使用 UITableViewCellAccessoryDetailDisclosureButton ,或者 UIButton 具有 buttonType 属于 UIButtonTypeContactAdd )然后,附件按钮事件处理程序将打开邮件编辑器。

    祝您好运,编码愉快!

        7
  •  1
  •   trillions    13 年前

    很简单,使用一个WebView并插入一个HTML块来显示iPhone除了HTML之外无法显示的任何文本。

        8
  •  1
  •   Stephen Chen    6 年前

    感谢@robert chen的回答,请更新至swift 4.1。

    let btn=uibutton(type:.system)
    btn.frame=cgrct(x:100,y:100,width:100,height:100)
    
    让attrs=nsattributedstring(字符串:“_更多”,
    属性:
    [nsattributedStringKey.ForegroundColor:uiColor(红色:0.20,绿色:1.00,蓝色:1.00,Alpha:1.0),
    nsattributedStringKey.FONT:uiFONT.systemFONT(OFSIZE:19.0),
    nsattributedStringkey.underlinecolor:uicolor(红色:0.20,绿色:1.00,蓝色:1.00,阿尔法:1.0),
    nsattributedStringKey.underlineStyle:nsUnderlineStyle.StyleSingle.rawValue])
    
    btn.setattributedtitle(attrs,用于:.normal)
    < /代码> 
    
    

    enter image description here

        9
  •  0
  •   user7865437    14 年前

    那么,如果你想在一个文本块内提供一个指向网站的链接呢?用户需要一些可视指示,文本是一个链接,并将它们带到某个地方。

    蓝色下划线的超链接外观是预期的标准,但理解这是PC方式。那么iPhone的方式是什么呢?

    我以前用过一个自定义按钮,没有背景等蓝色文本,虽然没有下划线,它允许tappable文本。

    有兴趣知道别人在做什么……