2009-06-18 11 views
15

का आकार बदलें, मैं लेबल की ऊंचाई और लेबल के अनुसार लेबल की ऊंचाई के अनुसार सेल की ऊंचाई का आकार बदलना चाहता हूं। या UITextView में दर्ज किए गए पाठ के अनुसार सेल की ऊंचाई का आकार बदलने का कोई तरीका है?UILabelViewCell को UILabel की ऊंचाई को गतिशील रूप से

+0

क्या आप कह रहे हैं कि आप UITextViewCell का आकार बदलना चाहते हैं जिसमें UITextView शामिल है क्योंकि टेक्स्ट टेक्स्ट में अधिक टेक्स्ट दर्ज किया गया है? – teabot

+0

कोई महोदय, मेरे पास सेल पर लेबल वाला एक कस्टम सेल है। और डेटाबेस डेटाबेस से आ रहा है। मैं सेल पंक्ति ऊंचाई और लेबल लाइनों या लेबल ऊंचाई की लेबल कैसे बढ़ा सकता हूं ताकि लंबे टेक्स्ट को सेल के लेबल –

+0

में संभावित डुप्लिकेट [सबव्यू के साथ परिवर्तनीय UITableCellView ऊंचाई] (http://stackoverflow.com/questions/128012/ परिवर्तनीय-uitablecellview-height-with-subview) – outis

उत्तर

17

इस विधि को आईओएस 7.0 से हटा दिया गया है।

heightForRowAtIndexPath नामक प्रतिनिधि विधि है जिसे सेल या टेबल बनाने से पहले कहा जाता है।

आप NSIndexPath इसे करने के लिए पारित कर दिया प्रयोग कर एक विशेष पंक्ति में पाठ पाने के लिए और उस पंक्ति के लिए एक CGSize गणना करने के लिए UIStringDrawing.h से sizeWithFont विधि का उपयोग कर सकते हैं।

उदाहरण के लिए:

CGSize size = [text sizeWithFont:font 
        constrainedToSize:maximumLabelSize 
        lineBreakMode:UILineBreakModeWordWrap]; 

और अंत में आप size.height लौट आते हैं।

+3

यह विधि आईओएस 7 में बहिष्कृत कर दी गई है। – mskw

6

--FOr iOS7--

जोश वेरा के जवाब के बंद इस आधार पर ... heightForRowAtIndexPath में रखें।

मेरे पास मेरा टेबल डेटा एनएसएआरएआर * टेबलडाटा में संग्रहीत है।

// set the desired width of the textbox that will be holding the adjusted text, (for clarity only, set as property or ivar) 
CGFloat widthOfMyTexbox = 250.0; 

-(float)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    //Get a reference to your string to base the cell size on. 
    NSString *cellText = [self.tableData objectAtIndex:indexPath.row]; 
    //set the desired size of your textbox 
    CGSize constraint = CGSizeMake(widthOfMyTextBox, MAXFLOAT); 
    //set your text attribute dictionary 
    NSDictionary *attributes = [NSDictionary dictionaryWithObject:[UIFont systemFontOfSize:14.0] forKey:NSFontAttributeName]; 
    //get the size of the text box 
    CGRect textsize = [cellText boundingRectWithSize:constraint options:NSStringDrawingUsesLineFragmentOrigin attributes:attributes context:nil]; 
    //calculate your size 
    float textHeight = textsize.size.height +20; 
    //I have mine set for a minimum size 
    textHeight = (textHeight < 50.0) ? 50.0 : textHeight; 

    return textHeight; 
} 

मैं आईओएस < 7 के लिए यह परीक्षण नहीं किया है, लेकिन मेरा मानना ​​है कि यह है कि के लिए भी काम करना चाहिए।

+0

क्या आप समझा सकते हैं कि चौड़ाई OfMyTextBox किस संदर्भ में है? – Scratcha

+0

स्क्रैचा अद्यतन अद्यतन देखें। यह चौड़ाई है कि आप अपना टेक्स्टबॉक्स होना चाहते हैं। – digitalHound