facetów! Mam etykietę z wieloma liniami, lineBreakMode jest ustawione na UILineBreakModeWordWrap. Jak określić szerokość ostatniej linii? DziękiiOS. Określ ostatnią szerokość linii UILabel
5
A
Odpowiedz
5
Oto jak to zrobiłem. Najpierw umieść linie etykiety w NSArray, a następnie sprawdź szerokość ostatniej linii. W viewDidLoad:
NSArray* lines = [self getSeparatedLinesFromLbl:srcLabel];
NSString *lastLine=[lines lastObject];
float lastLineWidth=[lastLine sizeWithFont:srcLabel.font constrainedToSize:boundingSize lineBreakMode:NSLineBreakByWordWrapping].width;
I getSeparatedLinesFromLbl:
-(NSArray*)getSeparatedLinesFromLbl:(UILabel*)lbl
{
if (lbl.lineBreakMode != NSLineBreakByWordWrapping)
{
return nil;
}
NSMutableArray* lines = [NSMutableArray arrayWithCapacity:10];
NSCharacterSet* wordSeparators = [NSCharacterSet whitespaceAndNewlineCharacterSet];
NSString* currentLine = lbl.text;
int textLength = [lbl.text length];
NSRange rCurrentLine = NSMakeRange(0, textLength);
NSRange rWhitespace = NSMakeRange(0,0);
NSRange rRemainingText = NSMakeRange(0, textLength);
BOOL done = NO;
while (!done)
{
// determine the next whitespace word separator position
rWhitespace.location = rWhitespace.location + rWhitespace.length;
rWhitespace.length = textLength - rWhitespace.location;
rWhitespace = [lbl.text rangeOfCharacterFromSet: wordSeparators options: NSCaseInsensitiveSearch range: rWhitespace];
if (rWhitespace.location == NSNotFound)
{
rWhitespace.location = textLength;
done = YES;
}
NSRange rTest = NSMakeRange(rRemainingText.location, rWhitespace.location-rRemainingText.location);
NSString* textTest = [lbl.text substringWithRange: rTest];
CGSize sizeTest = [textTest sizeWithFont: lbl.font forWidth: 1024.0 lineBreakMode: NSLineBreakByWordWrapping];
if (sizeTest.width > lbl.bounds.size.width)
{
[lines addObject: [currentLine stringByTrimmingCharactersInSet:wordSeparators]];
rRemainingText.location = rCurrentLine.location + rCurrentLine.length;
rRemainingText.length = textLength-rRemainingText.location;
continue;
}
rCurrentLine = rTest;
currentLine = textTest;
}
[lines addObject: [currentLine stringByTrimmingCharactersInSet:wordSeparators]];
return lines;
}
+0
Czy możesz zaktualizować na ios8 – suthar
1
Swift 3 (OI 10,3)
extension UILabel {
func getSeparatedLines() -> [Any] {
if self.lineBreakMode != NSLineBreakMode.byWordWrapping {
self.lineBreakMode = .byWordWrapping
}
var lines = [Any]() /* capacity: 10 */
let wordSeparators = CharacterSet.whitespacesAndNewlines
var currentLine: String? = self.text
let textLength: Int = (self.text?.characters.count ?? 0)
var rCurrentLine = NSRange(location: 0, length: textLength)
var rWhitespace = NSRange(location: 0, length: 0)
var rRemainingText = NSRange(location: 0, length: textLength)
var done: Bool = false
while !done {
// determine the next whitespace word separator position
rWhitespace.location = rWhitespace.location + rWhitespace.length
rWhitespace.length = textLength - rWhitespace.location
rWhitespace = (self.text! as NSString).rangeOfCharacter(from: wordSeparators, options: .caseInsensitive, range: rWhitespace)
if rWhitespace.location == NSNotFound {
rWhitespace.location = textLength
done = true
}
let rTest = NSRange(location: rRemainingText.location, length: rWhitespace.location - rRemainingText.location)
let textTest: String = (self.text! as NSString).substring(with: rTest)
let fontAttributes: [String: Any]? = [NSFontAttributeName: font]
let maxWidth = (textTest as NSString).size(attributes: fontAttributes).width
if maxWidth > self.bounds.size.width {
lines.append(currentLine?.trimmingCharacters(in: wordSeparators) ?? "")
rRemainingText.location = rCurrentLine.location + rCurrentLine.length
rRemainingText.length = textLength - rRemainingText.location
continue
}
rCurrentLine = rTest
currentLine = textTest
}
lines.append(currentLine?.trimmingCharacters(in: wordSeparators) ?? "")
return lines
}
var lastLineWidth: CGFloat {
let lines: [Any] = self.getSeparatedLines()
if !lines.isEmpty {
let lastLine: String = (lines.last as? String)!
let fontAttributes: [String: Any]? = [NSFontAttributeName: font]
return (lastLine as NSString).size(attributes: fontAttributes).width
}
return 0
}
}
Zastosowanie
print(yourLabel.lastLineWidth)
Swift 3 (IOS 10,3)
Powiązane problemy
- 1. Uzyskaj szerokość ostatniej linii multilinii UILabel
- 2. Określ szerokość i wysokość działki
- 3. UILabel wykrywanie linii podziału
- 4. Jak ograniczyć maksymalną szerokość UILabel?
- 5. Jak znaleźć liczbę linii UILabel
- 6. ustaw maksymalną liczbę linii uilabel
- 7. Usuń ostatnią porcję NSURL: iOS
- 8. iPhone - Dostosuj szerokość UILabel zgodnie z tekstem
- 9. Szerokość linii w raphaeljs
- 10. Java2D: Zwiększ szerokość linii
- 11. Szerokość linii OpenGL
- 12. iOS - Cień rozprzestrzeniony na UILabel
- 13. iOS 9 Breaks animacje UILabel
- 14. Zwiększ szerokość linii linii legendy w matplotlib
- 15. Zasięg linii sed, wszystkie poza ostatnią linią
- 16. iOS - Auto-obkurcz UILabel z przypisanym tekstem
- 17. iOS - Określ kraj użytkownika bez użycia CLLocationManager
- 18. Obcinanie uilabel ogonkiem
- 19. Sprawdź skracanie w UILabel - iOS, Swift
- 20. iOS: ustaw rozmiar czcionki programu UILabel Programowalnie
- 21. Nie można uzyskać wartości UIAutomation iOS UILabel
- 22. Jak wyróżnić tylko tekst w UILabel - IOS
- 23. Określ szerokość 3 znaków dla tekstu wejściowego html
- 24. Podział linii w UILabel jest ignorowany przy kompilacji
- 25. Jak obliczyć szerokość UILabel na podstawie długości tekstu?
- 26. Jak uzyskać prawidłową szerokość UILabel po ustawieniu tekstu?
- 27. Dynamicznie zmienić UILabel nie szerokość pracy z autoLayout
- 28. IOS jak obcinać za pomocą elipsy ostatnią widoczną linię akapitu?
- 29. Implementacja linii papilarnych IOS
- 30. Dostosowywanie wysokości UILabel dynamicznie za pomocą storyboardu iOS
ja nie znam żadnego sposobu, aby to zrobić. Ale dlaczego tego chcesz? Być może, jeśli wiemy, dlaczego tego chcesz, możemy wymyślić inny sposób rozwiązania Twojego problemu. – Rob
Mogę sobie wyobrazić jakąś skomplikowaną rutynę, w której wielokrotnie używałeś NSString 'sizeWithFont: constrainedToSize: lineBreakMode:', dodając słowo na raz, znajdź słowo, które popycha cię do następnego wiersza, a następnie powtarzaj ten proces, aż dojdziesz do ostatniego linii, a następnie ostateczny 'sizeWithFont: constrainedToSize: lineBreakMode:', aby określić szerokość ostatniej linii. – Rob
Użyłem tego podejścia, ale wygląda na dość skomplikowane, więc zastanawiałem się, czy istnieje jakieś dobre rozwiązanie problemu. W każdym razie dzięki! – leon4ic