iOS标签仅显示'';而不是预期的文本
我试图在标签中显示设备的偏航俯仰和横摇。我似乎无法得到要显示的值,它只显示了一个“…”数字应该在哪里。这是我的代码,非常感谢您的帮助iOS标签仅显示'';而不是预期的文本,ios,objective-c,uilabel,Ios,Objective C,Uilabel,我试图在标签中显示设备的偏航俯仰和横摇。我似乎无法得到要显示的值,它只显示了一个“…”数字应该在哪里。这是我的代码,非常感谢您的帮助 #import "ViewController.h" #import <CoreMotion/CoreMotion.h> @interface ViewController (){ } @property (strong, nonatomic) CMMotionManager *motionManager; @end @implementatio
#import "ViewController.h"
#import <CoreMotion/CoreMotion.h>
@interface ViewController (){
}
@property (strong, nonatomic) CMMotionManager *motionManager;
@end
@implementation ViewController
@synthesize motionManager;
@synthesize roll;
@synthesize pitch;
@synthesize yaw;
@synthesize xLabel;
@synthesize yLabel;
@synthesize zLabel;
- (void)viewDidLoad {
[super viewDidLoad];
/** Do any additional setup after loading the view, typically from a nib.
UIAccelerometer *accelerometer = [UIAccelerometer sharedAccelerometer];
accelerometer.updateInterval = 1.0f/60.0f;
accelerometer.delegate = self;
**/
//motionManager = [[CMMotionManager alloc] init];
//motionManager.deviceMotionUpdateInterval = 1.0 / 60.0;
motionManager = [[CMMotionManager alloc] init];
NSTimer *timer;
timer = [NSTimer scheduledTimerWithTimeInterval:1.0/60.0 target:self selector:@selector(doGyroUpdate) userInfo:nil repeats:YES];
//CMDeviceMotion *deviceMotion = motionManager.deviceMotion;
//CMAttitude *attitude = deviceMotion.attitude;
[motionManager startDeviceMotionUpdates];
}
-(void)doGyroUpdate
{
double x = motionManager.deviceMotion.attitude.roll*180/M_PI;
double y = motionManager.deviceMotion.attitude.pitch*180/M_PI;
double z = motionManager.deviceMotion.attitude.yaw*180/M_PI;
NSString *myString = [NSString stringWithFormat:@"%g", x];
xLabel.text = myString;
myString = [NSString stringWithFormat:@"%f", y];
yLabel.text = myString;
myString = [NSString stringWithFormat:@"%f", z];
zLabel.text = myString;
}
#导入“ViewController.h”
#进口
@界面视图控制器(){
}
@属性(强、非原子)CMMotionManager*motionManager;
@结束
@实现视图控制器
@综合经理;
@合成辊;
@合成沥青;
@综合偏航;
@合成交联标记物;
@合成甲基丙烯酰胺;
@合成zLabel;
-(无效)viewDidLoad{
[超级视图下载];
/**加载视图后,通常从nib执行任何其他设置。
UIAccelerator*加速计=[UIAccelerator sharedAccelerometer];
Accelerator.updateInterval=1.0f/60.0f;
加速度计。代表=自身;
**/
//motionManager=[[CMMotionManager alloc]init];
//motionManager.deviceMotionUpdateInterval=1.0/60.0;
motionManager=[[CMMotionManager alloc]init];
n定时器*定时器;
计时器=[NSTimer scheduledTimerWithTimeInterval:1.0/60.0目标:自选择器:@selector(doGyroUpdate)userInfo:nil repeats:YES];
//CMDeviceMotion*deviceMotion=motionManager.deviceMotion;
//c态度*态度=设备运动。态度;
[motionManager StartDeviceMotionUpdate];
}
-(无效)更新
{
双x=运动管理器、设备运动、姿态、滚动*180/M\u PI;
双y=运动管理器、设备运动、姿态、俯仰*180/M\u PI;
双z=运动管理器、设备运动、姿态、偏航*180/M_-PI;
NSString*myString=[NSString stringWithFormat:@“%g”,x];
xLabel.text=myString;
myString=[NSString stringWithFormat:@“%f”,y];
yLabel.text=myString;
myString=[NSString stringWithFormat:@“%f”,z];
zLabel.text=myString;
}
..
表示用于显示文本的UILabel
没有其应有的宽度
尝试在故事板或xib文件中使其更宽。尝试-
label.numberOfLines = 1;
label.minimumFontSize = 6;
label.adjustsFontSizeToFitWidth = YES;
或者您可以通过故事板(自动收缩选项)进行尝试-
您可以为此使用自动布局。这里我给出了2个约束条件 1.从superView俯视 2.从superview引导 现在,无论您的文本是什么,它都将显示,而不显示。
让标签更宽。仅供参考-查找更新的教程。不再需要
@synthesis
行了。谢谢你的帮助!现在,我该如何使数字只显示到小数点后两位?您可以搜索如何做到这一点。请不要只要求别人给你答案。在提问之前做一些调查。你将从自己做研究和尝试中学到比简单地问每一个细节要多得多的东西。为什么?这将在已经很小的标签中显示很小的文本。正确的解决方案是使标签更宽,而不是使用小字体。@rmaddy yes解决方案是增加标签的宽度,但如果不可能,上述解决方案会有所帮助。这两个约束如何确保标签足够宽,可以容纳给定的文本?这两个约束只设置标签的位置,而不设置其宽度。它的宽度与所需的宽度相同。这一切都取决于标签内容。你可以试试看,它会很好用的。@maddy是否为你做了这件事。这里我没有给出固定宽度的限制,因为它会限制那么多的宽度。所以无论你的内容是什么,它都会显示完整的内容…如果它仍然不适用于你。那么告诉我,在这种情况下我会提供帮助