2012-10-31 15 views
18

Jak ustawić szerokość etykiety zgodnie z tekstem? Jeśli długość tekstu jest mała, chcę mieć małą szerokość etykiety ... Jeśli długość tekstu jest mała, chcę szerokość etykiety zgodną z tą długością tekstu. Czy to możliwe?iPhone - Dostosuj szerokość UILabel zgodnie z tekstem

Właściwie mam dwa identyfikatory. Muszę umieścić te dwa w pobliżu. Ale jeśli tekst pierwszej etykiety jest za mały, będzie duża luka. Chcę usunąć tę lukę.

+0

Sprawdź moją odpowiedź na sekundę część. – iDev

+0

Spróbuj zaadresować tekst tekstowy. – Vinayak

Odpowiedz

37
//use this for custom font 
CGFloat width = [label.text sizeWithFont:[UIFont fontWithName:@"ChaparralPro-Bold" size:40 ]].width; 

//use this for system font 
CGFloat width = [label.text sizeWithFont:[UIFont systemFontOfSize:40 ]].width; 

label.frame = CGRectMake(point.x, point.y, width,height); 

//point.x, point.y -> origin for label; 
//height -> your label height; 
+0

Działa świetnie! Dzięki! – badweasel

+12

przestarzałe w iOS 7.0 – santuxus

3

Spróbuj tych opcji

UIFont *myFont = [UIFont boldSystemFontOfSize:15.0]; 
// Get the width of a string ... 
CGSize size = [@"Some string here" sizeWithFont:myFont]; 

// Get the width of a string when wrapping within a particular width 
NSString *mystring = @"some strings some string some strings..."; 
CGSize size = [mystring sizeWithFont:myFont 
           forWidth:150.0 
       lineBreakMode:UILineBreakModeWordWrap]; 

Można też spróbować z [label sizeToFit]; Stosując tę ​​metodę, można ustawić ramkę dwóch etykietach,

[firstLabel sizeToFit]; 
[secondLabel sizeToFit]; 
secondLabel.frame = CGRectMake(CGRectGetMaxX(firstLabel.frame), secondLabel.origin.y, secondLabel.frame.size.width, secondLabel.frame.size.height); 
2

sizeWithFont constrainedToSize:lineBreakMode: to oryginalna metoda. Oto przykład, jak go używać jest poniżej:

//Calculate the expected size based on the font and linebreak mode of your label 
CGSize maximumLabelSize = CGSizeMake(296,9999); 

CGSize expectedLabelSize = [yourString sizeWithFont:yourLabel.font constrainedToSize:maximumLabelSize lineBreakMode:yourLabel.lineBreakMode]; 

//adjust the label the the new height. 
CGRect newFrame = yourLabel.frame; 
newFrame.size.height = expectedLabelSize.height; 
yourLabel.frame = newFrame; 
0

Spróbuj wykonać następujące czynności:

/* Consider these two labels as the labels that you use, 
and that these labels have been initialized */ 

UILabel* firstLabel; 
UILabel* secondLabel; 

CGSize labelSize = [firstLabel.text sizeWithFont:[UIFont systemFontOfSize:12]]; 
//change the font size, or font as per your requirements 

CGRect firstLabelRect = firstLabel.frame; 

firstLabelRect.size.width = labelSize.width; 
//You will get the width as per the text in label 

firstLabel.frame = firstLabelRect; 


/* Now, let's change the frame for the second label */ 
CGRect secondLabelRect; 

CGFloat x = firstLabelRect.origin.x; 
CGFloat y = firstLabelRect.origin.y; 

x = x + labelSize.width + 20; //There are some changes here. 

secondLabelRect = secondLabel.frame; 
secondLabelRect.origin.x = x; 
secondLabelRect.origin.y = y; 

secondLabel.frame = secondLabelRect; 
+0

Przepraszam ... to nie działa dla mnie .. :( – Dev

+0

Na czym polega problem? –

+0

Edytowałem kod, zobacz, czy działa teraz. –

12

funkcji sizeWithFont: jest przestarzałe w iOS 7.0, więc trzeba użyć sizeWithAttributes: dla iOS 7.0 +. Także suport starsze wersje, ten kod poniżej można stosować:

CGFloat width; 
    if ([[UIDevice currentDevice].systemVersion floatValue] < 7.0) 
    { 
     width = [text sizeWithFont:[UIFont fontWithName:@"Helvetica" size:16.0 ]].width; 
    } 
    else 
    { 
     width = ceil([text sizeWithAttributes:@{NSFontAttributeName: [UIFont fontWithName:@"Helvetica" size:16.0]}].width); 
    } 

Korzystanie z funkcji ceil() na skutek sizeWithAttributes: jest rekomendowana przez dokumentacji Apple:

„Ta metoda zwraca rozmiary ułamkowych; użyć zwrócony rozmiar do size views, musisz podnieść jego wartość do najbliższej większej liczby całkowitej za pomocą funkcji ceil. "

sizeWithAttributes

+0

Jaki jest sens robienia tego po staremu? Dlaczego nie zawsze używać sizeWithAttributes? – clocksmith

1

wystarczy użyć do Jeśli używasz ograniczyć w widoku lub XIb lub komórek

[LBl sizeToFit]; 

jeśli jej nie działa wtedy

dispatch_async(dispatch_get_main_queue(), ^{ 
[LBl sizeToFit]; 
}); 
+0

Uaktualnianie interfejsu użytkownika w głównym wątku jest tutaj kluczowe. Nawet jeśli nie używamy bloków, dobrze jest upewnić się, że wszystkie aktualizacje mają miejsce w głównym wątku. –

4
// In swift 2.0 
    let lblDescription = UILabel(frame: CGRectMake(0, 0, 200, 20)) 
    lblDescription.numberOfLines = 0 
    lblDescription.text = "Sample text to show its whatever may be" 
    lblDescription.sizeToFit() 

    // Its automatically Adjust the height 
+0

Ten fragment kodu może rozwiązać pytanie, [w tym objaśnienie] (http://meta.stackexchange.com/questions/114762/explaining-entirely-code-ans -answers) naprawdę pomaga poprawić jakość twojego posta. Pamiętaj, że odpowiadasz na pytanie przeznaczone dla czytelników w przyszłości, a te osoby mogą nie znać powodów sugestii dotyczących kodu. – SmokeDispenser

+1

@Vinayak mahadev dostarcza wyjaśnienia na temat fragmentu kodu. – user23790

+0

Cześć SmokeDispenser, Po prostu daje lblDescription.sizeToFit() Automatycznie będzie rozszerzać wysokość UILabel.Nie musimy niczego kalkulować. – Vinayak

Powiązane problemy