How to create a UIButton with two lines of text, and the first line of text will be larger than the second?

Basically, I want a button that looks like this as the final result:

enter image description here

If the button spans two lines and the top line has a larger font size. In this figure, the top is 38pt and the bottom is 24pt.

I know this will be a task for NSAttributedString, but it’s hard for me to figure out how to do this correctly. I have a UIButton in my storyboard with the text set for the attribute (and line break set to word wrap), then exit, where in viewDidLoadI do the following:

UIFont *font = [UIFont systemFontOfSize:39.0];
UIFont *font2= [UIFont systemFontOfSize:17.0];

NSMutableAttributedString *string = [[NSMutableAttributedString alloc] initWithString:@"625 WPM"];
[string addAttribute:NSFontAttributeName value:font range:NSMakeRange(0, string.length)];
[string addAttribute:NSFontAttributeName value:font2 range:NSMakeRange(string.length - 2, 2)];

self.button.titleLabel.attributedText = string;

I basically give the whole big font, and then for the last few letters I do it less. But he just looks brave.

What am I doing wrong?

+4
2

titleLabel UIButton , UILabel. UIButton setAttributedTitle:forState:.

edit: ( )

NSParagraphStyleAttributeName, pStyle

NSMutableParagraphStyle *pStyle = [[NSMutableParagraphStyle alloc] init];
pStyle.lineSpacing = 8;

return @{
          NSParagraphStyleAttributeName : pStyle
          };
+4

BarButtonItem, 2 , , .

2 UILabel, :

-(void) initBottomBar
{
    UIColor *labelColor;

    if ([Utility getiOSVersion] == 7)
        labelColor = [UIColor colorWithRed:43.0f/255.0f green:41.0f/255.0f blue:81.0f/255.0f alpha:1.0f]; // grayblue color
    else
        labelColor = [UIColor whiteColor];

    labelStringCategory                             = [[UILabel alloc] initWithFrame:CGRectMake(0, 3, 300, 22)];
    labelStringCategory.backgroundColor             = [UIColor clearColor];
    labelStringCategory.textColor                   = labelColor;
    labelStringCategory.font                        = [UIFont systemFontOfSize:14];
    labelStringCategory.numberOfLines               = 1;
    labelStringCategory.textAlignment               = NSTextAlignmentLeft;
    labelStringCategory.adjustsFontSizeToFitWidth   = YES;
    labelStringCategory.text = @"First Row";

    changeStringCategory                            = [[UILabel alloc] initWithFrame:CGRectMake(0, 19, 300, 22)];
    changeStringCategory.backgroundColor            = [UIColor clearColor];
    changeStringCategory.textColor                  = labelColor;
    changeStringCategory.font                       = [UIFont systemFontOfSize:12];
    changeStringCategory.numberOfLines              = 1;
    changeStringCategory.textAlignment              = NSTextAlignmentLeft;
    changeStringCategory.adjustsFontSizeToFitWidth  = YES;
    changeStringCategory.text = @"Second row";

    UIButton *displayButton = [UIButton buttonWithType:UIButtonTypeCustom];
    displayButton.frame = CGRectMake(0, 0, 300, 44);
    [displayButton addSubview:labelStringCategory];
    [displayButton addSubview:changeStringCategory];
    displayButton.showsTouchWhenHighlighted = YES;
    [displayButton addTarget:self action:@selector(displayCategorySelected:) forControlEvents:UIControlEventTouchDown];

    UIBarButtonItem *labelStringCategoryBarItem = [[UIBarButtonItem alloc] initWithCustomView:displayButton];

    //...

textattribute . , 1 :

const CGFloat fontSize = 13;
        UIFont *boldFont = [UIFont boldSystemFontOfSize:fontSize];
        UIFont *regularFont = [UIFont systemFontOfSize:fontSize];
        //UIColor *foregroundColor = [UIColor whiteColor];

        // Create the attributes
        NSDictionary *attrs = [NSDictionary dictionaryWithObjectsAndKeys:
                               regularFont, NSFontAttributeName, nil];
        NSDictionary *subAttrs = [NSDictionary dictionaryWithObjectsAndKeys:
                                  boldFont, NSFontAttributeName, nil];
        const NSRange range = NSMakeRange(iFrom,iTo - iFrom);

        // Create the attributed string (text + attributes)
        NSMutableAttributedString *attributedText =
        [[NSMutableAttributedString alloc] initWithString:aText
                                               attributes:attrs];
        [attributedText setAttributes:subAttrs range:range];

        // Set it in our UILabel and we are done!
        [aLabel setAttributedText:attributedText];

, !

+1

All Articles