Vad
Vad

Reputation: 3740

UIButton with Text Stroke/Outline

I would like to draw a UIButton with text that is using an outline i.e. stroke. So I tried to play around with titleLabel making my own custom label and assigning to UIButton.titleLabel which does not work. Someone mentioned I can add UILabel on top of UIButton but I am not sure this solution is good.

Can someone recommend a stable approach to this issue? Thanks.

Upvotes: 2

Views: 1970

Answers (2)

Mert
Mert

Reputation: 6065

You can use

- (void)setAttributedTitle:(NSAttributedString *)title forState:(UIControlState)state

method of UIButton.

eg. like this for attributed string

NSAttributedString *attributedText =
[[NSAttributedString alloc] initWithString:title
                                       attributes:@{NSStrokeWidthAttributeName: [NSNumber numberWithInt:-6],
                       NSStrokeColorAttributeName: titleBorderColor,
                   NSForegroundColorAttributeName: [UIColor whiteColor]}];

Upvotes: 6

Maverick1st
Maverick1st

Reputation: 3770

You can also add a view as subview to your button. In this view you can place your Label or, if you want, more labels to have e.g. a button with two different formatted textlines.

Upvotes: 0

Related Questions