How to display Underlined Text in a Button on iOS?

后端 未结 10 1623
日久生厌
日久生厌 2020-12-16 15:20

I want to display the mail ID in my view such that clicking the mail ID should open the mail composer view.

I want to display the button text as underlined to show i

10条回答
  •  攒了一身酷
    2020-12-16 15:27

    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
    }
    

    Objective 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;
    }
    

提交回复
热议问题