2016-07-18 52 views
5

Bir paragraf çizmek istiyorum (bu yüzden CTFrameDraw veya ctlinedraw kullanmayı tahmin ediyorum) ancak tanımlı alana sığmıyorsa metni de kesmem gerekiyor (düzeltme). Normalde, bir elipsi ekleyerek son satırı keseriz (yani '...' karakterleri). Bunu nasıl yaparsınız? '... [+]' deIOS, elips ile bir paragrafın son görünen satırı nasıl kırpılır?

i bir (piksel cinsinden) ilk hat için ve eğer mümkünse de tanımlamak girinti özel belirtmek için bilmek istiyorum gibi özel karakter ile bunun nasıl yapılacağı

ve bitirmek için linespacing i bir test projede viewDidLoad için

cevap

0

Kopya aşağıdaki kodu (o tanımlanmış alandan daha belki daha düşük olduğunu) tam olarak dolu paragraf uyacak kesin nihai rect bilmemiz gerekir. üç nokta sonra [+] gelince

// Set the size of the area the paragraph will be drawn in. 
CGSize sizeOfTextArea = CGSizeMake(200.0f, 100.0f); 

// Sample text and views. 
NSString *text = @"Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur."; 
UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(100, 100, sizeOfTextArea.width, sizeOfTextArea.height)]; 
imageView.backgroundColor = [UIColor whiteColor]; 
[self.view addSubview:imageView]; 
self.view.backgroundColor = [UIColor blackColor]; 
CGRect textRect = imageView.frame; 
UIButton *moreBtn = [UIButton buttonWithType:UIButtonTypeCustom]; 
moreBtn.frame = CGRectMake(CGRectGetMaxX(textRect)-45, CGRectGetMaxY(textRect), 45, 20); 
[moreBtn setTitle:@"more" forState:UIControlStateNormal]; 
[self.view addSubview:moreBtn]; 

// Create a paragraph style and add it to attributed text options. 
NSMutableParagraphStyle *style = [[NSParagraphStyle defaultParagraphStyle] mutableCopy]; 
style.firstLineHeadIndent = 10.0f; // <- CHANGE THIS TO ADJUST INDENT 
style.lineSpacing = 10.0f;   // <- CHANGE THIS TO ADJUST LINE SPACING 
NSDictionary *attributes = @{NSParagraphStyleAttributeName : style}; 

// Render the text 
// The options set the text to word-wrap and to add an ellipsis if needed. 
UIGraphicsBeginImageContext(sizeOfTextArea); 
NSAttributedString *attributedText = [[NSAttributedString alloc] initWithString:text attributes:attributes]; 
[attributedText drawWithRect:CGRectMake(0, 0, sizeOfTextArea.width, sizeOfTextArea.height) 
        options:NSStringDrawingUsesLineFragmentOrigin|NSStringDrawingTruncatesLastVisibleLine 
        context:nil]; 
UIImage *renderedText = UIGraphicsGetImageFromCurrentImageContext(); 
UIGraphicsEndImageContext(); 

// Display the rendered text. 
imageView.image = renderedText; 

// Calculate the rect for the full text. We fix the width and let iOS calculate the height. 
CGRect fullTextRect = [attributedText boundingRectWithSize:CGSizeMake(sizeOfTextArea.width, CGFLOAT_MAX) 
                options:NSStringDrawingUsesLineFragmentOrigin|NSStringDrawingUsesFontLeading 
                context:nil]; 
NSLog(@"full rect: %@", NSStringFromCGRect(fullTextRect)); 

// Show/hide more button depending on whether there's more text to show 
moreBtn.hidden = CGRectGetHeight(fullTextRect) <= sizeOfTextArea.height; 

yerine göstermek (ne demek istediğimi görmek için yukarıdaki kodu bakınız) daha fazla metin var olmasına bağlı olarak gösterilen ya da gizli olur metinden sonra bir 'daha fazla' düğmesi ekleyin. Bu .U gibi UIelement etiket veya düğme koduna yukarıda ve

+0

teşekkürler jp2g ama '...' ile nasıl değiştirileceğini anlamıyorum '... [+]' ya da "... daha fazla" gibi bir şeyle? – loki

+0

@loki: 'more' düğmesini eklemek için örnek kodu güncelledim. – jp2g

+0

o nasıl yaptığınızı görüyorsunuz, düğmeyi metnin üst kısmına yerleştirirsiniz :(bu bir fikir, ancak bu metin güzel bir sonuç getirmeyecek çünkü metin her yerde kesilecek, örneğin char :( – loki

0
NSMutableParagraphStyle *paragraph = [[NSMutableParagraphStyle alloc] init]; 
    paragraph.lineBreakMode = NSLineBreakByTruncatingTail; 
    [attributedString addAttribute:NSParagraphStyleAttributeName value:paragraph range:NSMakeRange(0, [attributedString length])]; 
    descriptionLabel.attributedText = attributedString; 

kullanın geçen çizgisinde üç nokta alacak. Ve sonunda ipi eklemeye çalıştım ama yapmadım. çünkü sınırlı alanda etiket sadece gizleme kalan bilgileri gösterir. Bu satırda bazı metin denir elipsler anlamına gelir. Daha az bilgi vermek için.