objective c - How to set NSString's background cornerRadius on iOS7 -


I want to set NSString's background corner RDS on iOS7. But, NSString does not have a layer ...

Please tell me how to set NSString's background corner RADIO on iOS7?

Example http://a5.mzstatic.com/jp/r30/Purple/v4/ Ba / d3 / 75 / bad3753e-0e54-43a2-6b8a-e56d4966b5bf / screen568x568.jpeg

You can do this by using a UITextView subclass of NSLayoutManager , that override -fillBackgroundRectArray: count: forCharacterRange: color: . How to do a small sample:

  @implementation ViewController - (zero) viewDidload {{Super Viewedload]; // Setup Text Handling NSTextStorage * textStorage = [[NSTextStorage alloc] initWithString:. "@ Lorem sit ipsum dolor er change lamet, consectetaur cillium adipisicing pecu, SED eiusmod tempor is incididunt ut labore et dolore magna aliqua center no work to avoid using the best ad minim veniam, Quis drug Territories Apart from this, no kangaroo has been found, but it is used for any purpose. // NSLayoutManager MyLayoutManager * use our subclass of textLayout = [[MyLayoutManager alloc] init]; [TextStory AddLayout Manager: 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 a 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 - (minus) fillBackgroundRectArray: (constant CGRect *) rectArray count: (NSUInteger) rectCount forCharacterRange: (NSRange) charRange color: (UIColor *) color {CGFloat halfLineWidth = 4.; // Change the corners to change the radius CGMutablePathRef path = CGPathCreateMutable (); If (rectCount == 1 || (rectCount == 2 & amp; & amp; (CGRectGetMaxX (rectArray [1]) of & lt; CGRectGetMinX (rectArray [0])))) {// 1 in rect or contact CGPathAddRect 2 rects without edges (path, faucet, CGRactite (Rectairer [0], half leanwith, half leanwith)); If (rectCount == 2) CGPathAddRect (path, NULL, CGRectInset (rectArray [1], half linewidth, half leanwith)); } Other {// 2 or 3 notation NSUINTEGER final reference = rectact - 1; CGPathMoveToPoint (Path, Zero, CGRactGetManics (Reiki [0]) + halfline, CGRactget MXY (Rectairer [0]) + semi leanwith; CGPathAddLineToPoint (path, NULL, CGRactGetMinx (Rectairer [0]) + half leanwith, CGRactgetamine (Recirera [0]) + half leanwith; CGPathAddLineToPoint (Path, Zero, CGRTXXXx (Recurring [0]) - Half Leanwith, CGRact Gatmini (Reiki [0]) + half leanwith; CGPathAddLineToPoint (path, NULL, Sijiartiaksaksmaksaks (Riktaarey [0]) - Half Linvidth, Sijiaraktgetmini (Riktaare [final terms]) - Half Linvidth); CGPathAddLineToPoint (path, zero, Sijiaraktgetmaksaks (Rikaraare [final terms]) - Half Linvidth, Sijiaraktgetmini (Rikarare [final terms]) - Half Linvidth); CGPathAddLineToPoint (path, NULL, CGRectGetMaxX (rectArray [lastRect]) - Half leanwith, CGRactget MXY (Ricerre [last reference]) - semi-leanwith; CGPathAddLineToPoint (path, NULL, CGRectGetMinX (rectArray [final reference]) + semi leanwith, CGRactget MXY (reciever [last reference]) - semi-leanwith; CGPathAddLineToPoint (Path, NULL, CGRectGetMinX (rectArray [Final Reference]) + SemiLinVidth, CGRactGetMXY (ReikiErray [0]) + SemiLinvidha); CGPathCloseSubpath (path); } [Color set]; // fill the set and stroke color CGContextRef ctx = UIGraphicsGetCurrentContext (); CGContextSetLineWidth (CTX, Half Linwidth * 2.); CGContextSetLineJoin (CTX, KCGLineJoinRound); CGContextAddPath (ctx, path); CGPathRelease (path); CGContextDrawPath (ctx, kCGPathFillStroke); } @end  

sample image


Comments