我需要解决这个完全相同的问题:包含这两个链接的非常相似的文本,跨越多行,并且需要能够翻译成任何语言(包括不同的词序等)。我刚刚解决了它,所以让我分享一下我是如何做到的。
最初我想我应该创建属性文本,然后将点击的触摸位置映射到该文本中的区域。虽然我认为这是可行的,但我也认为这是一种过于复杂的方法。
这就是我最终所做的:
总结:
- 在您的英文信息中包含非常基本的自定义标记,以便您可以解析出不同的部分
- 指示您的翻译人员保留标记并翻译其余部分
- 有一个 UIView 可以作为这个消息的容器
- 将您的英文信息分成几部分,将常规文本与可点击文本分开
- 为每个部分在容器 UIView 上创建一个 UILabel
- 对于可点击的部分,设置您的样式,允许用户交互并创建您的点击手势识别器
- 做一些非常基本的簿记,以完美地跨行放置单词
详情:
在视图控制器的viewDidLoad我放了这个:
[self buildAgreeTextViewFromString:NSLocalizedString(@"I agree to the #<ts>terms of service# and #<pp>privacy policy#",
@"PLEASE NOTE: please translate \"terms of service\" and \"privacy policy\" as well, and leave the #<ts># and #<pp># around your translations just as in the English version of this message.")];
我正在调用一个构建消息的方法。注意我想出的标记。您当然可以自己发明,但关键是我还标记了每个可点击区域的末端,因为它们跨越多个单词。
这是将消息放在一起的方法 -- 见下文。首先,我通过# 字符(或者更确切地说@"#" 字符串)分解英文消息。这样我就得到了需要单独创建标签的每一件作品。我遍历它们并查找<ts> 和<pp> 的基本标记,以检测哪些片段是指向什么的链接。如果我正在使用的文本块是一个链接,那么我会设置一些样式并为它设置一个点击手势识别器。当然,我也去掉了标记字符。我认为这是一个非常简单的方法。
请注意一些细微之处,例如我如何处理空格:我只是从(本地化)字符串中获取空格。如果没有空格(中文,日文),那么块之间也不会有空格。如果有空格,则那些会根据需要自动将块隔开(例如英语)。但是,当我必须在下一行的开头放置一个单词时,我确实需要确保从该文本中删除任何空白前缀,否则它无法正确对齐。
- (void)buildAgreeTextViewFromString:(NSString *)localizedString
{
// 1. Split the localized string on the # sign:
NSArray *localizedStringPieces = [localizedString componentsSeparatedByString:@"#"];
// 2. Loop through all the pieces:
NSUInteger msgChunkCount = localizedStringPieces ? localizedStringPieces.count : 0;
CGPoint wordLocation = CGPointMake(0.0, 0.0);
for (NSUInteger i = 0; i < msgChunkCount; i++)
{
NSString *chunk = [localizedStringPieces objectAtIndex:i];
if ([chunk isEqualToString:@""])
{
continue; // skip this loop if the chunk is empty
}
// 3. Determine what type of word this is:
BOOL isTermsOfServiceLink = [chunk hasPrefix:@"<ts>"];
BOOL isPrivacyPolicyLink = [chunk hasPrefix:@"<pp>"];
BOOL isLink = (BOOL)(isTermsOfServiceLink || isPrivacyPolicyLink);
// 4. Create label, styling dependent on whether it's a link:
UILabel *label = [[UILabel alloc] init];
label.font = [UIFont systemFontOfSize:15.0f];
label.text = chunk;
label.userInteractionEnabled = isLink;
if (isLink)
{
label.textColor = [UIColor colorWithRed:110/255.0f green:181/255.0f blue:229/255.0f alpha:1.0];
label.highlightedTextColor = [UIColor yellowColor];
// 5. Set tap gesture for this clickable text:
SEL selectorAction = isTermsOfServiceLink ? @selector(tapOnTermsOfServiceLink:) : @selector(tapOnPrivacyPolicyLink:);
UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self
action:selectorAction];
[label addGestureRecognizer:tapGesture];
// Trim the markup characters from the label:
if (isTermsOfServiceLink)
label.text = [label.text stringByReplacingOccurrencesOfString:@"<ts>" withString:@""];
if (isPrivacyPolicyLink)
label.text = [label.text stringByReplacingOccurrencesOfString:@"<pp>" withString:@""];
}
else
{
label.textColor = [UIColor whiteColor];
}
// 6. Lay out the labels so it forms a complete sentence again:
// If this word doesn't fit at end of this line, then move it to the next
// line and make sure any leading spaces are stripped off so it aligns nicely:
[label sizeToFit];
if (self.agreeTextContainerView.frame.size.width < wordLocation.x + label.bounds.size.width)
{
wordLocation.x = 0.0; // move this word all the way to the left...
wordLocation.y += label.frame.size.height; // ...on the next line
// And trim of any leading white space:
NSRange startingWhiteSpaceRange = [label.text rangeOfString:@"^\\s*"
options:NSRegularExpressionSearch];
if (startingWhiteSpaceRange.location == 0)
{
label.text = [label.text stringByReplacingCharactersInRange:startingWhiteSpaceRange
withString:@""];
[label sizeToFit];
}
}
// Set the location for this label:
label.frame = CGRectMake(wordLocation.x,
wordLocation.y,
label.frame.size.width,
label.frame.size.height);
// Show this label:
[self.agreeTextContainerView addSubview:label];
// Update the horizontal position for the next word:
wordLocation.x += label.frame.size.width;
}
}
这是我处理检测到的对这些链接的点击的方法。
- (void)tapOnTermsOfServiceLink:(UITapGestureRecognizer *)tapGesture
{
if (tapGesture.state == UIGestureRecognizerStateEnded)
{
NSLog(@"User tapped on the Terms of Service link");
}
}
- (void)tapOnPrivacyPolicyLink:(UITapGestureRecognizer *)tapGesture
{
if (tapGesture.state == UIGestureRecognizerStateEnded)
{
NSLog(@"User tapped on the Privacy Policy link");
}
}
希望这会有所帮助。我确信有很多更聪明、更优雅的方法可以做到这一点,但这是我能够想出的,而且效果很好。
这是它在应用中的外观:
祝你好运! :-)
埃里克