<分区>
我认为大多数开发者在尝试对应用进行多语言本地化时都遇到过这个问题。
例如,我需要将下面的句子本地化为不同的语言,但是有一些单词用户可以单击以转到另一个页面。现在,如果其他语言中的单词比英语长或短,则很难使它们作为整个句子正确对齐,尤其是当有两个可点击部分时。对于点击部分,我想我们只能使用UIButton,或者还有其他好的建议吗?我相信一定有某种工业化的方法来做到这一点。
“条款”和“隐私政策”可点击:
最佳答案
因此,您可以按照以下步骤来完成您的工作。
创建一个 NSMutableString
NSString *initString = @"You agree to our terms and policy";
NSMutableAttributedString *nsms = [[NSMutableAttributedString alloc] initWithString:initString];
[nsms addAttribute:NSForegroundColorAttributeName value:[UIColor greenColor] range:[initString rangeOfString:@"terms"]];
[nsms addAttribute:NSForegroundColorAttributeName value:[UIColor greenColor] range:[initString rangeOfString:@"policy"]];
[nsms addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:15] range:NSMakeRange(0, initString.length)];
[nsms addAttribute:NSLinkAttributeName value:[NSURL URLWithString:@"http://stackoverflow.com/questions/40311151/ios-localization-best-practice-for-uilabel-uibutton"] range:[initString rangeOfString:@"policy"]];`
将属性字符串设置为 UITextView
UITextView *TextView = [[UITextView alloc] initWithFrame:CGRectMake(20, 100, [[UIScreen mainScreen] bounds].size.width - 40, 100)];
TextView.delegate = self;
TextView.attributedText = nsms;
TextView.editable = NO;
[self.view addSubview:TextView];
UITextView 的委托(delegate)函数内部
- (BOOL)textView:(UITextView *)textView shouldInteractWithURL:(NSURL *)URL inRange:(NSRange)characterRange interaction:(UITextItemInteraction)interaction {
// According to range condition/url condition
[self goToOtherPage];
return NO;
}
这里,goToOtherPage
只是一个自定义函数,您可以在其中为您的segue编写代码
关于UILabel + UIButton iOS 本地化最佳实践,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40311151/