7
votes

Comment puis-je souligner le texte du bouton de Uibutton?

Le texte provient d'une base de données. Je voudrais l'utiliser pour un bouton et souligner le texte du bouton. Comment puis-je faire ça?


1 commentaires

Voici comment faire la même chose dans Storyboard (Xcode 6). Stackoverflow.com/a/26930512/309046


4 Réponses :


3
votes

Pour cela, vous pouvez sous-classe Uilabel code> et écraser sa méthode -Drawrect code>, puis utilisez votre propre uilabel code> et ajoutez un uibutton sur celui-ci de type personnalisé.

Faites votre méthode Drawrect en Uilabel comme P>

- (void)drawRect:(CGRect)rect 
{
    CGContextRef context = UIGraphicsGetCurrentContext();

    CGContextSetRGBStrokeColor(context, 207.0f/255.0f, 91.0f/255.0f, 44.0f/255.0f, 1.0f);

    CGContextSetLineWidth(context, 1.0f);

    CGContextMoveToPoint(context, 0, self.bounds.size.height - 1);
    CGContextAddLineToPoint(context, self.bounds.size.width, self.bounds.size.height - 1);

    CGContextStrokePath(context);

    [super drawRect:rect]; 

}


0 commentaires

0
votes

Pour que cela soit un peu plus simple (c'est une exigence commune) J'ai construit une simple sous-classe Uibutton appelée BvunlineButton que vous pouvez déposer directement dans vos projets.

C'est sur github chez https://github.com/benvium/bvunderlinebutton (licence MIT).

Vous pouvez l'utiliser dans une xib / storyboard ou directement via le code.


0 commentaires

43
votes

dans iOS 6, Nsattributedstring fort> est utilisé en modifiant le texte, vous pouvez utiliser "NSMutaLeAttributeString" pour le texte multicolore, la police, le style, etc. en utilisant un seul UIBUTON ou UILABEL.

NSMutableAttributedString *titleString = [[NSMutableAttributedString alloc] initWithString:@"The Underlined text"];

// making text property to underline text-
[titleString addAttribute:NSUnderlineStyleAttributeName value:[NSNumber numberWithInteger:NSUnderlineStyleSingle] range:NSMakeRange(0, [titleString length])];

// using text on button
[button setAttributedTitle: titleString forState:UIControlStateNormal];


1 commentaires

Pouvons-nous rendre la ligne plus basse?



2
votes

Swift 3 L'extension suivante peut être utilisée pour un soulignement:

extension UIButton {
    func underlineButton(text: String) {
        let titleString = NSMutableAttributedString(string: text)
        titleString.addAttribute(NSUnderlineStyleAttributeName, value: NSUnderlineStyle.styleSingle.rawValue, range: NSMakeRange(0, text.characters.count))
        self.setAttributedTitle(titleString, for: .normal)
    }
} 


0 commentaires