Изменение положения метки кнопки в Xcode - PullRequest
1 голос
/ 03 марта 2011

Мне просто интересно, может ли кто-нибудь помочь мне изменить положение метки кнопки для кнопки, которую я сделал с кодом.

Большое спасибо,

Chris

Ответы [ 3 ]

7 голосов
/ 03 марта 2011

Если вы имеете в виду UIButton, посмотрите на свойство titleEdgeInsets UIButton.

2 голосов
/ 03 марта 2011

Значит, ярлык кнопки является экземпляром UILabel, не так ли? Установите положение этой метки, установив для нее рамку относительно рамки UIButton.

UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect]; 
[button setFrame:CGRectMake(3, 20, w, h)];
UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 20, w, h)];
[titleLabel setText:@"TITLE"];
[button addSubview:titleLabel];
[self.view addSubview:button];
[titleLabel release];

Я жестко закодировал позиции x и y кнопки и метки. Вы устанавливаете некоторые другие значения и размещаете метку в соответствующей позиции, где хотите.

Если вы хотите установить только текстовую позицию этого ярлыка, вы можете сделать это следующим образом:

    UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect]; 
    [button setFrame:CGRectMake(3, 20, w, h)];
    UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, w, h)];
    [titleLabel setText:@"TITLE"];
    [titleLabel setTextAlignment:UITextAlignmentCenter];//(UITextAlignmentRight/UITextAlignmentLeft)
    [button addSubview:titleLabel];
    [self.view addSubview:button];
    [titleLabel release];

Надеюсь, это поможет вам.

0 голосов
/ 13 февраля 2013
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    button.frame = CGRectMake(0,0,50,32);
    [button setBackgroundImage:[UIImage imageNamed:@"btn_back.png"] forState:UIControlStateNormal];

    [button addTarget:self action:@selector(BackBtnClicked) forControlEvents:UIControlEventTouchUpInside];
    UIView *backButtonView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 50, 32)];
    backButtonView.bounds = CGRectOffset(backButtonView.bounds, -1, -4);
    [backButtonView addSubview:button];

    UIBarButtonItem *barButtonItem = [[UIBarButtonItem alloc] initWithCustomView:backButtonView];
    //barButtonItem.imageInsets = UIEdgeInsetsMake(-6, 20, 30, 0);

    [self.navigationItem setLeftBarButtonItem:barButtonItem];
...