按钮之间的标签在哪里 [英] where are the labels among the buttons

查看:29
本文介绍了按钮之间的标签在哪里的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在下图中,我试图在每个按钮之间放置一个单字符标签,但如第二张图所示,当我插入标签时,按钮消失并且(如果仔细观察)最后一个按钮的文本移动向右.

In the image below I am attempting to put a single-character label between each button, but as the second image shows, when I do insert the labels, the buttons disappear and (if you look closely) the last button's text moves to the right.

你能帮我得到想要的结果吗?

Can you help me get the desired result, please?

没有标签的按钮

带有标签的按钮

视图控制器.h

@property(nonatomic,assign) UILabel* theSuit;

ViewController.m

ViewController.m

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    NSString* cards = @"AKQJT98765432";

    NSInteger yPipsOrigin = 100;
    NSInteger xPipsOrigin = 100;
    NSInteger xPipsStep = 40.0;
    NSInteger xPipsCurrent = xPipsOrigin;
    // Do any additional setup after loading the view.
    for( int x=0;x<[cards length]; x++ ){
        [cards substringWithRange:NSMakeRange(x,1)];
        UIButton *b= [UIButton buttonWithType:UIButtonTypeRoundedRect];
        xPipsCurrent += xPipsStep;
        [b setTitle:[cards substringWithRange:NSMakeRange(x,1)] forState:UIControlStateNormal];
        [b setTitle:@" " forState:UIControlStateDisabled];
        [b setFrame:CGRectMake(xPipsCurrent, yPipsOrigin, 20, 20)];
        [b setEnabled:YES];
        [b setUserInteractionEnabled:YES];
        [self.view addSubview:b];
        [b addTarget:self action:@selector(spadeButtonPressed:) forControlEvents:UIControlEventTouchUpInside];

        xPipsCurrent = xPipsOrigin + xPipsStep/2;
        for( int x=0;x<[cards length]-1; x++ ){
            xPipsCurrent += xPipsStep;
            UILabel *lab = self.theSuit;
            lab.text = @"Z";
            lab.backgroundColor = [UIColor clearColor];
            lab.center = CGPointMake(xPipsCurrent, yPipsOrigin);
            [self.view addSubview:lab];
        }
    }

}

推荐答案

NSString* cards = @"AKQJT98765432";

NSInteger yPipsOrigin = 100;
NSInteger xPipsOrigin = 100;
NSInteger xPipsStep = 40.0;
NSInteger xPipsCurrent = xPipsOrigin;

for( int x=0;x<[cards length]; x++ )
{
    [cards substringWithRange:NSMakeRange(x,1)];
    UIButton *b= [UIButton buttonWithType:UIButtonTypeRoundedRect];
    xPipsCurrent += xPipsStep;
    [b setTitle:[cards substringWithRange:NSMakeRange(x,1)] forState:UIControlStateNormal];
    [b setTitle:@" " forState:UIControlStateDisabled];
    [b setFrame:CGRectMake(xPipsCurrent, yPipsOrigin, 20, 20)];
    [b setEnabled:YES];
    [b setUserInteractionEnabled:YES];
    [self.view addSubview:b];
    [b addTarget:self action:@selector(spadeButtonPressed:) forControlEvents:UIControlEventTouchUpInside];

    UILabel *lab = [[UILabel alloc] initWithFrame:CGRectMake(xPipsCurrent+b.frame.size.width, yPipsOrigin, 20, 20)];
    lab.text = @"Z";
    lab.textAlignment = NSTextAlignmentCenter;
    lab.backgroundColor = [UIColor clearColor];
    [self.view addSubview:lab];
}

这篇关于按钮之间的标签在哪里的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆