ios中图像上的文本手势
Posted
技术标签:
【中文标题】ios中图像上的文本手势【英文标题】:Gestures for text on image in ios 【发布时间】:2016-03-03 06:01:21 【问题描述】:我想在图片上添加文字。用户可以在该图像的任何位置调整文本。我正在使用 UIRotationGestureRecognizer、UIPanGestureRecognizer、UIPinchGestureRecognizer。但问题是当我做手势时,文本在我的视野中到处移动。我希望它仅在该图像视图上移动、缩放、旋转。请帮忙。为此,我在主视图中采用了一个 UIView。然后是子视图中的 UIImageView 和 UITextView。
【问题讨论】:
【参考方案1】: [self buildAgreeTextViewFromString:NSLocalizedString(str,@"\UserName\xgdfg\" #<ts>#") forRow:indexPath.row];
- (void)buildAgreeTextViewFromString:(NSString *)localizedString forRow:(long)row
// 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 fontWithName:@"OpenSans" size:13.0F];
// label.font = [UIFont systemFontOfSize:11.0f];
label.text = chunk;
label.textColor = [UIColor yellowColor];
label.userInteractionEnabled = isLink;
label.backgroundColor = [UIColor clearColor];
label.tag = row;
if (isLink)
//label.textColor = [UIColor blueColor];
//[self.view setBackgroundColor: [self colorWithHexString:@"FFFFFF"]];
label.textColor = UIColorFromRGB(text1_color_hexcode);
//1D89FF
//label.highlightedTextColor = [UIColor yellowColor];
// 5. Set tap gesture for this clickable text:
SEL selectorAction = isTermsOfServiceLink ? @selector(tapUserName:) : @selector(tapPost:);
UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:selectorAction];
[label addGestureRecognizer:tapGesture];
tapGesture.numberOfTapsRequired = 1;
tapGesture.delegate = self;
// 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 darkGrayColor];
[label sizeToFit];
if (cell.notificationTxt_view.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
NSRange startingWhiteSpaceRange = [label.text rangeOfString:@"^\\s*"
options:NSRegularExpressionSearch];
if (startingWhiteSpaceRange.location == 0)
label.text = [label.text stringByReplacingCharactersInRange:startingWhiteSpaceRange withString:@""];
[label sizeToFit];
//CGFloat y = (cell.notificationTxt_view.frame.size.height - label.frame.size.height)/2;
label.frame = CGRectMake(wordLocation.x,
wordLocation.y,
label.frame.size.width,
label.frame.size.height);
[cell.notificationTxt_view addSubview:label];
wordLocation.x += label.frame.size.width;
NSLog(@"%ld",row);
if(row==[notificationArray count]-1)
chekScrolling=NO;
【讨论】:
以上是关于ios中图像上的文本手势的主要内容,如果未能解决你的问题,请参考以下文章
iOS 如何在滚动视图/webview 上实现点击手势以“敲出”文本字段?
在 iOS Swift 中使用键盘平移手势处理文本输入视图移动