如何创建一个包含两行文本的UIButton,第一行文本大于第二行?

时间:2013-10-23 20:40:08

标签: ios objective-c uibutton uilabel nsattributedstring

基本上,我想要一个看起来像这样的按钮作为最终结果:

enter image description here

按钮跨越两行,而顶行的字体大小较大。在这张照片中,顶部是38pt,底部是24pt。

我知道这对NSAttributedString来说是一项任务,但我无法弄清楚如何正确地完成它。我的故事板中有一个UIButton,其中文本设置为归属(并且换行设置为自动换行),然后是viewDidLoad中的出口我执行以下操作:

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;

基本上我给所有东西都是一个大字体,然后在最后几个字母上我把它缩小了。但它看起来很大胆。

我做错了什么?

2 个答案:

答案 0 :(得分:4)

titleLabel的{​​{1}}无法以与独立UIButton相同的方式访问。 您希望使用UILabel UIButton方法。

编辑:(添加回答行间距问题)

在此示例中,属性为setAttributedTitle:forState:,值为NSParagraphStyleAttributeName

pStyle

答案 1 :(得分:1)

我是在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];

    //...

然后我可以在两个标签上单独设置文本或文本属性。 例如,要设置粗体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];

希望这有帮助! 米克