adamların son satır genişliğini belirleyin! Birden çok satıra sahip bir etiketim var, lineBreakMode UILineBreakModeWordWrap olarak ayarlandı. Son satırın genişliğini nasıl belirleyebilirim? TeşekkürleriOS. UILabel
5
A
cevap
5
İşte nasıl yaptım. Önce etiketinizin satırlarını NSArray'a yerleştirin ve son satırın genişliğini kontrol edin. viewDidLoad :
NSArray* lines = [self getSeparatedLinesFromLbl:srcLabel];
NSString *lastLine=[lines lastObject];
float lastLineWidth=[lastLine sizeWithFont:srcLabel.font constrainedToSize:boundingSize lineBreakMode:NSLineBreakByWordWrapping].width;
ve 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
ios8 için lütfen güncelleme yapabilir misiniz? – suthar
1
Swift 3 (IOS 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
}
}
Kullanım
print(yourLabel.lastLineWidth)
Swift 3 (IOS 10,3)
İlgili konular
- 1. iOS - Shadow yayıldı UILabel
- 2. Konteyneri UILabel [ios] boyutuna büyütün (yükseklik)
- 3. UILabel - iOS için Ayar Kontrolü, Swift
- 4. iOS: UILabel çerçevesini metne göre nasıl ayarlayabilirim?
- 5. UILabel ve UILabel
- 6. UILabel
- 7. UILabel
- 8. UILabel
- 9. UILabel
- 10. Başka bir UILabel (Swift) arkasına UILabel gönder (
- 11. bir UILabel
- 12. UILabel - iPhone
- 13. Uilabel kuyruğu kesilmesi
- 14. iOS: UILabel Programatik Olarak Yazı Tipi boyutunu ayarlama
- 15. iOS: multiline uilabel sadece autolayout ile bir satır gösterir
- 16. Yuvarlak köşeli UILabel (Xcode 4.5 ve iOS 6)
- 17. UILabel metin sarılmıyor
- 18. Iphone'da UILabel eylemi
- 19. UILabel Metninde Değişiklik Algıla
- 20. Bir UILabel içeriği
- 21. UILabel negatif satır aralığı
- 22. mümkün UILabel alt sınıf
- 23. soluklaşıyor, animasyonu uilabel
- 24. Otomatik Boyut UILabel
- 25. UILabel dokunulabilir hale getirme
- 26. animasyon UILabel boyutu
- 27. Satır sonları UILabel
- 28. Dokunarak bir UILabel alfa nasıl değiştirilir?
- 29. UILabel - metin olarak dize ve
- 30. Storyboard'dan bir UILabel yükleyin, cellForRowAtIndexPath
Bunu yapmak için herhangi bir yol bilmiyorum. Ama neden bunu istiyorsun? Belki neden bunu istediğinizi biliyorsak sorununuzu çözmek için başka bir yol bulabiliriz. – Rob
NSString'in “sizeWithFont: kısıtlanmışToSize: lineBreakMode:” işlevini tekrar tekrar kullandığınız ve bir kelimeyi bir defada ekleyerek, hangi kelimenin sizi bir sonraki satıra gönderdiğine karar verdiğiniz karmaşık bir rutini hayal edebiliyorum. satır, ve sonra bir son 'sizeWithFont: kısıtlanmışToSize: lineBreakMode: 'bu son satırın genişliğini anlamaya. – Rob
Bu yaklaşımı kullandım ama oldukça karmaşık görünüyor, bu yüzden sorun için güzel bir çözüm olup olmadığını merak ettim. Neyse teşekkürler! – leon4ic