SORU
27 AĞUSTOS 2010, Cuma


Tek Bir UİLabel Ve Olmayan Kalın Kalın Metin?

Nasıl bir uiLabel cesur ve olmayan cesur hem metin eklemek mümkün mü?

Oldukça da okudum UİWebView.. mümkün NSAttributedString kullanılıyor olabilir kullanmak istemiyorum ama nasıl kullanılacağı hakkında hiçbir fikrim yok. Herhangi bir fikir?

Apple kendi uygulamaları birkaç başarır; Örnek Ekran Görüntüsü: link text

Teşekkürler! - Dom

CEVAP
27 AĞUSTOS 2010, Cuma


İOS6 ve sonrası için güncelleme

İçinde iOS6 şimdi UILabel, UIButton, UITextView, UITextField, ve belki Diğerleri artık destek atfedilen dizeler yani, hiç gerek yok. CATextLayers olarak bizim alıcı için atfedilen dizeler. Ayrıca ile oynamaya gerek yok atfedilen dize artık CoreText :) n-c Vakfı yeni derslerimiz var.NSParagraphStyle ve hayatımızı daha kolay hale getirecek diğer sabitler gibi çerçeve. Yay!

Eğer bu dize varsa:

NSString *text = @"Updated: 2012/10/14 21:59"

Biz sadece isnat dize oluşturmanız gerekir:

if ([_label respondsToSelector:@selector(setAttributedText:)])
{
    // iOS6 and above : Use NSAttributedStrings

    // Create the attributes
    const CGFloat fontSize = 13;
    NSDictionary *attrs = @{
        NSFontAttributeName:[UIFont boldSystemFontOfSize:fontSize],
        NSForegroundColorAttributeName:[UIColor whiteColor]
    };
    NSDictionary *subAttrs = @{
        NSFontAttributeName:[UIFont systemFontOfSize:fontSize]
    };

    // Range of " 2012/10/14 " is (8,12). Ideally it shouldn't be hardcoded
    // This example is about attributed strings in one label
    // not about internationalization, so we keep it simple :)
    const NSRange range = NSMakeRange(8,12);

    // Create the attributed string (text   attributes)
    NSMutableAttributedString *attributedText =
      [[NSMutableAttributedString alloc] initWithString:text
                                             attributes:attrs];
    [attributedText setAttributes:subAttrs range:range];

    // Set it in our UILabel and we are done!
    [_label setAttributedText:attributedText];
} else {
    // iOS5 and below
    // Here we have some options too. The first one is to do something
    // less fancy and show it just as plain text without attributes.
    // The second is to use CoreText and get similar results with a bit
    // more of code. Interested people please look down the old answer.

    // Now I am just being lazy so :p
    [_label setText:text];
}

İyi bir tanıtım blog yazıları birkaç sizleri 15* *varinvasivecodeNSAttributedString kullanımı, arayın daha fazla örnek ile açıklayın"Giriş iOS 6 için NSAttributedString"ve"İOS Interface Builder kullanarak dizeleri atfedilen":)

PS: çalışması gerekir ama Yukarıdaki kod beyin derlenmiş. Yeterlidir umarım :)

Aşağıda iOS5 için ve eski Cevap

Bir NSAttributedString CATextLayer kullanın ! fazla 2 UİLabels daha hafif ve daha basit. (iOS 3.2 ve üzeri)

Örnek.

QuartzCore çerçevesi (CALayers için gerekli), ve CoreText (isnat dize için gerekli.) eklemeyi unutmayın

#import <QuartzCore/QuartzCore.h>
#import <CoreText/CoreText.h>

Örnek aşağıda katacak gezinti denetleyicisi araç için bir yer tutarlar. à la Posta.iPhone uygulaması. :)

- (void)setRefreshDate:(NSDate *)aDate
{
    [aDate retain];
    [refreshDate release];
    refreshDate = aDate;

    if (refreshDate) {

        /* Create the text for the text layer*/    
        NSDateFormatter *df = [[NSDateFormatter alloc] init];
        [df setDateFormat:@"MM/dd/yyyy hh:mm"];

        NSString *dateString = [df stringFromDate:refreshDate];
        NSString *prefix = NSLocalizedString(@"Updated", nil);
        NSString *text = [NSString stringWithFormat:@"%@: %@",prefix, dateString];
        [df release];

        /* Create the text layer on demand */
        if (!_textLayer) {
            _textLayer = [[CATextLayer alloc] init];
            //_textLayer.font = [UIFont boldSystemFontOfSize:13].fontName; // not needed since `string` property will be an NSAttributedString
            _textLayer.backgroundColor = [UIColor clearColor].CGColor;
            _textLayer.wrapped = NO;
            CALayer *layer = self.navigationController.toolbar.layer; //self is a view controller contained by a navigation controller
            _textLayer.frame = CGRectMake((layer.bounds.size.width-180)/2   10, (layer.bounds.size.height-30)/2   10, 180, 30);
            _textLayer.contentsScale = [[UIScreen mainScreen] scale]; // looks nice in retina displays too :)
            _textLayer.alignmentMode = kCAAlignmentCenter;
            [layer addSublayer:_textLayer];
        }

        /* Create the attributes (for the attributed string) */
        CGFloat fontSize = 13;
        UIFont *boldFont = [UIFont boldSystemFontOfSize:fontSize];
        CTFontRef ctBoldFont = CTFontCreateWithName((CFStringRef)boldFont.fontName, boldFont.pointSize, NULL);
        UIFont *font = [UIFont systemFontOfSize:13];
        CTFontRef ctFont = CTFontCreateWithName((CFStringRef)font.fontName, font.pointSize, NULL);
        CGColorRef cgColor = [UIColor whiteColor].CGColor;
        NSDictionary *attributes = [NSDictionary dictionaryWithObjectsAndKeys:
                                    (id)ctBoldFont, (id)kCTFontAttributeName,
                                    cgColor, (id)kCTForegroundColorAttributeName, nil];
        CFRelease(ctBoldFont);
        NSDictionary *subAttributes = [NSDictionary dictionaryWithObjectsAndKeys:(id)ctFont, (id)kCTFontAttributeName, nil];
        CFRelease(ctFont);

        /* Create the attributed string (text   attributes) */
        NSMutableAttributedString *attrStr = [[NSMutableAttributedString alloc] initWithString:text attributes:attributes];
        [attrStr addAttributes:subAttributes range:NSMakeRange(prefix.length, 12)]; //12 is the length of " MM/dd/yyyy/ "

        /* Set the attributes string in the text layer :) */
        _textLayer.string = attrStr;
        [attrStr release];

        _textLayer.opacity = 1.0;
    } else {
        _textLayer.opacity = 0.0;
        _textLayer.string = nil;
    }
}

Bu örnekte ben sadece yazı tipinin iki farklı türleri (kalın ve normal var ama aynı zamanda farklı yazı tipi boyutu, farklı renk, altı çizili, italik vb olabilir. NSAttributedString / NSMutableAttributedString ve CoreText attributes string keys bir göz atın.

Umarım yardımcı olur

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

YORUMLAR

SPONSOR VİDEO

Rastgele Yazarlar

  • CZTUTORIALS

    CZTUTORIALS

    28 Ocak 2011
  • MagmaRhino

    MagmaRhino

    16 Temmuz 2011
  • pleated-jeans

    pleated-jean

    6 HAZİRAN 2013