Как установить угловой радиус фона NSString на iOS7

Я хочу установить угловой радиус фона NSString на iOS7. Но у NSString нет слоя...

Подскажите, пожалуйста, как настроить фоновый угол NSString на iOS7?

пример http://a5.mzstatic.com/jp/r30/Purple/v4/ba/d3/75/bad3753e  -0e54-43a2-6b8a-e56d4966b5bf/экран568x568.jpeg


person user3323951    schedule 18.02.2014    source источник


Ответы (6)


Вы можете сделать это, используя UITextView с подклассом NSLayoutManager, который переопределяет -fillBackgroundRectArray:count:forCharacterRange:color:. Просто небольшой пример, как это сделать:

@implementation ViewController
- (void)viewDidLoad
{
    [super viewDidLoad];

    // setup text handling
    NSTextStorage *textStorage = [[NSTextStorage alloc] initWithString:@"Lorem ipsum dolor sit er elit lamet, consectetaur cillium adipisicing pecu, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum. Nam liber te conscient to factor tum poen legum odioque civiuda."];

    // 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,20,self.view.bounds.size.width,self.view.bounds.size.height-20)
                                               textContainer:textContainer];
    [self.view addSubview:textView];

    // set some background color to our text
    [textView.textStorage setAttributes:[NSDictionary dictionaryWithObject:[UIColor blueColor] forKey:NSBackgroundColorAttributeName] range:NSMakeRange(22, textView.text.length - 61)];
}
@end

@interface MyLayoutManager : NSLayoutManager
@end

- (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

пример изображения

person Emmanuel    schedule 18.02.2014
comment
так что @Emmanuel есть ли способ использовать атрибуты в attributeText вместо NSTextStorage? Я попытался просто установить MyLayoutManager на свой UITextView, но у него все еще есть жесткие углы на фоне, что должно означать, что он не учитывает атрибуты в свойстве attributeText... - person Mike S; 11.02.2015
comment
Есть ли способ сделать это для текстового поля? - person user3752049; 16.10.2018
comment
любое решение для uilabel? - person famfamfam; 29.03.2021

Обновление до Swift 3.1 Код Эммануэля в Swift обновлен до версии 3.1

class TagLayoutManager: NSLayoutManager {

override func fillBackgroundRectArray(_ rectArray: UnsafePointer<CGRect>, count rectCount: Int, forCharacterRange charRange: NSRange, color: UIColor) {

    let cornerRadius:CGFloat = 5
    let path = CGMutablePath.init()




    if rectCount == 1 || (rectCount == 2 && (rectArray[1].maxX < rectArray[0].maxX)) {

        path.addRect(rectArray[0].insetBy(dx: cornerRadius, dy: cornerRadius))

        if rectCount == 2 {
            path.addRect(rectArray[1].insetBy(dx: cornerRadius, dy: cornerRadius))
        }

    } else {

        let lastRect = rectCount - 1

        path.move(to: CGPoint(x: rectArray[0].minX + cornerRadius, y: rectArray[0].maxY + cornerRadius))

        path.addLine(to: CGPoint(x: rectArray[0].minX + cornerRadius, y: rectArray[0].minY + cornerRadius))
        path.addLine(to: CGPoint(x: rectArray[0].maxX - cornerRadius, y: rectArray[0].minY + cornerRadius))

        path.addLine(to: CGPoint(x: rectArray[0].maxX - cornerRadius, y: rectArray[lastRect].minY - cornerRadius))
        path.addLine(to: CGPoint(x: rectArray[lastRect].maxX - cornerRadius, y: rectArray[lastRect].minY - cornerRadius))

        path.addLine(to: CGPoint(x: rectArray[lastRect].maxX - cornerRadius, y: rectArray[lastRect].maxY - cornerRadius))
        path.addLine(to: CGPoint(x: rectArray[lastRect].minX + cornerRadius, y: rectArray[lastRect].maxY - cornerRadius))

        path.addLine(to: CGPoint(x: rectArray[lastRect].minX + cornerRadius, y: rectArray[0].maxY + cornerRadius))

        path.closeSubpath()

    }

    color.set()

    let ctx = UIGraphicsGetCurrentContext()
    ctx!.setLineWidth(cornerRadius * 2.0)
    ctx!.setLineJoin(.round)

    ctx!.setAllowsAntialiasing(true)
    ctx!.setShouldAntialias(true)

    ctx!.addPath(path)
    ctx!.drawPath(using: .fillStroke)
}
}
person Keith Saft    schedule 01.06.2017
comment
Код без объяснения не является хорошим ответом. Прочтите это как ответить. - person thewaywewere; 01.06.2017
comment
OK. Код, как указано в заголовке поста, представляет собой просто обновление предыдущего кода Swift 2.x до Swift 3.1. - person Keith Saft; 01.06.2017

Код Эммануэля в Swift:

class TagLayoutManager : NSLayoutManager {

override func fillBackgroundRectArray(rectArray: UnsafePointer<CGRect>, count rectCount: Int, forCharacterRange charRange: NSRange, color: UIColor) {

    let cornerRadius:CGFloat = 3.0
    let path = CGPathCreateMutable()

    if rectCount == 1 || (rectCount == 2 && (CGRectGetMaxX(rectArray[1]) < CGRectGetMaxX(rectArray[0]))) {

        CGPathAddRect(path, nil, CGRectInset(rectArray[0], cornerRadius, cornerRadius))
        if rectCount == 2 {
            CGPathAddRect(path, nil, CGRectInset(rectArray[1], cornerRadius, cornerRadius))
        }

    } else {
            let lastRect = rectCount - 1

            CGPathMoveToPoint(path, nil, CGRectGetMinX(rectArray[0]) + cornerRadius, CGRectGetMaxY(rectArray[0]) + cornerRadius);

            CGPathAddLineToPoint(path, nil, CGRectGetMinX(rectArray[0]) + cornerRadius, CGRectGetMinY(rectArray[0]) + cornerRadius);
            CGPathAddLineToPoint(path, nil, CGRectGetMaxX(rectArray[0]) - cornerRadius, CGRectGetMinY(rectArray[0]) + cornerRadius);

            CGPathAddLineToPoint(path, nil, CGRectGetMaxX(rectArray[0]) - cornerRadius, CGRectGetMinY(rectArray[lastRect]) - cornerRadius);
            CGPathAddLineToPoint(path, nil, CGRectGetMaxX(rectArray[lastRect]) - cornerRadius, CGRectGetMinY(rectArray[lastRect]) - cornerRadius);

            CGPathAddLineToPoint(path, nil, CGRectGetMaxX(rectArray[lastRect]) - cornerRadius, CGRectGetMaxY(rectArray[lastRect]) - cornerRadius);
            CGPathAddLineToPoint(path, nil, CGRectGetMinX(rectArray[lastRect]) + cornerRadius, CGRectGetMaxY(rectArray[lastRect]) - cornerRadius);

            CGPathAddLineToPoint(path, nil, CGRectGetMinX(rectArray[lastRect]) + cornerRadius, CGRectGetMaxY(rectArray[0]) + cornerRadius);

            CGPathCloseSubpath(path);
        }

        color.set()

        let ctx = UIGraphicsGetCurrentContext()
        CGContextSetLineWidth(ctx, cornerRadius * 2.0)
        CGContextSetLineJoin(ctx, .Round)

        CGContextAddPath(ctx, path)

        CGContextDrawPath(ctx, .FillStroke)
    }

}
person João Nunes    schedule 14.01.2016

В дополнение к решению @Emmanuel добавление

CGContextSetAllowsAntialiasing(ctx, YES);
CGContextSetShouldAntialias(ctx, YES);

сделает его намного лучше.

person Gian Franco Zabarino    schedule 08.07.2015

У NSString этого нет. NSAttributedString имеет свойство backgroundColor, которое можно использовать, но без пользовательского рендеринга вы не сможете установить угловой радиус для выделения текстовых фрагментов.

Итак, вам нужно написать собственный UIView для отображения вашего текста.

person Daij-Djan    schedule 18.02.2014

NSString просто определяет строку текста. Он не определяет свойства того, как он отображается. Для отображения текста на экране обычно используется UILabel или UITextView. Однако для поведения выбора, которое вы показываете в своем примере, вам нужно будет сделать этот рисунок самостоятельно. Кроме того, он меняет цвет выделенного текста, поэтому вам нужно будет справиться с этим самостоятельно.

Если вы создали пользовательский UIView, который позволяет вам рисовать синюю область выбора, вы можете поместить ее за UILabel, и вы можете использовать NSAttributedString, чтобы установить текст метки, где «выбранный» текст будет белым, а не черным. Вероятно, это будет самый простой способ сделать это.

person Gavin    schedule 18.02.2014
comment
Не мной, но, вероятно, потому, что ваш ответ не учитывает атрибутированные строки, в которых хранится какая-то информация о визуальном представлении. - person Moshe; 28.12.2015