سؤال

كيف يمكن للمرء تعديل المقتطف التالي (في tableView:cellForRowAtIndexPath:UITableViewController الأسلوب) من "09a - PrefsTable" وصفة من الفصل 6 من اي فون المطور كتاب الطبخ:

if (row == 1) { 
    // Create a big word-wrapped UILabel 
    cell = [tableView dequeueReusableCellWithIdentifier:@"libertyCell"]; 
    if (!cell) { 
        cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:@"libertyCell"] autorelease]; 
        [cell addSubview:[[UILabel alloc] initWithFrame:CGRectMake(20.0f, 10.0f, 280.0f, 330.0f)]]; 
    } 
    UILabel *sv = [[cell subviews] lastObject]; 
    sv.text =  @"When in the Course of human events, it becomes necessary for one people to dissolve the political bands which have connected them with another, and to assume among the powers of the earth, the separate and equal station to which the Laws of Nature and of Nature's God entitle them, a decent respect to the opinions of mankind requires that they should declare the causes which impel them to the separation."; 
    sv.textAlignment = UITextAlignmentCenter; 
    sv.lineBreakMode = UILineBreakModeWordWrap; 
    sv.numberOfLines = 9999; 
    return cell; 
} 

...إلى حجم "sv" UILabel subview و "الخليوي" UITableViewCell أن يكون الحجم كبيرة بما يكفي لتناسب النص (و العمل مع أكثر أو أقل من النص وأنواع أخرى من محاذاة النص)?نظرت إلى UILabel textRectForBounds:limitedToNumberOfLines:طريقة, لكن الوثائق تنص على أنه لا ينبغي أن يسمى مباشرة (يجب فقط أن يتم تجاوز).أنا جربت مع UIView sizeToFit طريقة ، دون نجاح.

تحديث: سألت سؤال جديد عن مشكلتي مع NSString -sizeWithFont:forWidth:lineBreakMode:طريقة.

هل كانت مفيدة؟

المحلول

كان علي أن أفعل هذا يكفي أنني مددت UILabel أن تفعل ذلك بالنسبة لي:

@interface UILabel (BPExtensions)
- (void)sizeToFitFixedWidth:(CGFloat)fixedWidth;
@end

@implementation UILabel (BPExtensions)


- (void)sizeToFitFixedWidth:(CGFloat)fixedWidth
{
    self.frame = CGRectMake(self.frame.origin.x, self.frame.origin.y, fixedWidth, 0);
    self.lineBreakMode = NSLineBreakByWordWrapping;
    self.numberOfLines = 0;
    [self sizeToFit];
}
@end

ثم أن تسمية يكون متغير متعدد الارتفاع ولكن ثابتة العرض فقط:

[myLabel sizeToFitFixedWidth:kSomeFixedWidth];

نصائح أخرى

يجب عليك استخدام NSString هو -sizeWithFont:forWidth:lineBreakMode: طريقة لاسترداد المرتبطة التحجيم مقاييس التسمية الخاصة بك.

أيضا, تغيير numberOfLines الملكية 0 إذا كنت تنوي استخدام هذا الرمز.

NSString's -sizeWithFont:forWidth:lineBreakMode: لا تؤدي في الواقع كلمة التفاف.بدلا من, استخدام -sizeWithFont:constrainedToSize:lineBreakMode: للحصول على دقة عرض و ارتفاع قيمة سلسلة.

جرب هذا:

sv.text =  @"When in the Course of human events, it becomes necessary for one people to dissolve the political bands which have connected them with another, and to assume among the powers of the earth, the separate and equal station to which the Laws of Nature and of Nature's God entitle them, a decent respect to the opinions of mankind requires that they should declare the causes which impel them to the separation."; 
sv.textAlignment = UITextAlignmentCenter; 
sv.lineBreakMode = UILineBreakModeWordWrap; 
sv.numberOfLines = 0;
[sv sizeToFit]; 

أيضا, سوف تحتاج إلى تنفيذ UITableViewDelegate الطريقة:

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath

و قد تعود مجموع ارتفاع الخلية تعديلها من أجل تغيير حجم حقل النص.

ملاحظة واحدة أخرى - حجم لتناسب يجب أن تعمل في الواقع ، إذا كان لديك عدد من خطوط تعيين إلى 0 كما ذكر سابقا.أنها سوف تعطيك الحجم مع زيادة الارتفاع إلى accomidate كلمة-التفاف النص في تسمية وعرض مجموعة أيا كانت التسمية الأصلية العرض كان.

التي لن تساعدك على الرغم من أنها تحتاج إلى الحصول على الحجم في heightForRow قبل الخلية التي تم الحصول عليها, لذلك كنت أفضل حالا حساب ارتفاع الحاجة (و من المحتمل جدا التخزين المؤقت هذا الحساب حتى لا تبطئ طاولة التقديم)

هنا قليلا من التعليمات البرمجية استخدام:

CGSize textSize = [myLabel.text sizeWithFont:myLabel.font];

كان لي مشكلة مماثلة ، كان UITableViewCell التي تم تصميمها في القصص المصورة مثل خلية ثابتة.اعتدت [super tableView:cellForRowAtIndexPath:] للحصول عليه.إذا أردت تغيير حجم UILabel "detailTextLabel" لذلك يناسب النص أنا وضعت على ذلك.كان أسلوب "الحق في التفاصيل".

أنا فقط وضعت النص في بلدي tableView:cellForRowAtIndexPath:.و من في tableView:heightForRowAtIndexPath: عدت

UITableViewCell *cell = [super tableView:tableView cellForRowAtIndexPath:indexPath];
return cell.detailTextLabel.frame.size.height

كان سلسلة طويلة.و أخيرا الخلية واسعة مع 4 أسطر من النص في التسمية.

كان لي مشكلة مماثلة.لقد حللت هذا.

في cellForRowAtIndexPath طريقة تعيين حجم الخط إلى ما تريد.

cell.textLabel.lineBreakMode = UILineBreakModeWordWrap;
cell.textLabel.numberOfLines = 0;    
[cell.textLabel setFont:[UIFont systemFontOfSize:14.0]];
[cell.textLabel sizeToFit];

و في heightForRowAtIndexPath طريقة زيادة حجم الخط.

    CGFloat height;

    UITableViewCell *cell = [self tableView:tableView cellForRowAtIndexPath:indexPath];
    NSString *text = cell.detailTextLabel.text;
    CGSize constraint = CGSizeMake(320, 20000.0f);
    CGSize size = [text sizeWithFont:[UIFont systemFontOfSize:20.0]     constrainedToSize:constraint lineBreakMode:UILineBreakModeWordWrap];
    CGFloat calHeight = MAX(size.height, 44.0f);
    height =  calHeight + (CELL_CONTENT_MARGIN * 2);

    return height;
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top