文本格式问题objective-c
Posted
技术标签:
【中文标题】文本格式问题objective-c【英文标题】:text format trouble objective-c 【发布时间】:2013-09-30 23:14:35 【问题描述】:需要生效,该文本被另一个文本/uiimageview 打断,但无法理解它是如何工作的。例如,我需要制作类似于 ios7 状态栏的界面,其中操作员名称为“Oper ...”+图标+时间。所以我不能以正确的方式做到这一点
operatorName = [self getOperatorName];
UILabel *operatorLabel = [[UILabel alloc] init];
operatorLabel.backgroundColor = [UIColor clearColor];
operatorLabel.font = [UIFont fontWithName:@"HelveticaNeue-Light" size:kStatusBarFontOperatorSize];
operatorLabel.textColor = kStatusBarTextColor;
operatorLabel.shadowOffset = CGSizeMake(0.f, -0.6);
operatorLabel.shadowColor = kStatusBarTextShadow;
operatorLabel.adjustsFontSizeToFitWidth = YES;
operatorLabel.text = operatorName;
operatorLabel.frame = CGRectMake(0.0, 0.0, operatorStrSize.width, operatorStrSize.height);
[operatorLabel sizeToFit];
/* connection type */
UIImageView *conImgView = [[UIImageView alloc] initWithImage:conImg];
/* time in status bar */
time = [self getStatusBarTime];
UILabel *statusBarLabel = [[UILabel alloc] init];
statusBarLabel.font = [UIFont fontWithName:@"HelveticaNeue-Medium" size:kStatusBarFontSize];
statusBarLabel.textColor = kStatusBarTextColor;
statusBarLabel.adjustsFontSizeToFitWidth = YES;
statusBarLabel.text = time;
int maxDistance = imgView.frame.size.width/2 - timeStrSize.width/2;
int connectionPower = 44;
double delimiter = 0;
NSString *cName = [self returnChoosenConnectionName];
if ([cName isEqualToString:@"Wi-Fi"])
delimiter = 6.5;
else
delimiter = 9.5;
int fullLine = connectionPower + operatorLabel.frame.size.width + delimiter + conImgView.frame.size.width;
if (fullLine > maxDistance)
// need to interrupt text in operator name, but how ?
else
// all good placed
x = 44.0;
operatorLabel.frame = CGRectMake(x, 3.0, operatorStrSize.width , operatorStrSize.height);
[operatorLabel sizeToFit];
NSString *cName = [self returnChoosenConnectionName];
if ([cName isEqualToString:@"Wi-Fi"])
x += operatorLabel.frame.size.width + 6.5;
else
x += operatorLabel.frame.size.width + 9.5;
conImgView.frame = CGRectMake(x, 0.0, conImgView.frame.size.width, conImgView.frame.size.height);
[imgView addSubview:operatorLabel];
[imgView addSubview:conImgView];
statusBarLabel.frame = CGRectMake(imgView.frame.size.width/2 - timeStrSize.width/2, 2.5, timeStrSize.width , timeStrSize.height);
[imgView addSubview:statusBarLabel];
我需要什么:
我有什么:
【问题讨论】:
我建议你使用自动布局而不是自己做布局。 不幸的是,我不能为此使用 xib,也不能使用自动布局 xib 不是必需的。您可以通过编程方式创建和设置约束。 【参考方案1】:我建议使用自动布局而不是尝试自己计算所有内容。您可以使用 XIB 并在其中设置约束,或者您可以以编程方式完成所有操作。这是一个示例代码,可以帮助您入门。它以编程方式创建控件并设置约束;它不使用 XIB。
- (void)viewDidLoad
[super viewDidLoad];
/*
Uncomment this if you would like to translate your subviews vertically.
UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 40, self.view.frame.size.width, 50)];
[self.view addSubview:view];
UIView *superview = view;
*/
UIView *superview = self.view;
UILabel *label1 = [[UILabel alloc] init];
label1.font = [UIFont fontWithName:@"HelveticaNeue-Light" size:14.0];
label1.text = @"MTS";
[label1 sizeToFit];
[label1 setTranslatesAutoresizingMaskIntoConstraints:NO];
UILabel *label2 = [[UILabel alloc] init];
label2.font = [UIFont fontWithName:@"HelveticaNeue-Medium" size:14.0];
label2.text = @"1:22 PM";
[label2 sizeToFit];
[label2 setTranslatesAutoresizingMaskIntoConstraints:NO];
UIImageView *image1 = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"strength.png"]];
UIImageView *image2 = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"carrier.png"]];
[image1 setTranslatesAutoresizingMaskIntoConstraints:NO];
[image2 setTranslatesAutoresizingMaskIntoConstraints:NO];
[image1 sizeToFit];
[image2 sizeToFit];
[superview addSubview:image1];
[superview addSubview:label1];
[superview addSubview:image2];
[superview addSubview:label2];
NSDictionary *viewsDictionary = NSDictionaryOfVariableBindings(image1, image2, label1, label2);
NSString *format = [NSString stringWithFormat:@"|-[image1(<=%f)]-[label1]-[image2(<=%f)]-[label2(>=20)]-|",
image1.frame.size.width, image2.frame.size.width];
NSArray *constraints;
constraints = [NSLayoutConstraint constraintsWithVisualFormat:format
options:NSLayoutFormatAlignAllCenterY
metrics:nil
views:viewsDictionary];
[self.view addConstraints:constraints];
在代码中向布局添加视图时,iOS 会尝试将该视图的自动调整掩码转换为自动布局约束。这些自动生成的约束将与应用程序代码中添加的任何约束冲突。必须关闭翻译:
setTranslatesAutoresizingMaskIntoConstraints:NO
代码的结果是:
【讨论】:
非常感谢您的回答!很好的变体,但我已经写了我的算法来计算元素之间的距离,所以它适用于每个位置,也适用于需要裁剪操作符标签时,这可能不是一个好的做法,但工作起来就像一个亮点)以上是关于文本格式问题objective-c的主要内容,如果未能解决你的问题,请参考以下文章