人!私は複数の線でラベルを持っています、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. iOS - AutolayoutでUILabelの最大幅を設定する
- 2. 別のUILabelの裏にUILabelを送ってください(Swift)
- 3. テキストの入力幅は最大で1行に入力してください
- 4. 最終的にはDelphiで試してみてください。
- 5. をクリックしてください<th>をクリックしてください:<th>を100%の幅と高さに設定してください
- 6. symfony 1.4の最初のschema.ymlを設定してください
- 7. PHP最後の行を確認してください
- 8. ブラックベリーの最小バージョンを指定してください
- 9. ipythonの最後のコマンドの終了ステータスを確認してください
- 10. LatLngBoundsビルダーで最大ズームレベルを設定してください
- 11. NumberInputウィジェットに最大値を指定してください
- 12. Zend MultiCheckbox:最大選択を設定してください
- 13. Oracle NUMBER列の最大値と最小値の決定方法を教えてください。
- 14. ネットのフォームの最大幅または最大高さを変更してください
- 15. 最終結果の2行目の列を2行追加する方法を教えてください。
- 16. UIImageView上で正しいUILabelをサイズ変更してください
- 17. UIAlertViewの幅を変更してください
- 18. メニューの幅を変更してください
- 19. JS jqueryui/fancyboxのクラッシュを解決してください
- 20. javaFXの問題を解決してください。
- 21. ruby rest-client:決してタイムアウトしないでください。
- 22. jQuery UIオートコンプリート:決してポップアップしないでください。
- 23. UILabel固定幅/固定幅テキスト番号を取得
- 24. jtextareaの次の行に行の最後の単語を設定してください
- 25. Googleのオートコンプリートを都市と国に限定してください。iOS Swift
- 26. 行列(R)の特定の行の名前を変更してください
- 27. iOSのWWAN接続のタイプを確認してください
- 28. Metrowerksコンパイラでboost.threadコンパイルエラーを解決してください
- 29. Oracleストアドプロシージャエラーを解決してください。
- 30. PHPファイルによって呼び出される画像の幅と高さを指定してください
私はこれを行うにはどのような方法を知りません。しかし、なぜこれが欲しいのですか?もしあなたがなぜこれを望んでいるのか分かっていれば、あなたの問題を解決する別の方法を考え出すことができます。 – Rob
NSStringの 'sizeWithFont:constrainedToSize:lineBreakMode:'を繰り返し使用する複雑なルーチンを想像すると、一度に単語を追加し、次の行に移動する単語を特定し、最後に到達するまでこのプロセスを繰り返します最後の 'sizeWithFont:constrainedToSize:lineBreakMode:'を使って、最終行の幅を計算します。 – Rob
私はそのアプローチを使用しましたが、かなり複雑に見えるので、問題の解決策があるのかどうか疑問に思っていました。とりあえずありがとう! – leon4ic