2015-02-10 14 views
7

पैराग्राफ में लाइनों की संख्या को सीमित करने का कोई तरीका है NSAttributedString?
मैं में दो तारों को जोड़ रहा हूं NSAttributedString और मैं उन्हें अधिकतम 3 लाइनों के लिए चाहता हूं, पहली स्ट्रिंग 1-2 लाइनें होगी, यदि आवश्यक हो तो छोटा कर दिया जाएगा।
सीमाओं की एनएसएट्रिब्यूटेड स्ट्रिंग संख्या

this is my first string 
if its too long i't will get trun... 
But this is my second string 

मैं क्या किया है::

// First string 
    NSAttributedString *first = [[NSAttributedString alloc] initWithString:@"this is my first string if its too long i't will get trunticated" 
                   attributes:@{NSForegroundColorAttributeName:[UIColor redColor], 
                      NSFontAttributeName:[UIFont fontWithName:@"HelveticaNeue-Light" size:17.0]]; 
    [str appendAttributedString:first]; 

    // New line 
    [str appendAttributedString:[[NSAttributedString alloc] initWithString:@"\n"]]; 

    // Add photo count 
    NSAttributedString *second = [[NSAttributedString alloc] initWithString:@"But this is my second string" 
                   attributes:@{NSForegroundColorAttributeName:[UIColor redColor], 
                      NSFontAttributeName:[UIFont fontWithName:@"HelveticaNeue-Light" size:14.0]}]; 
    [str appendAttributedString:second]; 

लेकिन परिणाम है:

this is my first string 
if its too long i't will get 
trunticated 

और दूसरी स्ट्रिंग अंतिम पंक्ति
कुछ की तरह पर हमेशा होना चाहिए पहली स्ट्रिंग पहली 3 लाइनें लेती है और दूसरी स्ट्रिंग को लेबल से बाहर धक्का देती है।
मैं पहले स्ट्रिंग पैराग्राफ को 2 लाइनों तक सीमित कैसे कर सकता हूं?

+0

क्या आप अभी तक इसे हल करने में सक्षम हैं? – ZeMoon

+1

नहीं ... अभी के लिए मैं बस दो अलग-अलग लेबल का उपयोग करता हूं – ItayAmza

उत्तर

0

एक बाधा के साथ लाइनों की संख्या सीमित करें!

आप निम्न मान के साथ अपने UILabel पर एक NSLayoutConstraint जोड़ें:

  • गुण = NSLayoutAttributeHeight (स्टोरीबोर्ड में 'ऊंचाई')
  • संबंध = NSLayoutRelationLessThanOrEqual ('से कम या बराबर' स्टोरीबोर्ड में)
  • निरंतर = ऊंचाई के लिए संख्या के-लाइनों में आप चाहते हैं

स्टोरीबोर्ड दिखाई एकीकरण:

See storyboard integration

1

आप पत्र है कि आपके ग्राफ़िक घटक (UITextView या UITextField) बार-बार अपरकेस और बड़ा चौड़ाई लोगों का उपयोग कर इस को देखने के लिए संभाल कर सकते हैं की राशि भरोसा कर सकते हैं। से अधिक, उपयोग करें:

- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text{} 

प्रत्येक इनपुट की जांच करने के लिए, यदि पर्याप्त मात्रा है, या यदि यह अभी भी अधिक अक्षरों के लिए उपलब्ध है। एक चरित्र सीमा बनाएं और इस विधि को हर बार इसे कम करें।

संबंधित मुद्दे