夥計!我有一個多行標籤,lineBreakMode設置爲UILineBreakModeWordWrap。我如何確定最後一行的寬度?謝謝iOS。確定UILabel的最後一行寬度
5
A
回答
5
這是我做到的。首先將標籤的行放在NSArray中,然後檢查最後一行的寬度。 在viewDidLoad中:
NSArray* lines = [self getSeparatedLinesFromLbl:srcLabel];
NSString *lastLine=[lines lastObject];
float lastLineWidth=[lastLine sizeWithFont:srcLabel.font constrainedToSize:boundingSize lineBreakMode:NSLineBreakByWordWrapping].width;
而且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 – suthar 2015-05-06 13:21:52
1
夫特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
}
}
用法
print(yourLabel.lastLineWidth)
斯威夫特3(IOS 10.3)
相關問題
- 1. 獲取多行UILabel的最後一行的寬度
- 2. IOS調整UILabel寬度
- 3. 更改寬度的細節UILabel - iOS
- 4. UILabel幀寬度不正確的值
- 5. 如何在設置文本後獲得UILabel的正確寬度?
- 6. iOS UILabel高度
- 7. 如何創建最大寬度的多行UILabel?
- 8. 自動調整UILabel在iOS中的位置和高度寬度
- 9. 如何確定RichTextBox中最後一行的文本高度?
- 10. 確定要放在div中的文本的數量以最大化最後一行的寬度
- 11. 多行UILabel與內容大小相同的寬度UILabel
- 12. 如何計算固定寬度的UILabel的行數?
- 13. 帶固定最大線數的UILabel iOS
- 14. UILabel文本多行給定文本的寬度
- 15. 如何確定iOS上的實際視口寬度和高度
- 16. 根據VW單位確定寬度,同時確保最小寬度
- 17. 確定的TLabel的寬度
- 18. 確定的UILabel
- 19. 特定寬度後的文字換行
- 20. 確定最後一行在Ruby中
- 21. UILabel Autolayout寬度問題
- 22. 確定元素的寬度
- 23. 最小寬度,最大寬度的CSS使用最小寬度
- 24. 如何知道最後一行標籤的寬度?
- 25. 是否可以動態獲取固定寬度的文本的最後一行?
- 26. UILabel固定寬度/固定高度獲取文本編號
- 27. 「:之後」元素的高度和寬度是如何確定的?
- 28. iOS固定寬度ScrollView
- 29. iOS中的UIPickerView最大寬度9
- 30. 確定SDL_surface的寬度/高度的SDL_function?
我不知道任何方式做到這一點。但爲什麼你想要這個?也許如果我們知道爲什麼你想要這個,我們可以想出另一種方法來解決你的問題。 – Rob 2012-04-17 14:25:12
我可以想象一些複雜的例程,你反覆使用NSString的'sizeWithFont:constrainedToSize:lineBreakMode:',一次添加一個單詞,找出哪個單詞將你推到下一行,然後重複這個過程,直到你到達最後行,然後最後一個'sizeWithFont:constrainedToSize:lineBreakMode:'來確定最後一行的寬度。 – Rob 2012-04-17 14:30:43
我用這種方法,但它看起來很複雜,所以我想知道是否有一些很好的解決方案。不管怎樣,謝謝! – leon4ic 2012-04-30 07:24:05