UILabel automatyczna zmiana rozmiaru na podstawie wyświetlanego tekstu
Pracuję nad aplikacją, w której jestem zobowiązany do autoresize obszaru tekstowego na podstawie tekstu, który ma być wyświetlany.
Po pierwsze, nie jestem pewien czy powinienem użyć UILabel
(logicznie jest to najlepszy wybór do wyświetlania tekstu statycznego, co jest w moim przypadku) lub UITextView
.
Jak chcę go wykorzystać?
Chcę po prostu zainicjować Mój widok etykiety lub tekstu za pomocą tekstu.
Zamiast tego najpierw definiuję ramkę, a następnie ograniczam mój tekst w tym miejsce.
Przejrzałem dokumentację i inne referencje, ale nie znalazłem zbyt wiele, co mogłoby mi tu pomóc lub mogłem to przeoczyć.
10 answers
Metoda sizeToFit
zadziałała świetnie.
UILabel *testLabel =[[UILabel alloc] initWithFrame:CGRectMake(6,3, 262,20 )]; // RectMake(xPos,yPos,Max Width I want, is just a container value);
NSString * test=@"this is test this is test inthis is test ininthis is test inthis is test inthis is test in uilabel ...this is test in uilabel ...this is test in uilabel ...this is test in uilabel ...this is test in uilabel ...this is test in uilabel ...this is test in uilabel ...this is test in uilabel ...this is test in uilabel ...this is test in uilabel ...this is test in uilabel ...this is test in uilabel ...this is test in uilabel ...this is test in uilabel ...this is test in uilabel ...this is test in uilabel ...this is test in uilabel ...";
testLabel.text = test;
testLabel.numberOfLines = 0; //will wrap text in new line
[testLabel sizeToFit];
[self.view addSubview:testLabel];
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2011-03-27 15:39:06
Rozmiar tekstu można znaleźć za pomocą :
CGSize textSize = [[myObject getALongText]
sizeWithFont:[UIFont boldSystemFontOfSize:15]
constrainedToSize:CGSizeMake(maxWidth, 2000)
lineBreakMode:UILineBreakModeWordWrap];
Wtedy możesz utworzyć swoje UILabel
w ten sposób:
UILabel * lbl = [[UILabel alloc] initWithFrame:CGRectMake(0,0,textSize.width, textSize.height];
[lbl setNumberOfLines:0];
[lbl setLineBreakMode:UILineBreakModeWordWrap];
[lbl setText:[myObject getALongText]];
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2011-03-25 10:04:13
W Języku Swift:
testLabel = UILabel(frame: CGRectMake(6, 3, 262, 20))
testLabel.text = test
testLabel.numberOfLines = 0
testLabel.sizeToFit()
W Celu C
UILabel *testLabel = [[UILabel alloc] initWithFrame: CGRectMake(6, 3, 262, 20)]];
testLabel.text = test;
testLabel.numberOfLines = 0;
[testLabel sizeToFit];
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2016-01-06 14:08:48
Jeśli chcesz zmienić rozmiar Etykiety tylko w wysokości, użyj tego:
@property (nonatomic, weak) IBOutlet UILabel *titleLabel;
CGRect titleLabelBounds = self.titleLabel.bounds;
titleLabelBounds.size.height = CGFLOAT_MAX;
// Change limitedToNumberOfLines to your preferred limit (0 for no limit)
CGRect minimumTextRect = [self.titleLabel textRectForBounds:titleLabelBounds limitedToNumberOfLines:2];
CGFloat titleLabelHeightDelta = minimumTextRect.size.height - self.titleLabel.frame.size.height;
CGRect titleFrame = self.titleLabel.frame;
titleFrame.size.height += titleLabelHeightDelta;
self.titleLabel.frame = titleFrame;
Teraz możesz użyć titleLabelHeightDelta
do układania innych widoków w zależności od rozmiaru etykiety (bez użycia autolayout).
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2016-04-14 06:31:40
Nie jestem pewien, czy całkowicie rozumiem pytanie, ale możesz użyć metody sizeToFit
Na UILabel
(metoda jest dziedziczona z UIView
), aby zmienić rozmiar zgodnie z tekstem etykiety.
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2011-03-25 10:04:43
Najprostszy sposób, aby znaleźć nie. linii w zależności od tekstu. Możesz użyć tego kodu:
ceil(([aText sizeWithFont:aFont].width)/self.bounds.size.width-300);
Zwraca pewną wartość zmiennoprzecinkową.
[lbl setNumberOfLines:floatvalue];
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2011-11-26 18:37:48
Należy pamiętać, że przy wywołaniu autolayout sizetofit nie zmieni rozmiaru, ponieważ zostanie później zmieniony przez obliczenia autolayout. W tym przypadku musisz ustawić odpowiednie ograniczenie wysokości dla Twojego UILabel z wartością "height > = xx".
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2015-11-16 09:36:40
Ponieważ zamierzasz używać tego rozwiązania niezliczoną ilość razy w swojej aplikacji, wykonaj następujące czynności:
1) Utwórz katalog o nazwie extensions
i dodaj nowy plik o nazwie UILabel.swift
o następującym kodzie:
import UIKit
extension UILabel {
func resizeToText() {
self.numberOfLines = 0
self.sizeToFit()
}
}
2) w kodzie aplikacji zdefiniuj żądaną szerokość etykiety i po prostu zadzwoń resizeToText()
:
label.frame.size.width = labelWidth
label.resizeToText()
Spowoduje to zachowanie szerokości przy jednoczesnym automatycznym zwiększaniu wysokości.
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2016-03-18 22:28:44
Wypróbuj poniższy kod, działa dla multiline
self.labelText.text = string;
self.labelText.lineBreakMode = NSLineBreakByWordWrapping;
self.labelText.numberOfLines = 0;
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2016-04-14 06:29:27
Możesz zmienić rozmiar etykiety na podstawie długości ciągu znaków za pomocą tej funkcji
func ChangeSizeOfLabel(text:String) -> CGSize{
let font = UIFont(name: "HelveticaNeue", size: 12)!
let textAttributes = [NSFontAttributeName: font]
let size = text.boundingRectWithSize(CGSizeMake(310, 999), options: .UsesLineFragmentOrigin, attributes: textAttributes, context: nil)
let adjustSize = CGSizeMake(size.width, size.height)
return adjustSize
}
I użyj go tak:
Let
showLabel.frame = CGRectMake(x, y, width , self.ChangeSizeOfLabel("Hello , Height is changing dynamically.....").height)
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2016-10-12 05:04:46