Добавление значка значка к круглой прямоугольной кнопке?

Есть ли удобный способ сделать кнопку со значком? Не на значке... Я хочу сделать это в приложении. Кнопка или изображение со значком, выбранным программой. Или мне нужно создать библиотеку изображений Photoshop?


person Cherr Skees    schedule 23.04.2012    source источник
comment
вы имеете в виду значок, который автоматически подстраивается под ширину вашего текста?   -  person sooper    schedule 24.04.2012


Ответы (1)


Вам нужно будет использовать resizableImageWithCapInsets, чтобы добиться этого без библиотеки изображений Photoshop. Есть несколько отличных тем (здесь и здесь), которые объясняют его использование.

Вот пример, который я только что сделал, чтобы дать вам представление:

//Create a label (width/height not important at this stage)
UILabel *yourLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 1, 1)];
yourLabel.text = @"7+";
[yourLabel sizeToFit];

CGRect labelFrame = yourLabel.frame;

//Here we create a UIImage that is resizable, but will not resize the areas concerned with the cap insets you've defined
UIImage *badgeImage = [[UIImage imageNamed:@"badge.png"]resizableImageWithCapInsets:UIEdgeInsetsMake(5, 5, 5, 5)];
UIImageView *badgeImageView = [[UIImageView alloc]initWithImage:badgeImage];

badgeImageView.contentMode = UIViewContentModeScaleToFill;
badgeImageView.backgroundColor = [UIColor clearColor];

labelFrame.size.width += 5; //This is the 'padding' on the right and left (added together)
//If your badge edges are completely circular then you don't want to change the height, but if they're not then go ahead in the same way with the width. If your badge has a static height, you'll need to make sure the font size doesn't exceed this height; better start off with a small font-size

badgeImageView.frame = labelFrame; //The badge is now the right width with padding taken into account

//Center the label on the badge image view
yourLabel.center = CGPointMake(badgeImageView.frame.size.width/2, badgeImageView.frame.size.height/2);

//Finally we add the label to the badge image view
[badgeImageView addSubview:yourLabel];
//Add your badge to the main view
[self.view addSubview:badgeImageView];

[badgeImageView release];
[yourLabel release];
person sooper    schedule 24.04.2012
comment
Большое спасибо... Попробую. - person Cherr Skees; 24.04.2012