Ios 如何使用删除选项实现带边框文本的UItextview或UItextfield?
我正在从事一个项目,在这个项目中,我必须为UITextView或UITextField实现类似web的功能,并在文本上使用delete选项分离背景。它看起来应该完全显示在我所附的图片。请帮助实现这一目标 以下是我迄今为止所尝试的:Ios 如何使用删除选项实现带边框文本的UItextview或UItextfield?,ios,objective-c,uitextfield,uitextview,Ios,Objective C,Uitextfield,Uitextview,我正在从事一个项目,在这个项目中,我必须为UITextView或UITextField实现类似web的功能,并在文本上使用delete选项分离背景。它看起来应该完全显示在我所附的图片。请帮助实现这一目标 以下是我迄今为止所尝试的: NSMutableAttributedString * string = [[NSMutableAttributedString alloc]initWithString:txtString]; NSArray *words=[txtStri
NSMutableAttributedString * string = [[NSMutableAttributedString alloc]initWithString:txtString];
NSArray *words=[txtString componentsSeparatedByString:@","];
for (NSString *word in words) {
[txtString stringByReplacingOccurrencesOfString:@"," withString:@" "]; //if ([word hasSuffix:@","]) {
self.m_interestsTxtView.font = [UIFont boldSystemFontOfSize:17];
self.m_interestsTxtView.textColor = [UIColor whiteColor];
NSRange range=[txtString rangeOfString:word];
[string addAttribute:NSBackgroundColorAttributeName value:[UIColor blackColor] range:range];
[string addAttribute:NSForegroundColorAttributeName value: [UIColor whiteColor] range:range];
[self.m_interestsTxtView setAttributedText:string];
}
最简单的方法是使用xib创建带有按钮和标签的UIView。另一种方法是创建一个自定义视图,作为满足您需求的模板,并向模板添加UILabel。我做这个是为了学习。我希望你能根据你的要求定制 自定义UIView类:
- (void)drawRect:(CGRect)frame {
CGContextRef context = UIGraphicsGetCurrentContext();
UIColor* color = [UIColor colorWithRed: 1 green: 1 blue: 1 alpha: 1];
UIBezierPath* rectanglePath = [UIBezierPath bezierPathWithRoundedRect: CGRectMake(CGRectGetMinX(frame), CGRectGetMinY(frame), 82, 36) cornerRadius: 11];
[color setFill];
[rectanglePath fill];
[UIColor.darkGrayColor setStroke];
rectanglePath.lineWidth = 1;
[rectanglePath stroke];
UIBezierPath* ovalPath = [UIBezierPath bezierPathWithOvalInRect: CGRectMake(CGRectGetMinX(frame) + 67, CGRectGetMinY(frame) + 3, 11, 11)];
[UIColor.redColor setFill];
[ovalPath fill];
CGContextSaveGState(context);
CGContextTranslateCTM(context, CGRectGetMinX(frame) + 71.5, CGRectGetMinY(frame) + 8.5);
UIBezierPath* bezierPath = UIBezierPath.bezierPath;
[bezierPath moveToPoint: CGPointMake(-4, 0)];
[bezierPath addCurveToPoint: CGPointMake(5, 0) controlPoint1: CGPointMake(5, 0) controlPoint2: CGPointMake(5, 0)];
[UIColor.whiteColor setStroke];
bezierPath.lineWidth = 1;
[bezierPath stroke];
CGContextRestoreGState(context);
}
我在自定义视图中添加了一个UILabel“Egg”
这将给你:
添加这些自定义视图我将它们称为buttonLabel到数组buttonLabelsArray
如果您想使用UIButton显示X标记,则不必管理触摸,但如果您不想使用UIButton,则应精确定位触摸
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
for (UIView *but in buttonLabelsArray) {
CGPoint locationPoint = [[touches anyObject] locationInView:but];
if (CGRectContainsPoint(but.bounds, [touches.anyObject locationInView:but])==YES){
if ( (locationPoint.x>=64 && locationPoint.x<=80)
&& (locationPoint.y>=3 && locationPoint.y<=12) ) {
NSLog(@"pressed close button");
[but removeFromSuperview];
//Do something else
}
}
}
}
您可能必须更改if
( (locationPoint.x>=64 && locationPoint.x<=80)
&& (locationPoint.y>=3 && locationPoint.y<=12) )
基于自定义视图框的条件。有无数种方法可以做到这一点。你试过什么?嗨,为什么不投票。。我无法找到解决方案,因此我发布了。相反,你会帮助我实现目标。你可能足够聪明,可以破解这个,但不是每个人都会是你。。。这是我尝试过的……我没有否决你,我想在给你提建议之前看看你尝试过什么。对不起!请帮助实现我已经编辑了我的帖子,我使用了属性字符串,但没有按预期工作。最简单的方法是创建一个带有按钮和标签的UIVIew。