objective c - كيفية تعيين عنوان UIButton كمحاذاة لليسار؟
objective-c (7)
أحتاج إلى عرض عنوان البريد الإلكتروني من الجانب الأيسر من UIButton
، ولكن يتم وضعه في المركز.
هل هناك أي طريقة لتعيين المحاذاة إلى الجانب الأيسر من UIButton
؟
هذا هو شيفتي الحالية:
UIButton* emailBtn = [[UIButton alloc] initWithFrame:CGRectMake(5,30,250,height+15)];
emailBtn.backgroundColor = [UIColor clearColor];
[emailBtn setTitle:obj2.customerEmail forState:UIControlStateNormal];
emailBtn.titleLabel.font = [UIFont systemFontOfSize:12.5];
[emailBtn setTitleColor:[[[UIColor alloc]initWithRed:0.121 green:0.472 blue:0.823 alpha:1]autorelease] forState:UIControlStateNormal];
[emailBtn addTarget:self action:@selector(emailAction:) forControlEvents:UIControlEventTouchUpInside];
[elementView addSubview:emailBtn];
[emailBtn release];
في Swift 3+:
button.contentHorizontalAlignment = .left
ل Swift 2.0:
emailBtn.contentHorizontalAlignment = UIControlContentHorizontalAlignment.Left
هذا يمكن أن يساعد إذا كان هناك حاجة واحد.
هنا يتم شرح كيفية القيام بذلك ولماذا يعمل ذلك: http://cocoathings.blogspot.com/2013/03/how-to-make-uibutton-text-left-or-right.html
يمكنك أيضًا استخدام أداة إنشاء الواجهة إذا كنت لا تريد إجراء التعديلات في الشفرة. هنا غادرت محاذاة النص وأقسمه أيضًا:
لا تنس أنه يمكنك أيضًا محاذاة صورة في الزر أيضًا:
يوجد خطأ صغير في كودDyingCactus. هذا هو الحل الصحيح لإضافة UILabel إلى UIButton لمحاذاة نص الزر للتحكم بشكل أفضل في الزر 'title':
NSString *myLabelText = @"Hello World";
UIButton *myButton = [UIButton buttonWithType:UIButtonTypeCustom];
// position in the parent view and set the size of the button
myButton.frame = CGRectMake(myX, myY, myWidth, myHeight);
CGRect myButtonRect = myButton.bounds;
UILabel *myLabel = [[UILabel alloc] initWithFrame: myButtonRect];
myLabel.text = myLabelText;
myLabel.backgroundColor = [UIColor clearColor];
myLabel.textColor = [UIColor redColor];
myLabel.font = [UIFont fontWithName:@"Helvetica Neue" size:14.0];
myLabel.textAlignment = UITextAlignmentLeft;
[myButton addSubview:myLabel];
[myLabel release];
أتمنى أن يساعدك هذا....
شركة
UIButton *btn;
btn.contentVerticalAlignment = UIControlContentVerticalAlignmentTop;
btn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;