drawrect

How to fill a path with gradient in drawRect:?

佐手、 提交于 2019-11-28 15:23:23
filling a path with a solid color is easy enough: CGPoint aPoint; for (id pointValue in points) { aPoint = [pointValue CGPointValue]; CGContextAddLineToPoint(context, aPoint.x, aPoint.y); } [[UIColor redColor] setFill]; [[UIColor blackColor] setStroke]; CGContextDrawPath(context, kCGPathFillStroke); I'd like to draw a gradient instead of solid red, but I am having trouble. I've tried the code listed in the Question/Answer: Gradients on UIView and UILabels On iPhone which is: CAGradientLayer *gradient = [CAGradientLayer layer]; [gradient setFrame:rect]; [gradient setColors:[NSArray

Having UIView drawRect occur in a background thread

余生颓废 提交于 2019-11-28 11:28:05
I would like to have a UIView subclass that implements a method similar to setNeedsDisplay , except that redrawing (i.e., that would usually be called via drawRect: ) will occur in a background thread sometime soonish, rather than at the end of the current update cycle. It might be called setNeedsAsynchronousDisplay . Or the existing setNeedsDisplay could get hijacked and not cause redraw at the end of the cycle, or whatever, as long as it lets the redraw not happen on the main thread blocking screen updating an interaction until its completed. Until the redraw occurs, the view can continue to

iPhone SDK: How to trigger drawRect on UIView subclass after orientation change?

岁酱吖の 提交于 2019-11-28 09:43:44
I am subclassing a UIView and overwrite the drawRect method. I'm noticing that the view's drawrect is only being called when the view first loads. After that it is never called again. How to I make sure it gets called after an orientation change? I've tried calling setNeedsDisplay on the view before and after the rotation and that doesn't do it. [myView setContentMode:UIViewContentModeRedraw]; You can set this in IB as well (i.e., set mode to "redraw") This was a bug related to something completely different. setNeedsDisplay does indeed cause drawRect to be called. to answer this and the other

drawRect drawing 'transparent' text?

妖精的绣舞 提交于 2019-11-28 04:46:57
I am looking to draw a UILabel (preferable through subclassing) as a transparent label, but with solid background. I draw up an quick example (sorry, it's ugly, but it gets the points across :)). Basically I have a UILabel and I would like the background to be a set colour, and the text should be transparent. I do not want to colour the text with the views background, but instead have it be 100% transparent, since I have a texture in the background that I want to make sure lines up inside and outside of the label. I've been spending the night browsing SO and searching on Google, but I have

Getting a glyph boundingRect in draw#rect in UILabel

北慕城南 提交于 2019-11-28 04:09:37
问题 Using Swift, I want to get the boundingRect of a glyph, in draw#rect in a UILabel. The UILabel already has a size (say 300x300 in the example) and qualities such as the text being centered. class RNDLabel: UILabel { override func draw(_ rect: CGRect) { let manager = NSLayoutManager() let store = NSTextStorage(attributedString: NSAttributedString( string: text!, attributes: [NSAttributedString.Key.font: font])) store.addLayoutManager(manager) let textContainer = NSTextContainer(size: rect.size

iPhone: why isn't drawRect getting called?

我是研究僧i 提交于 2019-11-28 01:32:55
问题 Okay, I guess I'm missing something important, and I can't seem to find the answer. I'm posting all the code, because it's very small. Can someone please tell me what I'm doing wrong? I've worked on this looking at example after example, for quite a while, and nothing I do seems to work. When I create the app, I use whichever skeleton gives me a UIViewController. I put a view onto the controller. I create the variables to link into the controller. When I try to connect the UIView in my nib to

How to draw an UIImage or directly in -drawRect:?

霸气de小男生 提交于 2019-11-27 18:57:12
I have an UIImage which I want to draw on a UIView . But instead of creating an UIImageView and adding this as a subview, I want to overwrite -drawRect: and draw my UIView directly. For example, my code looks like: - (void)drawRect:(CGRect)rect { CGContextRef myContext = UIGraphicsGetCurrentContext(); UIImage *img = [UIImage imageNamed:@"foo.png"]; // how to draw the directly into the view now? } Call [img drawInRect:rect]; . BTW, you shouldn't load the image file in the drawRect method. Because the method is called whenever the view is required to update. Therefore, it (of course, loading

How can I tint a UIImage with gradient?

只谈情不闲聊 提交于 2019-11-27 17:43:33
I searched everywhere but didn't find the solution. I have image 1. How can I programatically tint them with gradient to get images 2 and 3? Here are those images: Tints that I applied to them via Photoshop are simple 2-color linear gradients. And my question is: how can I achieve this effect programatically? Solution: jrtc27 gave me almost working example. I fixed it (for ARC) and made it reusable (using UIImage's category). Here is it: - (UIImage *)tintedWithLinearGradientColors:(NSArray *)colorsArr { CGFloat scale = self.scale; UIGraphicsBeginImageContext(CGSizeMake(self.size.width * scale,

UIView with shadow, rounded corners and custom drawRect

ぃ、小莉子 提交于 2019-11-27 17:03:25
I have to create a custom UIView that will have round corners, a border, a shadow and its drawRect() method is overridden to provide custom drawing code with which several straight lines are drawn into the view (I need to use a fast, lightweight approach here since many of these views may be rendered). The problem I'm currently facing is that the shadow doesn't apply anymore to the round corners as soon as I override drawRect() in the view class (even without any custom code yet in it). See the attached image for the difference: In the view controller I'm using the following code: view.layer

Most efficient way to draw part of an image in iOS

心不动则不痛 提交于 2019-11-27 16:53:08
Given an UIImage and a CGRect , what is the most efficient way (in memory and time) to draw the part of the image corresponding to the CGRect (without scaling)? For reference, this is how I currently do it: - (void)drawRect:(CGRect)rect { CGContextRef context = UIGraphicsGetCurrentContext(); CGRect frameRect = CGRectMake(frameOrigin.x + rect.origin.x, frameOrigin.y + rect.origin.y, rect.size.width, rect.size.height); CGImageRef imageRef = CGImageCreateWithImageInRect(image_.CGImage, frameRect); CGContextTranslateCTM(context, 0, rect.size.height); CGContextScaleCTM(context, 1.0, -1.0);