2

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.

madlymad
  • 6,367
  • 6
  • 37
  • 68
Vad
  • 3,658
  • 8
  • 46
  • 81

2 Answers2

6

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]}];
Mert
  • 6,025
  • 3
  • 21
  • 33
0

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.

Maverick1st
  • 3,774
  • 2
  • 34
  • 50
  • This may not be the best solution but I wanted iOS 5 support so could not use answer with NSAttributedString and I also had trouble subclasssing UIButton. UILable I could subclass fine and it works for me. – Vad Mar 30 '13 at 02:05