【问题标题】:How can we fix UItextfields text and UIbuttons title labels exactly at center position我们如何将 UItextfields 文本和 UIbuttons 标题标签精确地固定在中心位置
【发布时间】:2015-10-19 06:28:47
【问题描述】:

嗨,我是 ios 的初学者,我正在尝试将 UItextfield“文本”数据准确插入 y 轴的中心,如下图文本字段图像所示

我想插入 UIbutton“titleLabel 文本”正好是 x 轴的中心和 y 轴的中心,就像下面的按钮图片一样,我尝试了很多,但没有得到结果,请帮帮我

我的代码:-

 [self.TripIdTxt setValue:[UIColor blackColor] forKeyPath:@"_placeholderLabel.textColor"];
    self.TripIdTxt.layer.sublayerTransform = CATransform3DMakeTranslation(10.0f, 0.0f, 0.0f);

    self.TripIdTxt.backgroundColor = [UIColor whiteColor];
    self.TripIdTxt.layer.cornerRadius = 3.0f;
    self.TripIdTxt.layer.masksToBounds = YES;
    [self.TripIdTxt setContentHorizontalAlignment:UIControlContentHorizontalAlignmentCenter];
    [self.TripIdTxt setContentVerticalAlignment:UIControlContentVerticalAlignmentCenter];
    self.TripIdTxt.textColor = [UIColor blackColor];
    self.TripIdTxt.font = [UIFont fontWithName:bitter_Regular size:14];

【问题讨论】:

  • 你试过 label.textAlignment = NSTextAlignmentCenter 吗?
  • 我只能设置 x 轴的标题标签中心,但我也无法使用您的答案设置 y 轴的中心
  • 好的,也许你应该尝试使用 NSMutableParagraphStyle *style = [[NSMutableParagraphStyle alloc] init]; style.minimumLineHeight = "你的字体大小" + 5; style.maximumLineHeight = YOUR FONTSIZE" + 5; NSDictionary *attributes = @{NSParagraphStyleAttributeName : style}; yourLabel.setAttributedText = [[NSAttributedString alloc] initWithString:[NSString stringWithFormat:@"%@", attributes:attributes]];跨度>

标签: ios objective-c


【解决方案1】:

设置

UIButton

yourbtnName.titleLabel.textAlignment = NSTextAlignmentCenter;

y 轴

[yourbtnName setContentHorizontalAlignment:UIControlContentHorizontalAlignmentCenter];

X 轴

[yourbtnName setContentVerticalAlignment:UIControlContentVerticalAlignmentCenter];

UITextFields

yourtextfieldName.textAlignment = NSTextAlignmentCenter;

您可以直接在情节提要中访问,例如

有关更多信息,请参阅link

使用 TextField 填充

UIView *paddingView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 10, 20)];
TripIdTxt.leftView = paddingView;
self. TripIdTxt.leftViewMode = UITextFieldViewModeAlways;

否则使用变换

TripIdTxt.layer.sublayerTransform = CATransform3DMakeTranslation(10.0f, 0.0f, 0.0f);

【讨论】:

  • yes andbu.kartik 但是使用您的答案我只能设置 x 轴的标题标签中心,但我也无法设置 y 轴的中心
  • @Anil -- 检查更新的答案,它也适用于文本字段
  • @Anil -- 祝你有快乐的一天,我的兄弟
  • Anbu.Kartik 当我为 UItextfild 应用 leftpadding 视图时,一个小帮助即将到来我的意思是文本字段文本未设置在 x 轴的中心 [self.TripIdTxt setValue:[UIColor blackColor] forKeyPath :@"_placeholderLabel.textColor"]; self.TripIdTxt.leftViewMode = UITextFieldViewModeAlways; UIView *paddingView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 10, 20)]; self.TripIdTxt.leftView = paddingView;
  • 为什么要告诉我reson?
【解决方案2】:

设置标签属性的文本对齐中心。

mainlabel.textAlignment = NSTextAlignmentCenter;

【讨论】:

    【解决方案3】:

    你使用xib设置对齐方式

    你也可以使用xib by

    来设置它的垂直对齐方式

    您可以通过编程方式为文本字段和按钮设置内容的垂直对齐方式

    button.contentVerticalAlignment = UIControlContentVerticalAlignmentBottom;
    txtField.contentVerticalAlignment = UIControlContentHorizontalAlignmentRight;
    

    希望对您有所帮助。 快乐编码.. :)

    【讨论】:

      猜你喜欢
      • 2016-01-18
      • 1970-01-01
      • 1970-01-01
      • 2021-12-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多