UIButton上的两行文本,每行都有不同的字体

use*_*992 3 objective-c ios

我找到了一种在UIButton上添加两行文本的方法,但我想要的是这些文本行中的每一行都有不同的字体(例如一个是粗体,另一个不是).怎么可能这样做?

谢谢.

Rol*_*som 10

您应该将2 UILabel添加到UIButton作为子视图.

你可以这样做:

UIButton *testButton = [UIButton buttonWithType:UIButtonTypeCustom];
testButton.frame = CGRectMake(0, 0, 200, 40);
[self.view addSubview:testButton];

UILabel *firstLineTestButton = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 200, 20)];
firstLineTestButton.text = @"First line";
firstLineTestButton.font = [UIFont systemFontOfSize:12];
[testButton addSubview:firstLineTestButton];

UILabel *secondLineTestButton = [[UILabel alloc] initWithFrame:CGRectMake(0, 20, 200, 20)];
secondLineTestButton.text = @"Second line";
secondLineTestButton.font = [UIFont boldSystemFontOfSize:12];
[testButton addSubview:secondLineTestButton];
Run Code Online (Sandbox Code Playgroud)

为了使UILabel可以突出显示,您需要突出显示按钮自定义.

因此,将操作添加到按钮,然后检查标签的按钮子视图并更改其颜色.

[testButton addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
[testButton addTarget:self action:@selector(changeColor:) forControlEvents:UIControlEventTouchDown];
[testButton addTarget:self action:@selector(touchCancel:) forControlEvents:UIControlEventTouchDragExit];

-(void)buttonAction:(UIButton*)sender
{
    [self touchCancel:sender];
    /* DO SOME MORE ACTIONS */
}

-(void)changeColor:(UIButton*)sender
{
    sender.backgroundColor = [UIColor grayColor];

    for( UIView *subview in sender.subviews ){
        if( [subview isKindOfClass:[UILabel class]] ){
            UILabel *subViewLabel = (UILabel*)subview;
            subViewLabel.textColor = [UIColor blueColor];
        }
    }
}

-(void)touchCancel:(UIButton*)sender
{
    sender.backgroundColor = [UIColor clearColor];

    for( UIView *subview in sender.subviews ){
        if( [subview isKindOfClass:[UILabel class]] ){
            UILabel *subViewLabel = (UILabel*)subview;
            subViewLabel.textColor = [UIColor blackColor];
        }
    }
}
Run Code Online (Sandbox Code Playgroud)


Bjö*_*ser 8

Roland的解决方案很好,另一种方法是使用a NSAttributedString.缺点是,它只适用于iOS 6及更高版本.

如果这不是问题,这是代码

- (void)viewDidLoad
{
    [super viewDidLoad];

    // We want 2 lines for our buttons' title label
    [[self.button titleLabel] setNumberOfLines:2];

    // Setup the string
    NSMutableAttributedString *titleText = [[NSMutableAttributedString alloc] initWithString:@"This should be bold,\n and this should not."];

    // Set the font to bold from the beginning of the string to the ","
    [titleText addAttributes:[NSDictionary dictionaryWithObject:[UIFont boldSystemFontOfSize:14] forKey:NSFontAttributeName] range:NSMakeRange(0, 20)];

    // Normal font for the rest of the text
    [titleText addAttributes:[NSDictionary dictionaryWithObject:[UIFont systemFontOfSize:14] forKey:NSFontAttributeName] range:NSMakeRange(20, 22)];

    // Set the attributed string as the buttons' title text
    [self.button setAttributedTitle:titleText forState:UIControlStateNormal];
}
Run Code Online (Sandbox Code Playgroud)