Cap*_*eal 3 iphone objective-c uiview uilabel ios
我有一个UILabel我在屏幕中间居中.数值最多可包含三位数.例如500.
我的问题是,当值改变时,它会使UILabel位置偏离中心.
在下面的图像中,您将看到当值等于3位时,滑块标签看起来居中,当滑块标签值为12时,标签看起来偏离中心.
我需要UILabel调整自己总是在中心UIView.
我已经尝试了下面的代码添加到我UILabel没有运气.

self.myCircleSliderValueLabel.font = sliderValueFont;
self.myCircleSliderValueLabel.textColor = [UIColor colorWithRed:103/255.0 green:187/255.0 blue:230/255.0 alpha:1.0];;
self.myCircleSliderValueLabel.numberOfLines = 0;
self.myCircleSliderValueLabel.lineBreakMode = NSLineBreakByWordWrapping;
self.myCircleSliderValueLabel.text = @"400";
[self.myCircleSlider addSubview:self.myCircleSliderValueLabel];
self.myCircleSliderValueLabel.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin;
Run Code Online (Sandbox Code Playgroud)
最简单快捷的方法是为您设置固定宽度myCircleSliderValueLabel(以容纳所有三位数)并将textAlignment属性设置为NSTextAlignmentCenter.
编辑:
或者您可以使用自动布局.像这样......
NSLayoutConstraint *con = [NSLayoutConstraint
constrainWithItem:self.myCircleSliderValueLabel
attribute:NSLayoutAttributeCenterX // It says that X center
relatedBy:NSLayoutRelationEqual // Should equal
toItem:self.myCircleSliderValueLabel.superview // To your label superview's...
attribute:NSLayoutAttributeCenterX // ... X center
multiplier:1.0
constant:0.0];
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
233 次 |
| 最近记录: |