كيفية إضافة نص برمجياً إلى UIView
-
13-09-2020 - |
سؤال
انا املك UIView
التي أود إضافة عدة أجزاء من النص إليها.لقد استخدمت أ UITextView
لكنني أعتقد أن هذا مبالغة لأنه لا يحتاج إلى أن يكون قابلاً للتحرير.فكرت في استخدام أ UILabel
أو أ UITextField
, ، لكنني لا أرى كيف تخبر المشرف بمكان وضع UILabel
أو UITextField
داخل نفسه.أريد أقل كائن بصمة يتيح لي وضع نص الخط/اللون/الحجم الذي أختاره في ملفي UIView
حيث أريد ذلك.ليس كثيرا أن نسأل، إيه؟
المحلول
أبسط نهج بالنسبة لك سيكون:
UILabel *yourLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 10, 300, 20)];
[yourLabel setTextColor:[UIColor blackColor]];
[yourLabel setBackgroundColor:[UIColor clearColor]];
[yourLabel setFont:[UIFont fontWithName: @"Trebuchet MS" size: 14.0f]];
[yourSuperView addSubview:yourLabel];
من المحتمل أن يتطلب منك إنشاء طرق عرض أو نشرها في التعليمات البرمجية الخاصة بك استخدامها CGRectMake
كثيراً.كما يوحي اسمه، فهو يقوم بإنشاء مستطيل يمكنك استخدامه لتعيين الموضع النسبي (بالنسبة لحدود العرض الإشرافي الخاص بك) وحجم العرض الخاص بك UIView-Subclass
(في هذه الحالة أ UILabel
).
يعمل مثل هذا:
yourLabel.Frame = CGRectMake(x, y, width, height); //x,y,width,height are float values.
x
يحدد التباعد بين الحد الأيسر للعرض الفائق وبداية العرض الفرعي الذي أنت على وشك إضافته، وينطبق الشيء نفسه على y
ولكن يتعلق بالتباعد بين الحدود العليا لجهة العرض الإشرافية الخاصة بك.ثم أعتقد أن العرض والارتفاع واضحان بذاتهما.
نأمل أن يضعك هذا على المسار الصحيح.
نصائح أخرى
بدلاً من العثور على طريقة لإخبار العرض بمكان وضع UILabel، يمكنك إخبار UILabel بمكان وضع نفسه في العرض باستخدام "المركز".
على سبيل المثال
myLabel.center = CGPointMake(0.0, 0.0);
أتمنى أن تتمكن من استخدام UILabel، فهو بالنسبة لي هو الشكل الأساسي للنص المرن غير القابل للتحرير.
بالنسبة لسويفت:
let yourLabel = UILabel(frame: CGRectMake(100, 100, 100, 100))
yourLabel.textColor = UIColor.whiteColor()
yourLabel.backgroundColor = UIColor.blackColor()
yourLabel.text = "mylabel text"
yoursuperview.addSubview(yourLabel)
هذا السؤال قديم، ولكن بالنسبة لخيار نص UIView خالص دون استخدام UILabel أو UITextField (كما تصف جميع الإجابات الأخرى، ولكن السؤال هو كيفية القيام بذلك بدونها)، فإن drawRect في UIView فئة فرعية يعمل معي.مثل ذلك:
- (void)drawRect:(CGRect)rect{
NSString *string = @"Hello World!";
[string drawAtPoint:CGPointMake(100, 100) withFont:[UIFont boldSystemFontOfSize:16.0]];
}
يعرض هذا الروتين نصًا في موضع X-Y
-(void)placeText:(NSString *)theText:(int)theX:(int)theY {
UILabel *textLabel;
// Set font and calculate used space
UIFont *textFont = [UIFont fontWithName:@"Helvetica" size:14];
CGSize textStringSize = [theText sizeWithFont:textFont constrainedToSize:CGSizeMake(300,50) lineBreakMode:NSLineBreakByTruncatingTail];
// Position of the text
textLabel = [[UILabel alloc] initWithFrame:CGRectMake(theX+OFFSETIMAGEX-(textStringSize.width/2), theY+OFFSETIMAGEY-(textStringSize.height/2), textStringSize.width,textStringSize.height)];
// Set text attributes
textLabel.textColor = [UIColor blackColor];
textLabel.backgroundColor = [UIColor orangeColor];
textLabel.font = textFont;
textLabel.text = theText;
// Display text
[self.view addSubview:textLabel];
}
قد يكون الوقت متأخرًا ولكن هنا ما أستخدمه: -
CGRect labelFrame = CGRectMake(120,300, 530, 100);
UILabel *myLabel = [[UILabel alloc] initWithFrame:labelFrame];
//If you need to change the color
[myLabel setTextColor:[UIColor whiteColor]];
//If you need to change the system font
[myLabel setFont:[UIFont fontWithName:NULL size:23]];
//If you need alignment
[myLabel setTextAlignment:NSTextAlignmentCenter];
// The label will use an unlimited number of lines
[myLabel setNumberOfLines:0];
//Add label view to current view
[self.view addSubview:myLabel];
NSString *someString = @"Sample String, Yarp!";
myLabel.text = someString;
أضف UILabel إلى طريقة العرض الخاصة بك.ثم قم بتجاوز طريقة LayoutSubviews الخاصة بالعرض.