我試圖做一個更復雜的UILabels繪圖(或UIView,把UILabel放在頂部,應該是必需的)背景。因爲我想要這個在用戶更改iPhone方向時自動調整大小,所以我試圖在子類drawRect函數中實現它。如果可能的話,我希望能夠在代碼中調整這一切,而不需要模式圖像。UIView CGGradient修剪繪製「框架」
我得到了一些提示,從一些關於這個問題前職位:
Gradients on UIView and UILabels On iPhone 和 Make Background of UIView a Gradient Without Sub Classing
遺憾的是他們都錯過了目標,因爲我想梯度自定義繪製結合起來。我想讓CGGradient被我正在繪製的線框剪裁(可以在圓角處看到),但是我無法做到這一點。
另一種方法是使用CAGradientLayer,它似乎工作得很好,但這需要對框架進行旋轉時的一些調整,並且漸變層似乎在文本的頂部繪製,無論我如何嘗試。
我的問題爲此是雙重
- 如何修改下面的代碼,以使梯度剪輯繪製的「幀」
- 我如何CAGradientLayer繪製的UILabel的文本後面(或我必須把落後的UILabel一個UIView與CAGradientLayer爲背景)
這裏是我的drawRect功能:
- (void)drawRect:(CGRect)rect {
// Drawing code
CGContextRef c = UIGraphicsGetCurrentContext();
CGContextSetFillColorWithColor(c, [[UIColor clearColor] CGColor]);
CGContextSetStrokeColorWithColor(c, [strokeColor CGColor]);
CGContextSetLineWidth(c, 1);
CGFloat minx = CGRectGetMinX(rect), midx = CGRectGetMidX(rect), maxx = CGRectGetMaxX(rect) ;
CGFloat miny = CGRectGetMinY(rect), midy = CGRectGetMidY(rect), maxy = CGRectGetMaxY(rect) ;
minx = minx + 1;
miny = miny + 1;
maxx = maxx - 1;
maxy = maxy - 1;
CGGradientRef glossGradient;
CGColorSpaceRef rgbColorspace;
size_t num_locations = 2;
CGFloat locations[2] = { 0.0, 1.0 };
CGFloat components[8] = { 0.6, 0.6, 0.6, 1.0, // Start color
0.3, 0.3, 0.3, 1.0 }; // End color
rgbColorspace = CGColorSpaceCreateDeviceRGB();
glossGradient = CGGradientCreateWithColorComponents(rgbColorspace, components, locations, num_locations);
CGRect currentBounds = [self bounds];
CGPoint topCenter = CGPointMake(CGRectGetMidX(currentBounds), 0.0f);
CGPoint midCenter = CGPointMake(CGRectGetMidX(currentBounds), CGRectGetMidY(currentBounds));
CGPoint lowCenter = CGPointMake(CGRectGetMidX(currentBounds), CGRectGetMidY(currentBounds));
CGContextDrawLinearGradient(c, glossGradient, topCenter, midCenter, 0);
CGGradientRelease(glossGradient);
CGColorSpaceRelease(rgbColorspace);
CGContextMoveToPoint(c, minx, midy);
CGContextAddArcToPoint(c, minx, miny, midx, miny, ROUND_SIZE_INFO);
CGContextAddArcToPoint(c, maxx, miny, maxx, midy, ROUND_SIZE_INFO);
CGContextAddArcToPoint(c, maxx, maxy, midx, maxy, ROUND_SIZE_INFO);
CGContextAddArcToPoint(c, minx, maxy, minx, midy, ROUND_SIZE_INFO);
// Close the path
CGContextClosePath(c);
// Fill & stroke the path
CGContextDrawPath(c, kCGPathFillStroke);
// return;
[super drawRect: rect];
基本上,我想知道是否有辦法將Gradient限制到某個邊界,有點像CALayer的maskToBounds標誌。 – jollyCocoa 2010-10-03 08:10:00
使用CAGradientLayer似乎足以解決簡單的矩形,漸變背景,但問題仍然存在,如果可能的話,完成與自定義繪圖相同的任務...(如果我想繪製一個非方形邊框的自定義邊框背景使用CGContextAddLineToPoint()和CGContextAddArcToPoint()。是否有可能用漸變填充邊界內容,這將如何完成)? – jollyCocoa 2010-10-04 07:32:26