【问题标题】:UITextField rounded corner textUITextField 圆角文字
【发布时间】:2016-10-10 11:56:44
【问题描述】:

有什么方法可以添加圆角和灰色背景色的字符串数组。但是最后一个字符串在我的 UITextField 上没有这些属性,只使用属性文本?

【问题讨论】:

标签: ios swift uitextfield


【解决方案1】:

我不得不将我的 UITextField 更改为 UITextView,然后使用来自 How to set NSString's background cornerRadius on iOS7 的解决方案,并使用 UITextView 委托进行了一些调整。

@implementation MyLayoutManager
- (void)fillBackgroundRectArray:(const CGRect *)rectArray count:(NSUInteger)rectCount forCharacterRange:(NSRange)charRange color:(UIColor *)color
{
    CGFloat halfLineWidth = 4.; // change this to change corners radius

    CGMutablePathRef path = CGPathCreateMutable();

    if (rectCount == 1
        || (rectCount == 2 && (CGRectGetMaxX(rectArray[1]) < CGRectGetMinX(rectArray[0])))
        )
    {
        // 1 rect or 2 rects without edges in contact

        CGPathAddRect(path, NULL, CGRectInset(rectArray[0], halfLineWidth, halfLineWidth));
        if (rectCount == 2)
            CGPathAddRect(path, NULL, CGRectInset(rectArray[1], halfLineWidth, halfLineWidth));
    }
    else
    {
        // 2 or 3 rects
        NSUInteger lastRect = rectCount - 1;

        CGPathMoveToPoint(path, NULL, CGRectGetMinX(rectArray[0]) + halfLineWidth, CGRectGetMaxY(rectArray[0]) + halfLineWidth);

        CGPathAddLineToPoint(path, NULL, CGRectGetMinX(rectArray[0]) + halfLineWidth, CGRectGetMinY(rectArray[0]) + halfLineWidth);
        CGPathAddLineToPoint(path, NULL, CGRectGetMaxX(rectArray[0]) - halfLineWidth, CGRectGetMinY(rectArray[0]) + halfLineWidth);

        CGPathAddLineToPoint(path, NULL, CGRectGetMaxX(rectArray[0]) - halfLineWidth, CGRectGetMinY(rectArray[lastRect]) - halfLineWidth);
        CGPathAddLineToPoint(path, NULL, CGRectGetMaxX(rectArray[lastRect]) - halfLineWidth, CGRectGetMinY(rectArray[lastRect]) - halfLineWidth);

        CGPathAddLineToPoint(path, NULL, CGRectGetMaxX(rectArray[lastRect]) - halfLineWidth, CGRectGetMaxY(rectArray[lastRect]) - halfLineWidth);
        CGPathAddLineToPoint(path, NULL, CGRectGetMinX(rectArray[lastRect]) + halfLineWidth, CGRectGetMaxY(rectArray[lastRect]) - halfLineWidth);

        CGPathAddLineToPoint(path, NULL, CGRectGetMinX(rectArray[lastRect]) + halfLineWidth, CGRectGetMaxY(rectArray[0]) + halfLineWidth);

        CGPathCloseSubpath(path);
    }

    [color set]; // set fill and stroke color

    CGContextRef ctx = UIGraphicsGetCurrentContext();
    CGContextSetLineWidth(ctx, halfLineWidth * 2.);
    CGContextSetLineJoin(ctx, kCGLineJoinRound);

    CGContextAddPath(ctx, path);
    CGPathRelease(path);

    CGContextDrawPath(ctx, kCGPathFillStroke);
}
@end

    - (void)updateHeaderWithText:(NSString*) text {
        // setup text handling
        NSTextStorage *textStorage = [[NSTextStorage alloc] initWithString:text];

        // use our subclass of NSLayoutManager
        MyLayoutManager *textLayout = [[MyLayoutManager alloc] init];

        [textStorage addLayoutManager:textLayout];

        NSTextContainer *textContainer = [[NSTextContainer alloc] initWithSize:self.view.bounds.size];

        [textLayout addTextContainer:textContainer];
        UITextView *textView = [[UITextView alloc]initWithFrame:CGRectMake(0, 0, self.navigationController.navigationBar.frame.size.width, 30.0) textContainer:textContainer];
        self.navigationItem.titleView = textView;
        // set some background color to our text
        NSInteger rangeInitPlace = 0;
        for (NSString *word in self.namesArray) {
            if (![word isEqualToString:@" , "]) {
                [textView.textStorage setAttributes:[NSDictionary dictionaryWithObject:[UIColor lightGrayColor] forKey:NSBackgroundColorAttributeName] range:NSMakeRange(rangeInitPlace, word.length)];
            }
            rangeInitPlace += word.length;
        }
        textView.delegate = self;
        [textView becomeFirstResponder];
        textView.selectedRange = NSMakeRange([textView.text length], 0);
    }

-(BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text {
    NSRange selectedRange = textView.selectedRange;

    UITextPosition *beginning = textView.beginningOfDocument;
    UITextPosition *start = [textView positionFromPosition:beginning offset:selectedRange.location];
    UITextPosition *end = [textView positionFromPosition:start offset:selectedRange.length];

    UITextRange* textRange = [textView.tokenizer rangeEnclosingPosition:end withGranularity:UITextGranularityWord inDirection:UITextLayoutDirectionLeft];

    NSLog(@"Word that is currently being edited is : %@", [textView textInRange:textRange]);
    NSDictionary *normalAttrdict = [NSDictionary dictionaryWithObject:[UIColor brownColor] forKey:NSForegroundColorAttributeName];
    textView.typingAttributes = normalAttrdict;
    return YES;
}

【讨论】:

  • 如果 UItextView 在故事板中,我该怎么做??
【解决方案2】:

对于 Swift 开发者

有时会有所帮助。

Rounded Corner

【讨论】:

    【解决方案3】:

    试试zftokenfield,你可以根据自己的需求改变这个组件的UI

    【讨论】:

      猜你喜欢
      • 2012-11-22
      • 2015-08-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-05-27
      • 2011-07-24
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多