SORU
15 Mart 2010, PAZARTESİ


UİButton: bir resim ve bir metin imageEdgeİnsets kullanarak merkezi ve titleEdgeİnsets?

Bir düğme sadece bir resim koydum ve imageEdgeİnsets zirveye daha yakın ayarlarsanız, görüntü merkezli kalır ve beklendiği gibi çalışır:

[button setImage:image forState:UIControlStateNormal];
[button setImageEdgeInsets:UIEdgeInsetsMake(-15.0, 0.0, 0.0, 0.0)];

Bir düğme sadece bir metin koydum ve titleEdgeİnsets altına daha yakın ayarlamak, metin merkezli kalır ve beklendiği gibi çalışır:

[button setTitle:title forState:UIControlStateNormal];
[button setTitleEdgeInsets:UIEdgeInsetsMake(0.0, 0.0, -30, 0.0)];

Ama, eğer 4 satırları birleştirdim metin görüntü ile müdahale ve her ikisi de merkezi hizalama kaybetti.

Benim tüm görüntüleri 30 piksel genişliği ve eğer setTitleEdgeİnsets için UİEdgeİnsetMake sol parametre 30 koyarsam, metni tekrar ortalanır. Sorun görüntü hiç düğme bağımlı olduğunu görünür, çünkü merkezli alır.titleLabel boyutu. Ben zaten düğme boyutu, görüntü boyutu ile birçok hesaplamalar çalıştı, boyut titleLabel ve hiç de mükemmel yerleştirilmiş.

Birileri de aynı sorun vardı?

Şimdiden teşekkürler.

CEVAP
26 AĞUSTOS 2011, Cuma


Buna değer ne için, burada herhangi bir sihirli sayı kullanmadan resim metnin üstünde ortalanmış konumlandırma için genel bir çözüm:

// the space between the image and text
CGFloat spacing = 6.0;

// lower the text and push it left so it appears centered 
//  below the image
CGSize imageSize = button.imageView.frame.size;
button.titleEdgeInsets = UIEdgeInsetsMake(
  0.0, - imageSize.width, - (imageSize.height   spacing), 0.0);

// raise the image and push it right so it appears centered
//  above the text
CGSize titleSize = button.titleLabel.frame.size;
button.imageEdgeInsets = UIEdgeInsetsMake(
  - (titleSize.height   spacing), 0.0, 0.0, - titleSize.width);

Aşağıdaki sürüm açıklamaları aşağıda tavsiye edilmiştir iOS 7 desteklemek için değişiklikler içeriyor. Bu kodu kendim test etmedim, nasıl çalışır ya da eğer iOS önceki sürümlerinde kullanılan kırılır mı emin değilim.

// the space between the image and text
CGFloat spacing = 6.0;

// lower the text and push it left so it appears centered 
//  below the image
CGSize imageSize = button.imageView.image.size;
button.titleEdgeInsets = UIEdgeInsetsMake(
  0.0, - imageSize.width, - (imageSize.height   spacing), 0.0);

// raise the image and push it right so it appears centered
//  above the text
CGSize titleSize = [button.titleLabel.text sizeWithAttributes:@{NSFontAttributeName: button.titleLabel.font}];
button.imageEdgeInsets = UIEdgeInsetsMake(
  - (titleSize.height   spacing), 0.0, 0.0, - titleSize.width);

Bunu Paylaş:
  • Google+
  • E-Posta
Etiketler:

YORUMLAR

SPONSOR VİDEO

Rastgele Yazarlar

  • Epic Tutorials for iPhone, iPad and iOS

    Epic Tutoria

    18 EYLÜL 2011
  • NextGenWindows

    NextGenWindo

    8 Kasım 2011
  • theKGB65

    theKGB65

    24 Aralık 2007