लोगों की अंतिम पंक्ति चौड़ाई निर्धारित करें! मेरे पास एकाधिक लाइनों वाला एक लेबल है, लाइन ब्रेकमोड UILineBreakModeWordWrap पर सेट है। मैं अंतिम पंक्ति की चौड़ाई कैसे निर्धारित कर सकता हूं? धन्यवादआईओएस। UILabel
5
A
उत्तर
5
यहां मैंने यह कैसे किया है। सबसे पहले अपने लेबल की लाइनें एनएसएआरएआरई में डालें, और फिर अंतिम पंक्ति की चौड़ाई की जांच करें। 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 (आईओएस 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 (आईओएस 10,3)
संबंधित मुद्दे
- 1. UILabel आईओएस
- 2. UIAutomation आईओएस UILabel मान
- 3. UILabel
- 4. UILabel
- 5. uilabel
- 6. UIlabel
- 7. UILabel
- 8. UILabel
- 9. UILabel
- 10. UILabel
- 11. UILabel
- 12. UILabel
- 13. UILabel
- 14. UILabel -
- 15. uilabel
- 16. UILabel/NSTextView बनाम CATextLayer
- 17. गोलाकार कोने (एक्सकोड 4.5 और आईओएस 6) के साथ UILabel
- 18. आईओएस - UILabel कल मेरे सवाल का एक विकलांग लेबल
- 19. आईओएस ऐप में CoreAnimation/QuartzCore के साथ UILabel एनिमेटिंग
- 20. UILabel के लिए अलग अस्पष्टता और UILabel
- 21. uilabel पूंछ truncation
- 22. मल्टीलाइन UILabel
- 23. UILabel टेक्स्ट
- 24. मल्टीलाइन UILabel
- 25. मल्टीलाइन UILabel?
- 26. UILabel Strikethrough
- 27. UILabel/UITextView
- 28. UILabel में लाइन ब्रेक को
- 29. UILabel स्पर्श करने योग्य
- 30. लाइन ब्रेक UILabel
मैं यह करने के लिए किसी भी तरह का पता नहीं है। लेकिन आप यह क्यों चाहते हैं? शायद अगर हम जानते हैं कि आप यह क्यों चाहते हैं तो हम आपकी समस्या का समाधान करने के लिए एक और तरीके से आ सकते हैं। – Rob
मैं कुछ जटिल दिनचर्या की कल्पना कर सकता हूं जहां आप बार-बार एनएसएसटींग के 'आकार के साथ उपयोग करते हैं विथफॉन्ट: अवरुद्ध करने के लिए: लाइनब्रैक मोड:', एक समय में एक शब्द जोड़कर, यह पता लगाएं कि कौन सा शब्द आपको अगली पंक्ति में धक्का देता है, और फिर जब तक आप अंतिम तक नहीं पहुंच जाते, तब तक इस प्रक्रिया को दोहराएं लाइन, और उसके बाद एक अंतिम 'आकार WithFont: बाधित ToSize: lineBreakMode:' उस अंतिम पंक्ति की चौड़ाई को समझने के लिए। – Rob
मैंने उस दृष्टिकोण का उपयोग किया लेकिन यह बहुत जटिल लग रहा है इसलिए मुझे आश्चर्य हुआ कि समस्या के लिए कुछ अच्छा समाधान है या नहीं। कोई बात नहीं धन्यवाद! – leon4ic