Reputation: 382
I'm currently developing an iPad application and want to apply a custom font to the UIButtons on a certain screen. I have noticed similar problems with other screens, namely that the text on some (seemingly random) UIButtons disappears. In this case, the custom font is being applied to some buttons but not others, again there doesn't seem to be any pattern as to which buttons work and which don't. I've attached a screenshot below to try to give you an idea of what exactly I mean.
As I've mentioned, on some other screens I have noticed text completely disappearing from some buttons and have had to replace these with images featuring the text instead.
All buttons are created in Interface Builder. They use attributed text to allow multiple lines and centred alignment. Any help would be much appreciated.
edit - my code is like the following:
for (UIView *sub in view.subviews) {
UIButton *btn = (UIButton *) sub;
UILabel *lbl = [btn titleLabel];
[lbl setFont: myFont size: mySize];
}
Upvotes: 0
Views: 147
Reputation: 10129
From the comments:
You can achieve centered and multiline title labels in UIButtons without using NSAttributedStrings by adding the following lines to your for loop:
lbl.textAlignment = UITextAlignmentCenter;
lbl.numberOfLines = 0;
Upvotes: 2
Reputation: 112855
To use attributed insure the IB items are set to use attributed text, not plain text.
To set the attributed title for a NSButton
use:
- (NSAttributedString *)attributedTitleForState:(UIControlState)state
for a NSLabel
use:
@property(nonatomic,copy) NSAttributedString *attributedText
Of course you may not need attributed text is all you are doing is just setting a font and alignment.
For multiple lines of text set:
@property(nonatomic) NSInteger numberOfLines
as appropriate.
Upvotes: 3