人!私は複数の線でラベルを持っています、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
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. コードをリアルタイムで最終決定する方法を教えてください。
- 2. 行の列幅を "display:table"に設定してください
- 3. iOS - AutolayoutでUILabelの最大幅を設定する
- 4. 別のUILabelの裏にUILabelを送ってください(Swift)
- 5. QScrollAreaの最小幅を適切に設定してください
- 6. 固定幅divを等幅フォントで埋めてください
- 7. テキストの入力幅は最大で1行に入力してください
- 8. サイドバーコンポーネントの幅(フレックス)を指定してください
- 9. 用紙の幅をlaravel-excelに設定してください
- 10. 最終的にはDelphiで試してみてください。
- 11. をクリックしてください<th>をクリックしてください:<th>を100%の幅と高さに設定してください
- 12. インターネットからダウンロードした画像をUIlabelに設定してください
- 13. ViewControllerを終了し、別のViewControlloer-IOS-Swiftを提示してください。
- 14. プログラムで固定幅、動的高さUILabel?
- 15. MySQLは、特定の値の最新の行を選択してください
- 16. 3つの値のうち最も高い値を決定し、その値を追加してください
- 17. 月の最終日に従ってレコードを選択してください
- 18. 年の最終日に従ってレコードを選択してください
- 19. Gridviewの最初の5行を確認してください
- 20. 最後の行の「m 1」を説明してください。
- 21. 終了してウィンドウを最小化しないでください。
- 22. UIImageの高さを幅と同じに設定してください。
- 23. symfony 1.4の最初のschema.ymlを設定してください
- 24. libphonenumberを使用して国内のキャリアコードを決定してください
- 25. monetdb最後のn行を選択してください
- 26. PHP最後の行を確認してください
- 27. 最新の行を選択してください
- 28. ユニークキーの最大日付行を選択してください
- 29. ipythonの最後のコマンドの終了ステータスを確認してください
- 30. ブラックベリーの最小バージョンを指定してください
私はこれを行うにはどのような方法を知りません。しかし、なぜこれが欲しいのですか?もしあなたがなぜこれを望んでいるのか分かっていれば、あなたの問題を解決する別の方法を考え出すことができます。 – Rob
NSStringの 'sizeWithFont:constrainedToSize:lineBreakMode:'を繰り返し使用する複雑なルーチンを想像すると、一度に単語を追加し、次の行に移動する単語を特定し、最後に到達するまでこのプロセスを繰り返します最後の 'sizeWithFont:constrainedToSize:lineBreakMode:'を使って、最終行の幅を計算します。 – Rob
私はそのアプローチを使用しましたが、かなり複雑に見えるので、問題の解決策があるのかどうか疑問に思っていました。とりあえずありがとう! – leon4ic