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? P>
4 Réponses :
Pour cela, vous pouvez sous-classe Faites votre méthode Drawrect en Uilabel comme P> 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é.
- (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];
}
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. p>
C'est sur github chez https://github.com/benvium/bvunderlinebutton (licence MIT). p>
Vous pouvez l'utiliser dans une xib / storyboard ou directement via le code. P>
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];
Pouvons-nous rendre la ligne plus basse?
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) } }
Voici comment faire la même chose dans Storyboard (Xcode 6). Stackoverflow.com/a/26930512/309046